Socket.st
branchjv
changeset 4037 dea4e4c75fa3
parent 3992 529479ee431f
parent 4032 24df60dcd5f4
child 4040 645d303ede4d
--- a/Socket.st	Sat Aug 13 06:52:20 2016 +0200
+++ b/Socket.st	Tue Aug 16 06:51:56 2016 +0200
@@ -2119,7 +2119,7 @@
     |isAsync err|
 
     handle isNil ifTrue:[
-	^ self errorNotOpen
+        ^ self errorNotOpen
     ].
 
 %{  /* STACK: 100000 */
@@ -2139,25 +2139,25 @@
     int sockaddr_size;
 
     if (!__isNonNilObject(aSocketAddress) || !__isBytes(aSocketAddress)) {
-	DBGPRINTF(("SOCKET: invalid socketAddress\n"));
-	err = @symbol(argumentError);
-	goto out;
+        DBGPRINTF(("SOCKET: invalid socketAddress\n"));
+        err = @symbol(argumentError);
+        goto out;
     }
 
     {
-	int sockAddrOffs = 0;
-	int nIndex =__byteArraySize(aSocketAddress);
-	OBJ cls = __qClass(aSocketAddress);
-
-	//if (cls != @global(ByteArray))
-	//    sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	sockaddr_size = nIndex - sockAddrOffs;
-	if (sockaddr_size > sizeof(sa)) {
-	    DBGPRINTF(("SOCKET: invalid (short) socketAddress\n"));
-	    err = @symbol(argumentError);
-	    goto out;
-	}
-	memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size);
+        int sockAddrOffs = 0;
+        int nIndex =__byteArraySize(aSocketAddress);
+        OBJ cls = __qClass(aSocketAddress);
+
+        //if (cls != @global(ByteArray))
+        //    sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        sockaddr_size = nIndex - sockAddrOffs;
+        if (sockaddr_size > sizeof(sa)) {
+            DBGPRINTF(("SOCKET: invalid (short) socketAddress\n"));
+            err = @symbol(argumentError);
+            goto out;
+        }
+        memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size);
     }
 
     sock = SOCKET_FROM_FILE_OBJECT(fp);
@@ -2184,18 +2184,18 @@
 
 
     do {
-	DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock));
-	ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size);
-	DBGFPRINTF((stderr, "SOCKET: connect(%d) -> %"_ld_" (%d)\n", sock, (INT)ret, __threadErrno));
+        DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock));
+        ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size);
+        DBGFPRINTF((stderr, "SOCKET: connect(%d) -> %"_ld_" (%d)\n", sock, (INT)ret, __threadErrno));
     } while ((ret < 0) && (__threadErrno == EINTR));
 
     if (ret < 0) {
-	int optLen = sizeof(errno);
-	errno = __threadErrno;
+        int optLen = sizeof(errno);
+        errno = __threadErrno;
 #if 0
-	if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) {
-	    DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError()));
-	}
+        if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) {
+            DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError()));
+        }
 #endif // 0
     }
 
@@ -2206,16 +2206,16 @@
     __BEGIN_INTERRUPTABLE__
 #  endif
     do {
-	ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size);
+        ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size);
     } while ((ret < 0)
 #  ifdef __win32__
-	     && (errno = WSAGetLastError())
+             && (errno = WSAGetLastError())
 #  endif
-	     && ((errno == EINTR)
+             && ((errno == EINTR)
 #  ifdef EAGAIN
-		 || (errno == EAGAIN)
+                 || (errno == EAGAIN)
 #  endif
-		));
+                ));
 #  if !defined(__win32__) && !defined(O_NONBLOCK)
     __END_INTERRUPTABLE__
 #  endif
@@ -2228,47 +2228,47 @@
 
     if (ret < 0) {
 # if defined(EINPROGRESS) || defined(EALREADY)
-	if (0
+        if (0
 #  ifdef __win32__
-	    || (errno == WSAEWOULDBLOCK)
+            || (errno == WSAEWOULDBLOCK)
 #  endif
 #  ifdef EINPROGRESS
-	    || (errno == EINPROGRESS)
+            || (errno == EINPROGRESS)
 #  endif
 #  ifdef EALREADY
-	    || (errno == EALREADY)
+            || (errno == EALREADY)
 #  endif
-	) {
-	    /*
-	     * This was a nonblocking operation that will take some time.
-	     * Do a select on read to get informed when the operation is ready.
-	     */
-	    DBGFPRINTF((stderr, "SOCKET: isAsync is true\n"));
-	    isAsync = true;
-	} else
+        ) {
+            /*
+             * This was a nonblocking operation that will take some time.
+             * Do a select on read to get informed when the operation is ready.
+             */
+            DBGFPRINTF((stderr, "SOCKET: isAsync is true\n"));
+            isAsync = true;
+        } else
 # endif /* EINPROGRESS or EALREADY */
-	{
-	    DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n",
-			(INT)ret, errno, __threadErrno ));
+        {
+            DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n",
+                        (INT)ret, errno, __threadErrno ));
 # ifdef DUMP_ADDRESS
-	    {
-		unsigned char *cp = (unsigned char *)(&sa);
-		int i;
-
-		console_printf("address data:\n");
-		for (i=0; i<sockaddr_size; i++) {
-		    console_printf(" %02x\n", *cp++);
-		}
-	    }
+            {
+                unsigned char *cp = (unsigned char *)(&sa);
+                int i;
+
+                console_printf("address data:\n");
+                for (i=0; i<sockaddr_size; i++) {
+                    console_printf(" %02x\n", *cp++);
+                }
+            }
 # endif
-	    err = __MKSMALLINT(errno);
-	}
+            err = __MKSMALLINT(errno);
+        }
     }
 
 # ifdef __win32__
     {
-	int off = 0;
-	ioctlsocket(sock, FIONBIO, &off);
+        int off = 0;
+        ioctlsocket(sock, FIONBIO, &off);
     }
 # elif defined(O_NONBLOCK) // Linux / Unix
     fcntl(sock, F_SETFL, oldFlags);
@@ -2281,34 +2281,34 @@
 %}.
 
     err notNil ifTrue:[
-	err isSymbol ifTrue:[
-	    self primitiveFailed:err.
-	].
-	lastErrorNumber := err.
-	^ false.
-	"/ Once we will raise an exception instead of returning false (and have to change some code above):
+        err isSymbol ifTrue:[
+            self primitiveFailed:err.
+        ].
+        lastErrorNumber := err.
+        ^ false.
+        "/ Once we will raise an exception instead of returning false (and have to change some code above):
 "/        (OperatingSystem errorHolderForNumber:err) reportError.
     ].
     isAsync == true ifTrue:[
-	(self writeExceptionWaitWithTimeoutMs:timeout) ifTrue:[
-	    "/ a timeout occured
-	    "/ should I cancel the connect?
-	    lastErrorNumber := OperatingSystem errorNumberFor:#ETIMEDOUT.
-	    ^ false.
-	].
-	err := self getSocketError.
-	err ~~ 0 ifTrue:[
-	    lastErrorNumber := err.
-	    ^ false.
-	].
+        (self writeExceptionWaitWithTimeoutMs:timeout) ifTrue:[
+            "/ a timeout occurred
+            "/ should I cancel the connect?
+            lastErrorNumber := OperatingSystem errorNumberFor:#ETIMEDOUT.
+            ^ false.
+        ].
+        err := self getSocketError.
+        err ~~ 0 ifTrue:[
+            lastErrorNumber := err.
+            ^ false.
+        ].
     ].
 
 
     peer := aSocketAddress.
     port isNil ifTrue:[
-	"socket has not been explicitly bound,
-	 after connect it has been bound implicitly - fetch the port"
-	port := self getFullSocketAddress port.
+        "socket has not been explicitly bound,
+         after connect it has been bound implicitly - fetch the port"
+        port := self getFullSocketAddress port.
     ].
     ^ true
 
@@ -4113,7 +4113,7 @@
      For a new connection, an accept is performed and the new socket is returned.
      For an old connection, that socket is returned.
      In any case, the caller gets a socket to operate on as return value,
-     or nil, if a timeout occured.
+     or nil, if a timeout occurred.
      This method implements the inner wait-primitive of a multi-connection
      server application."
 
@@ -4122,41 +4122,41 @@
 
     "first, a quick check if data is already available"
     self canReadWithoutBlocking ifTrue:[
-	^ self accept.
+        ^ self accept.
     ].
     otherConnections do:[:aConnection |
-	aConnection canReadWithoutBlocking ifTrue:[
-	    ^ aConnection
-	]
+        aConnection canReadWithoutBlocking ifTrue:[
+            ^ aConnection
+        ]
     ].
 
     "check again - prevent incoming interrupts from disturbing our setup"
     wasBlocked := OperatingSystem blockInterrupts.
     [
-	sema := Semaphore new name:'multiReadWait'.
-	otherConnections do:[:aConnection |
-	    Processor signal:sema onInput:(aConnection fileDescriptor).
-	].
-	Processor signal:sema onInput:(self fileDescriptor).
-	timeoutSeconds notNil ifTrue:[
-	    Processor signal:sema afterSeconds:timeoutSeconds
-	].
-	Processor activeProcess state:#ioWait.
-	sema wait.
+        sema := Semaphore new name:'multiReadWait'.
+        otherConnections do:[:aConnection |
+            Processor signal:sema onInput:(aConnection fileDescriptor).
+        ].
+        Processor signal:sema onInput:(self fileDescriptor).
+        timeoutSeconds notNil ifTrue:[
+            Processor signal:sema afterSeconds:timeoutSeconds
+        ].
+        Processor activeProcess state:#ioWait.
+        sema wait.
     ] ifCurtailed:[
-	sema notNil ifTrue:[Processor disableSemaphore:sema].
-	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+        sema notNil ifTrue:[Processor disableSemaphore:sema].
+        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
     ].
     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
 
     "see who it was ..."
     self canReadWithoutBlocking ifTrue:[
-	^ self accept.
+        ^ self accept.
     ].
     otherConnections do:[:aConnection |
-	aConnection canReadWithoutBlocking ifTrue:[
-	    ^ aConnection
-	]
+        aConnection canReadWithoutBlocking ifTrue:[
+            ^ aConnection
+        ]
     ].
 
     "none - a timeout"
@@ -4167,20 +4167,20 @@
     "suspend the current process, until a new connection comes
      in at the receiver or a timeout occurs.
      For a new connection, an accept is performed and the new socket is returned.
-     Returns nil, if a timeout occured.
+     Returns nil, if a timeout occurred.
      This method implements the inner wait-primitive of a single-connection
      server application."
 
     |newSock|
 
     (self readWaitWithTimeout:timeoutSecondsOrNil) ifTrue:[
-	"a timeout occurred - no connection within timeout"
-	^ nil
+        "a timeout occurred - no connection within timeout"
+        ^ nil
     ].
     newSock := self class new.
     (newSock primAcceptOn:self blocking:false) ifFalse:[
-	"should raise an error here"
-	^ nil
+        "should raise an error here"
+        ^ nil
     ].
     ^ newSock
 ! !