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

socket: no need to initialize WinSock on each request

This commit is contained in:
vitalyster 2019-10-09 21:01:31 +03:00 committed by Alexander Medvednikov
parent febd532c4a
commit 0796e1dd69
6 changed files with 38 additions and 34 deletions

View File

@ -10,7 +10,6 @@ INT port_number = 443;
BOOL use_proxy = FALSE; BOOL use_proxy = FALSE;
DWORD protocol = 0; DWORD protocol = 0;
ALG_ID aid_key_exch = 0; ALG_ID aid_key_exch = 0;
WSADATA wsa_data;
// TODO: joe-c // TODO: joe-c
// socket / tls ctx // socket / tls ctx
@ -70,7 +69,6 @@ void vschannel_cleanup(TlsContext *tls_ctx) {
} }
void vschannel_init(TlsContext *tls_ctx) { void vschannel_init(TlsContext *tls_ctx) {
WSAStartup(0x202, &wsa_data);
tls_ctx->sspi = InitSecurityInterface(); tls_ctx->sspi = InitSecurityInterface();
if(tls_ctx->sspi == NULL) { if(tls_ctx->sspi == NULL) {

View File

@ -4,3 +4,5 @@ module net
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
fn init() int { return 1 }

31
vlib/net/init_win.v Normal file
View File

@ -0,0 +1,31 @@
module net
#flag -lws2_32
#include <winsock2.h>
#include <Ws2tcpip.h>
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
}

View File

@ -1,5 +1,9 @@
module net module net
const (
_ = net.init()
)
// hostname returns the host name reported by the kernel. // hostname returns the host name reported by the kernel.
pub fn hostname() ?string { pub fn hostname() ?string {
mut name := [256]byte mut name := [256]byte
@ -11,3 +15,4 @@ pub fn hostname() ?string {
} }
return tos_clone(name) return tos_clone(name)
} }

View File

@ -8,24 +8,6 @@ pub:
proto int 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 { struct C.in_addr {
mut: mut:
@ -55,13 +37,6 @@ 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(WSA_V22, &wsadata)
if res != 0 {
return error('socket: WSAStartup failed')
}
}
sockfd := C.socket(family, _type, proto) sockfd := C.socket(family, _type, proto)
one:=1 one:=1
@ -316,5 +291,3 @@ pub fn (s Socket) get_port() int {
sockname_res := C.getsockname(s.sockfd, &addr, &size) sockname_res := C.getsockname(s.sockfd, &addr, &size)
return int(C.ntohs(addr.sin_port)) return int(C.ntohs(addr.sin_port))
} }

View File

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