#FEATURE by Stefan Reise
support non-blocing for windows / web socket
class: Socket
class definition
added: #setNonBlocking
comment/format in: #primNonBlockingNextPutAll:
changed: #nonBlockingNextPutAll:
--- a/Socket.st Thu Feb 06 11:11:10 2020 +0100
+++ b/Socket.st Thu Feb 06 13:21:52 2020 +0100
@@ -4378,14 +4378,17 @@
!Socket methodsFor:'support websocket'!
nonBlockingNextPutAll:someBytes
- "should be replaced in the calles by #nextPutAll:
- but currently #nextPutAll: handles WSAEWOULDBLOCK / EWOULDBLOCK incorrect (at least for windows)
+ "using #primNonBlockingNextPutAll:
+ because #nextPutAll: handles WSAEWOULDBLOCK / EWOULDBLOCK incorrect (at least for windows)
only called by WebSocketStream #criticalSocketNextPutAll:"
|bytes countRemainingBytesToWrite result|
OperatingSystem isMSWINDOWSlike ifFalse:[
- self error:'this method is for windows os only'.
+ "this method supports non blocking socket for windows,
+ for other os use the default behavior"
+ self nextPutAll:someBytes.
+ ^ self
].
bytes := someBytes asByteArray.
@@ -4406,12 +4409,12 @@
].
"Created: / 30-01-2020 / 16:35:08 / Stefan Reise"
- "Modified (comment): / 05-02-2020 / 16:08:47 / Stefan Reise"
+ "Modified (format): / 06-02-2020 / 13:18:59 / Stefan Reise"
!
primNonBlockingNextPutAll:someBytes
- "should be replaced in the calles by #nextPutAll:
- but currently #nextPutAll: handles WSAEWOULDBLOCK / EWOULDBLOCK incorrect (at least for windows)
+ "using #primNonBlockingNextPutAll:
+ because #nextPutAll: handles WSAEWOULDBLOCK / EWOULDBLOCK incorrect (at least for windows)
only called by WebSocketStream #criticalSocketNextPutAll:"
|bytes byteLength returnValue|
@@ -4459,7 +4462,39 @@
"Created: / 30-01-2020 / 16:36:01 / Stefan Reise"
"Modified: / 31-01-2020 / 11:53:01 / Stefan Reise"
- "Modified (comment): / 05-02-2020 / 16:08:50 / Stefan Reise"
+ "Modified (comment): / 06-02-2020 / 13:11:17 / Stefan Reise"
+!
+
+setNonBlocking
+ "using #setNonBlocking
+ because implementing and using Win32OperatingSystem #setBlocking:fd:
+ would reset to blocking (see senders of #blocking:)
+ and we need the socket to be non-blocking forever
+ only called by WebSocketStream #criticalSocketNextPutAll:"
+
+ OperatingSystem isMSWINDOWSlike ifFalse:[
+ "/ this method is for windows os only
+ ^ self
+ ].
+
+ %{
+# ifdef __win32__
+ int result;
+ u_long nonBlocking = 1;
+ SOCKET socket = SOCKET_FROM_FILE_OBJECT(__INST(handle));
+
+ result = ioctlsocket(socket, FIONBIO, &nonBlocking);
+ if (result == SOCKET_ERROR) {
+ console_fprintf(stderr, "Win32OS [info]: ioctlsocket failed with %d\n", WSAGetLastError());
+ }
+
+ RETURN(true);
+#endif // __win32__
+ %}.
+
+ self primitiveFailed.
+
+ "Created: / 06-02-2020 / 13:09:42 / Stefan Reise"
! !
!Socket methodsFor:'waiting'!