cleaned all trailing white space from source files.
[sdk] / ecere / src / net / Socket.ec
index 11c841e..d45dc69 100644 (file)
@@ -5,7 +5,9 @@ namespace net;
 #if defined(__WIN32__)
 
 #define WIN32_LEAN_AND_MEAN
+#define String _String
 #include <winsock.h>
+#undef String
 static WSADATA wsaData;
 
 #elif defined(__unix__) || defined(__APPLE__)
@@ -56,9 +58,9 @@ public:
 };
 
 static class SocketConnectThread : Thread
-{  
+{
    Socket socket;
-   
+
    uint Main()
    {
       bool result = false;
@@ -76,10 +78,10 @@ static class SocketConnectThread : Thread
             {
                network.mutex.Wait();
                strcpy(socket.inetAddress, inet_ntoa(socket.a.sin_addr));
-               socket.inetPort = ntohs(socket.a.sin_port); 
+               socket.inetPort = ntohs(socket.a.sin_port);
                network.mutex.Release();
 
-               if(socket.OnEstablishConnection(socket.s))
+               if(socket.OnEstablishConnection((int)socket.s))
                {
                   network.mutex.Wait();
                   result = true;
@@ -132,7 +134,7 @@ public:
             SOCKET s;
             SOCKADDR_IN a;
             int addrLen = sizeof(a);
-            
+
             value.accepted = true;
             s = accept(value.s,(SOCKADDR *)&a, &addrLen);
             if(s != -1)
@@ -152,7 +154,7 @@ public:
                address = null;
                this.a = a;
                strcpy(inetAddress, inet_ntoa(this.a.sin_addr));
-               inetPort = ntohs(a.sin_port); 
+               inetPort = ntohs(a.sin_port);
                this.s = s;
                service = value;
                connectThread = null;
@@ -162,9 +164,9 @@ public:
                network.mutex.Wait();
                FD_SET(s, &network.exceptSet);
                FD_SET(s, &network.readSet);
-               if(s >= network.ns) 
+               if(s >= network.ns)
                {
-                  network.ns = s+1;
+                  network.ns = (int)(s+1);
                   network.socketsSemaphore.Release();
                }
                network.mutex.Release();
@@ -208,7 +210,7 @@ public:
             OnReceivePacket(packet);
             delete tempBuffer;
             return 0;
-         }                   
+         }
       }
       return 0;
    }
@@ -268,7 +270,7 @@ public:
 
          if(s == network.ns - 1)
             Network_DetermineMaxSocket();
-      
+
          if(s != -1)
          {
             FD_CLR(s, &network.readSet);
@@ -303,7 +305,7 @@ public:
          fd_set ws, es;
 
          if(s != -1 && ((type == tcp && (count = SendData(buffer, size, 0))) ||
-            (type == udp && (count = sendto(s, buffer, size,0, (SOCKADDR *)&a, sizeof(a))))))
+            (type == udp && (count = (int)sendto(s, buffer, size,0, (SOCKADDR *)&a, sizeof(a))))))
          {
    #if defined(__WIN32__)
             int error = WSAGetLastError();
@@ -317,7 +319,7 @@ public:
    #endif
             {
                //Print("~");
-            }               
+            }
 
             // This is what was making eCom jam...
             // select(s+1, null, &ws, &es, null);
@@ -344,7 +346,7 @@ public:
 
    bool SendString(char * string)
    {
-      return Send(string, strlen(string));
+      return Send(string, (int)strlen(string));
    }
 
    bool Sendf(char * format, ...)
@@ -352,10 +354,10 @@ public:
       bool result;
       va_list args;
       char string[MAX_F_STRING];
-      string[sizeof(string)-1] = 0;
       va_start(args, format);
       vsnprintf(string, sizeof(string), format, args);
-      result = Send(string, strlen(string));
+      string[sizeof(string)-1] = 0;
+      result = Send(string, (int)strlen(string));
       va_end(args);
       return result;
    }
@@ -375,7 +377,7 @@ public:
    bool DatagramHost(int port)
    {
       SOCKET s = socket(AF_INET,SOCK_DGRAM,0);
-      if(s != -1)
+      if(s != -1 && !_connected)
       {
          SOCKADDR_IN a;
          bool val = true;
@@ -397,9 +399,9 @@ public:
             FD_CLR(s, &network.writeSet);
             FD_SET(s, &network.readSet);
             FD_SET(s, &network.exceptSet);
-            if(s >= network.ns) 
+            if(s >= network.ns)
             {
-               network.ns = s+1;
+               network.ns = (int)(s+1);
                network.socketsSemaphore.Release();
             }
             network.mutex.Release();
@@ -463,16 +465,16 @@ private:
          service = null;
          _connected = 0;
       }
-      
+
       if(s != -1) { closesocket(s); this.s = -1; }
 
       delete address;
-      delete recvBuffer; 
+      delete recvBuffer;
 
       recvBufferSize = 0;
       recvBytes = 0;
 
-      if(s != -1) 
+      if(s != -1)
       {
          FD_CLR(s, &network.readSet);
          FD_CLR(s, &network.writeSet);
@@ -528,9 +530,9 @@ private:
          _connected = -2;
 
          FD_SET(s, &network.writeSet);
-         if(s >= network.ns && !processAlone) 
+         if(s >= network.ns && !processAlone)
          {
-            network.ns = s+1;
+            network.ns = (int)(s+1);
             network.socketsSemaphore.Release();
          }
          connectThread = SocketConnectThread { socket = this };
@@ -546,7 +548,7 @@ private:
             if(_connected == -1 || destroyed)
             {
                _connected = 0;
-               
+
                if(s == network.ns - 1)
                   Network_DetermineMaxSocket();
 #if 0
@@ -569,7 +571,7 @@ private:
             }
             else
                this.s = -1;
-            
+
             delete connectThread;
          }
          else
@@ -603,12 +605,12 @@ private:
          int count = 0;
 
          result = true;
-         if(recvBufferSize - recvBytes < MAX_RECEIVE)
+         if((int)recvBufferSize - recvBytes < MAX_RECEIVE)
          {
             recvBuffer = renew recvBuffer byte[recvBufferSize + MAX_RECEIVE];
             recvBufferSize += MAX_RECEIVE;
          }
-           
+
          if(FD_ISSET(s, rs) && disconnectCode == (DisconnectCode)-1)
          {
             if(type == tcp /*|| _connected*/)
@@ -616,7 +618,7 @@ private:
             else
             {
                int len = sizeof(a);
-               count = recvfrom(s, recvBuffer + recvBytes, 
+               count = (int)recvfrom(s, recvBuffer + recvBytes,
                   recvBufferSize - recvBytes, 0, (SOCKADDR *)&a, &len);
                strcpy(inetAddress, inet_ntoa(this.a.sin_addr));
                inetPort = ntohs((uint16)a.sin_port);
@@ -703,7 +705,7 @@ private:
 
    public bool Process()
    {
-      ProcessTimeOut(0);
+      return ProcessTimeOut(0);
    }
 
    public bool ProcessTimeOut(Seconds timeOut)
@@ -714,7 +716,7 @@ private:
       fd_set rs, ws, es;
       int selectResult;
 
-      if(disconnectCode > 0) return false;
+      if(disconnectCode > 0 && !leftOver) return false;
       FD_ZERO(&rs);
       FD_ZERO(&ws);
       FD_ZERO(&es);
@@ -722,13 +724,15 @@ private:
       //FD_SET(s, &ws);
       FD_SET(s, &es);
 
-      selectResult = select(s+1, &rs, &ws, &es, leftOver ? &tv : (timeOut ? &tvTO : null));
+      incref this;
+      selectResult = select((int)(s+1), &rs, &ws, &es, leftOver ? &tv : (timeOut ? &tvTO : null));
       mutex.Wait();
       if(s != -1 && _refCount && (leftOver || selectResult))
       {
          gotEvent |= ProcessSocket(&rs, &ws, &es);
       }
       mutex.Release();
+      delete this;
       return gotEvent;
    }