From 0796e1dd6920d6b0e46cc78aa89da17a1548fc94 Mon Sep 17 00:00:00 2001 From: vitalyster Date: Wed, 9 Oct 2019 21:01:31 +0300 Subject: [PATCH] socket: no need to initialize WinSock on each request --- thirdparty/vschannel/vschannel.c | 2 -- vlib/net/{socket_nix.v => init_nix.v} | 2 ++ vlib/net/init_win.v | 31 +++++++++++++++++++++++++++ vlib/net/net.v | 5 +++++ vlib/net/socket.v | 27 ----------------------- vlib/net/socket_win.v | 5 ----- 6 files changed, 38 insertions(+), 34 deletions(-) rename vlib/net/{socket_nix.v => init_nix.v} (77%) create mode 100644 vlib/net/init_win.v delete mode 100644 vlib/net/socket_win.v diff --git a/thirdparty/vschannel/vschannel.c b/thirdparty/vschannel/vschannel.c index 3ea4891193..74ddc94e30 100644 --- a/thirdparty/vschannel/vschannel.c +++ b/thirdparty/vschannel/vschannel.c @@ -10,7 +10,6 @@ INT port_number = 443; BOOL use_proxy = FALSE; DWORD protocol = 0; ALG_ID aid_key_exch = 0; -WSADATA wsa_data; // TODO: joe-c // socket / tls ctx @@ -70,7 +69,6 @@ void vschannel_cleanup(TlsContext *tls_ctx) { } void vschannel_init(TlsContext *tls_ctx) { - WSAStartup(0x202, &wsa_data); tls_ctx->sspi = InitSecurityInterface(); if(tls_ctx->sspi == NULL) { diff --git a/vlib/net/socket_nix.v b/vlib/net/init_nix.v similarity index 77% rename from vlib/net/socket_nix.v rename to vlib/net/init_nix.v index 6b6b8e1124..4caced4df7 100644 --- a/vlib/net/socket_nix.v +++ b/vlib/net/init_nix.v @@ -4,3 +4,5 @@ module net #include #include #include + +fn init() int { return 1 } diff --git a/vlib/net/init_win.v b/vlib/net/init_win.v new file mode 100644 index 0000000000..1155e7fbd8 --- /dev/null +++ b/vlib/net/init_win.v @@ -0,0 +1,31 @@ +module net + +#flag -lws2_32 +#include +#include + +struct C.WSAData { +mut: + wVersion u16 + wHighVersion u16 + szDescription [257]byte + szSystemStatus [129]byte + iMaxSockets u16 + iMaxUdpDg u16 + lpVendorInfo byteptr +} + + +const ( + WSA_V22 = 0x202 // C.MAKEWORD(2, 2) +) + +fn init() int { + mut wsadata := C.WSAData{} + res := C.WSAStartup(WSA_V22, &wsadata) + if res != 0 { + panic('socket: WSAStartup failed') + } + return 1 +} + diff --git a/vlib/net/net.v b/vlib/net/net.v index b6a157b96c..2101d36cc8 100644 --- a/vlib/net/net.v +++ b/vlib/net/net.v @@ -1,5 +1,9 @@ module net +const ( + _ = net.init() +) + // hostname returns the host name reported by the kernel. pub fn hostname() ?string { mut name := [256]byte @@ -11,3 +15,4 @@ pub fn hostname() ?string { } return tos_clone(name) } + diff --git a/vlib/net/socket.v b/vlib/net/socket.v index 3fcbfce2f5..e986ccf5b7 100644 --- a/vlib/net/socket.v +++ b/vlib/net/socket.v @@ -8,24 +8,6 @@ pub: proto int } -struct C.WSAData { -mut: - wVersion u16 - wHighVersion u16 - szDescription [257]byte - szSystemStatus [129]byte - iMaxSockets u16 - iMaxUdpDg u16 - lpVendorInfo byteptr -} - -const ( - WSA_V1 = 0x100 // C.MAKEWORD(1, 0) - WSA_V11 = 0x101 // C.MAKEWORD(1, 1) - WSA_V2 = 0x200 // C.MAKEWORD(2, 0) - WSA_V21 = 0x201 // C.MAKEWORD(2, 1) - WSA_V22 = 0x202 // C.MAKEWORD(2, 2) -) struct C.in_addr { mut: @@ -55,13 +37,6 @@ 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(WSA_V22, &wsadata) - if res != 0 { - return error('socket: WSAStartup failed') - } - } sockfd := C.socket(family, _type, proto) one:=1 @@ -316,5 +291,3 @@ pub fn (s Socket) get_port() int { sockname_res := C.getsockname(s.sockfd, &addr, &size) return int(C.ntohs(addr.sin_port)) } - - diff --git a/vlib/net/socket_win.v b/vlib/net/socket_win.v deleted file mode 100644 index b50da976fd..0000000000 --- a/vlib/net/socket_win.v +++ /dev/null @@ -1,5 +0,0 @@ -module net - -#flag -lws2_32 -#include -#include