net: add windows socket support

pull/1133/head
Justice Suh 2019-07-13 17:29:00 -04:00 committed by Alexander Medvednikov
parent 2cb12b4f4e
commit 5dfd5fa3e7
3 changed files with 58 additions and 11 deletions

View File

@ -1,9 +1,5 @@
module net
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
struct Socket {
pub:
sockfd int
@ -25,8 +21,21 @@ import const (
SHUT_RD
SHUT_WR
SHUT_RDWR
SD_BOTH
)
// used for WinSock init
struct C.WSAData {
mut:
wVersion u16
wHighVersion u16
szDescription [257]byte
szSystemStatus [129]byte
iMaxSockets u16
iMaxUdpDg u16
lpVendorInfo byteptr
}
struct C.in_addr {
mut:
s_addr int
@ -54,6 +63,12 @@ struct C.sockaddr_storage {}
// create socket
pub fn socket(family int, _type int, proto int) Socket {
$if windows {
mut wsadata := C.WSAData{}
res := C.WSAStartup(0x202, &wsadata)
// TODO: throw error if WSAStartup fails
}
sockfd := C.socket(family, _type, proto)
s := Socket {
sockfd: sockfd
@ -185,14 +200,36 @@ pub fn (s Socket) recv(bufsize int) byteptr {
// shutdown and close socket
pub fn (s Socket) close() int {
shutdown_res := C.shutdown(s.sockfd, SHUT_RDWR)
if shutdown_res < 0 {
println('socket: shutdown failed')
// WinSock
$if windows {
C.WSACleanup()
}
res := C.close(s.sockfd)
if res < 0 {
println('socket: close failed')
$if windows {
shutdown_res := C.shutdown(s.sockfd, SD_BOTH)
if shutdown_res < 0 {
println('socket: shutdown failed')
}
}
$else {
shutdown_res := C.shutdown(s.sockfd, SHUT_RDWR)
if shutdown_res < 0 {
println('socket: shutdown failed')
}
}
$if windows {
res := C.closesocket(s.sockfd)
if res < 0 {
println('socket: close failed')
}
}
$else {
res := C.close(s.sockfd)
if res < 0 {
println('socket: close failed')
}
}
return 0
}

View File

@ -0,0 +1,5 @@
module net
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

View File

@ -0,0 +1,5 @@
module net
#flag -lws2_32
#include <winsock2.h>
#include <Ws2tcpip.h>