Skip to content

Commit 5d7df2b

Browse files
committed
Fix httpClient example on windows.
1 parent b1fe830 commit 5d7df2b

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/fbNetworkClient.bas

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function getIpAdress(addressInfo as addrInfo ptr) as string
2929
IP = space(16)
3030
inet_ntop( addressInfo->ai_family, @(cast(sockaddr_in ptr,addressInfo->ai_addr)->sin_addr), strptr(IP), len(IP) )
3131
end if
32-
3332
return trim(IP)
3433
end function
3534

@@ -124,26 +123,38 @@ function fbNetworkClient.open(address as string, _port as uinteger, timeoutValue
124123
return false
125124
end if
126125

127-
dim socketFlags as integer = fcntl(this._socket, F_GETFL, 0)
128-
fcntl(this._socket, F_SETFL, socketFlags or O_NONBLOCK)
126+
#ifdef __FB_WIN32__
127+
dim as integer blocking = 1
128+
ioctlsocket(this._socket, FIONBIO, @blocking)
129+
#else
130+
dim socketFlags as integer = fcntl(this._socket, F_GETFL, 0)
131+
fcntl(this._socket, F_SETFL, socketFlags or O_NONBLOCK)
132+
#endif
129133
if (connect( this._socket, this._addressInfo->ai_addr, this._addressInfo->ai_addrlen) <> 0) then
130134
dim timeout as timeval
131135
dim as fd_set fdset
132136
timeout.tv_sec = timeoutValue
133137
timeout.tv_usec = 0
134138
FD_ZERO(@fdset)
135139
FD_SET_(this._socket, @fdset)
136-
dim foo as integer = select_(this._socket+1, 0, @fdset, 0, @timeout)
137-
if (foo = 1) then
140+
if ( select_(this._socket+1, 0, @fdset, 0, @timeout) = 1) then
138141
dim as integer socketError
139-
dim as socklen_t length = sizeof(so_error)
140-
141-
getsockopt(this._socket, SOL_SOCKET, SO_ERROR, @socketError, @length)
142+
143+
#ifdef __FB_WIN32__
144+
socketError = WSAGetLastError()
145+
#else
146+
dim as socklen_t length = sizeof(so_error)
147+
getsockopt(this._socket, SOL_SOCKET, SO_ERROR, @socketError, @length)
148+
#endif
142149
if (socketError) then
143150
this.errorHandler(net_undefined)
144151
return false
145152
end if
146-
fcntl(this._socket, F_SETFL, socketFlags AND NOT O_NONBLOCK)
153+
#ifdef __FB_WIN32__
154+
blocking = 0 : ioctlsocket(this._socket, FIONBIO, @blocking)
155+
#else
156+
fcntl(this._socket, F_SETFL, socketFlags or O_NONBLOCK)
157+
#endif
147158
else
148159
this.errorHandler(net_timeout)
149160
return false
@@ -160,12 +171,17 @@ function fbNetworkClient.open(address as string, _port as uinteger, timeoutValue
160171
do
161172
messageLength = recv( this._socket, recvBuffer, fbNetwork.RECVBUFFLEN, 0 )
162173
if( messageLength <= 0 ) then
174+
closesocket(this._socket)
175+
freeaddrinfo(this._addressInfo)
176+
this._socket = 0
177+
this._addressInfo = 0
163178
this.close()
164179
return true
165180
end if
166181
recvbuffer[messageLength] = 0
167182
this.onMessage(recvbuffer)
168183
loop
184+
169185

170186
return true
171187
end function

src/fbNetworkClient.bi

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
#ifdef __FB_WIN32__
88
#include once "win/winsock2.bi"
9+
10+
#include once "win/ws2tcpip.bi"
11+
#ifndef inet_ntop
12+
extern "C"
13+
' For some reason, inet_ntop from the ws2tcpip.bi isn't defined on my machine.
14+
declare function inet_ntop stdcall alias "inet_ntop"(byval Family as INT_, byval pAddr as PVOID, byval pStringBuf as LPSTR, byval StringBufSize as uinteger) as LPCSTR
15+
end extern
16+
#endif
17+
WSAStartup( MAKEWORD( 2, 0 ), new WSAData )
918
#else
1019
#include once "crt/netdb.bi"
1120
#include once "crt/fcntl.bi"
@@ -16,6 +25,8 @@
1625
#include once "crt/sys/select.bi"
1726
#endif
1827

28+
29+
1930
declare function resolveHost( byref hostname as string, port as uinteger ) as addrinfo ptr
2031
declare function getIpAdress(addressInfo as addrInfo ptr) as string
2132

@@ -59,7 +70,7 @@ type fbNetworkClient extends object
5970
declare constructor()
6071
declare destructor()
6172

62-
declare function open(address as string, port as uinteger, timeout as clong = 60, protocol as TransportProtocol = TCP) as boolean
73+
declare function open(address as string, _port as uinteger, timeoutValue as integer = 60, _protocol as TransportProtocol = TCP ) as boolean
6374
declare sub close()
6475
declare function sendData(byref _data as string) as boolean
6576

0 commit comments

Comments
 (0)