1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: re-apply C++ support

This commit is contained in:
Uwe Krüger
2020-05-18 21:38:06 +02:00
committed by GitHub
parent 895cefe351
commit 2635be511f
33 changed files with 261 additions and 150 deletions

View File

@ -15,6 +15,9 @@ mut:
s_addr int
}
struct C.sockaddr {
}
struct C.sockaddr_in {
mut:
sin_family int
@ -108,7 +111,9 @@ pub fn (s Socket) bind(port int) ?int {
addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := 16 // sizeof(C.sockaddr_in)
res := C.bind(s.sockfd, &addr, size)
tmp := voidptr(&addr)
skaddr := &C.sockaddr(tmp)
res := C.bind(s.sockfd, skaddr, size)
if res < 0 {
return error('net.bind: failed with $res')
}
@ -165,7 +170,9 @@ pub fn (s Socket) accept() ?Socket {
}
addr := C.sockaddr_storage{}
size := 128 // sizeof(sockaddr_storage)
sockfd := C.accept(s.sockfd, &addr, &size)
tmp := voidptr(&addr)
skaddr := &C.sockaddr(tmp)
sockfd := C.accept(s.sockfd, skaddr, &size)
if sockfd < 0 {
return error('net.accept: failed with $sockfd')
}
@ -255,8 +262,8 @@ pub fn (s Socket) cread(buffer byteptr, buffersize int) int {
// Receive a message from the socket, and place it in a preallocated buffer buf,
// with maximum message size bufsize. Returns the length of the received message.
pub fn (s Socket) crecv(buffer byteptr, buffersize int) int {
return C.recv(s.sockfd, buffer, buffersize, 0)
pub fn (s Socket) crecv(buffer voidptr, buffersize int) int {
return C.recv(s.sockfd, byteptr(buffer), buffersize, 0)
}
// shutdown and close socket
@ -325,7 +332,8 @@ pub fn (s Socket) read_line() string {
break
}
}
line = tos_clone(buf)
bufbp := byteptr(buf)
line = tos_clone(bufbp)
if eol_idx > 0 {
// At this point, we are sure that recv returned valid data,
// that contains *at least* one line.
@ -357,7 +365,8 @@ pub fn (s Socket) read_all() string {
if n == 0 {
return res
}
res += tos_clone(buf)
bufbp := byteptr(buf)
res += tos_clone(bufbp)
}
return res
}
@ -365,6 +374,8 @@ pub fn (s Socket) read_all() string {
pub fn (s Socket) get_port() int {
mut addr := C.sockaddr_in{}
size := 16 // sizeof(sockaddr_in)
C.getsockname(s.sockfd, &addr, &size)
tmp := voidptr(&addr)
skaddr := &C.sockaddr(tmp)
C.getsockname(s.sockfd, skaddr, &size)
return C.ntohs(addr.sin_port)
}