net: add windows socket support
							parent
							
								
									2cb12b4f4e
								
							
						
					
					
						commit
						5dfd5fa3e7
					
				| 
						 | 
					@ -1,9 +1,5 @@
 | 
				
			||||||
module net
 | 
					module net
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/socket.h>
 | 
					 | 
				
			||||||
#include <netinet/in.h>
 | 
					 | 
				
			||||||
#include <netdb.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct Socket {
 | 
					struct Socket {
 | 
				
			||||||
pub:
 | 
					pub:
 | 
				
			||||||
	sockfd int
 | 
						sockfd int
 | 
				
			||||||
| 
						 | 
					@ -25,8 +21,21 @@ import const (
 | 
				
			||||||
	SHUT_RD
 | 
						SHUT_RD
 | 
				
			||||||
	SHUT_WR
 | 
						SHUT_WR
 | 
				
			||||||
	SHUT_RDWR
 | 
						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 {
 | 
					struct C.in_addr {
 | 
				
			||||||
mut:
 | 
					mut:
 | 
				
			||||||
	s_addr int
 | 
						s_addr int
 | 
				
			||||||
| 
						 | 
					@ -54,6 +63,12 @@ struct C.sockaddr_storage {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// create socket
 | 
					// create socket
 | 
				
			||||||
pub fn socket(family int, _type int, proto int) 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)
 | 
						sockfd := C.socket(family, _type, proto)
 | 
				
			||||||
	s := Socket {
 | 
						s := Socket {
 | 
				
			||||||
		sockfd: sockfd
 | 
							sockfd: sockfd
 | 
				
			||||||
| 
						 | 
					@ -185,14 +200,36 @@ pub fn (s Socket) recv(bufsize int) byteptr {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// shutdown and close socket
 | 
					// shutdown and close socket
 | 
				
			||||||
pub fn (s Socket) close() int {
 | 
					pub fn (s Socket) close() int {
 | 
				
			||||||
	shutdown_res := C.shutdown(s.sockfd, SHUT_RDWR)
 | 
						// WinSock
 | 
				
			||||||
	if shutdown_res < 0 {
 | 
						$if windows {
 | 
				
			||||||
		println('socket: shutdown failed')
 | 
							C.WSACleanup()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	res := C.close(s.sockfd)
 | 
					
 | 
				
			||||||
	if res < 0 {
 | 
						$if windows {
 | 
				
			||||||
		println('socket: close failed')
 | 
							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
 | 
						return 0
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					module net
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <sys/socket.h>
 | 
				
			||||||
 | 
					#include <netinet/in.h>
 | 
				
			||||||
 | 
					#include <netdb.h>
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					module net
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#flag -lws2_32
 | 
				
			||||||
 | 
					#include <winsock2.h>
 | 
				
			||||||
 | 
					#include <Ws2tcpip.h>
 | 
				
			||||||
		Loading…
	
		Reference in New Issue