ecere/drivers/Android: Tweaks to run with latest Android SDK/NDK on Nexus 9
[sdk] / ecere / src / net / Socket.ec
index e4e5b1d..56dd0fa 100644 (file)
@@ -8,11 +8,12 @@ namespace net;
 #define String _String
 #include <winsock.h>
 #undef String
-static WSADATA wsaData;
+#define SOCKLEN_TYPE int
 
 #elif defined(__unix__) || defined(__APPLE__)
 
 default:
+#define SOCKLEN_TYPE socklen_t
 #define set _set
 #define uint _uint
 #include <sys/time.h>
@@ -40,6 +41,14 @@ typedef struct in_addr IN_ADDR;
 
 import "network"
 
+#ifdef __ANDROID__
+// TOFIX:
+#undef ntohs
+#undef htons
+#define ntohs(x) (x)
+#define htons(x) (x)
+#endif
+
 #define GETLEDWORD(b) (uint32)(((b)[3] << 24) | ((b)[2] << 16) | ((b)[1] << 8) | (b)[0])
 
 #define PUTLEDWORD(b, d) \
@@ -58,9 +67,9 @@ public:
 };
 
 static class SocketConnectThread : Thread
-{  
+{
    Socket socket;
-   
+
    uint Main()
    {
       bool result = false;
@@ -78,7 +87,7 @@ 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((int)socket.s))
@@ -107,7 +116,7 @@ static class SocketConnectThread : Thread
    #endif
       if(result && !socket.destroyed)
          socket._connected = 1;
-      else
+      else if(socket._connected == -2)
          socket._connected = -1;
 
    #ifdef DEBUG_SOCKETS
@@ -133,8 +142,8 @@ public:
          {
             SOCKET s;
             SOCKADDR_IN a;
-            int addrLen = sizeof(a);
-            
+            SOCKLEN_TYPE addrLen = sizeof(a);
+
             value.accepted = true;
             s = accept(value.s,(SOCKADDR *)&a, &addrLen);
             if(s != -1)
@@ -154,7 +163,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;
@@ -164,7 +173,7 @@ public:
                network.mutex.Wait();
                FD_SET(s, &network.exceptSet);
                FD_SET(s, &network.readSet);
-               if(s >= network.ns) 
+               if(s >= network.ns)
                {
                   network.ns = (int)(s+1);
                   network.socketsSemaphore.Release();
@@ -180,7 +189,7 @@ public:
       get { return this ? service : null; }
    };
 
-   property char * inetAddress { get { return (char *)inetAddress; } };
+   property const char * inetAddress { get { return (char *)inetAddress; } };
    property int inetPort { get { return inetPort; } }
    property Socket next { get { return next; } };
    property bool connected { get { return _connected == 1 || _connected == -2; } };
@@ -210,7 +219,7 @@ public:
             OnReceivePacket(packet);
             delete tempBuffer;
             return 0;
-         }                   
+         }
       }
       return 0;
    }
@@ -218,7 +227,7 @@ public:
    virtual void OnDisconnect(int code);
    virtual void OnReceivePacket(Packet packet);
 
-   bool Connect(char * address, int port)
+   bool Connect(const char * address, int port)
    {
       bool result = false;
    #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
@@ -251,11 +260,20 @@ public:
 
          if(!disconnected)
          {
+            if(_connected == -2 && connectThread)
+            {
+               incref this;
+               network.mutex.Release();
+               connectThread.Wait();
+               delete connectThread;
+               network.mutex.Wait();
+               _refCount--;
+            }
             disconnected = true;
             if(!service)
             {
                if(_connected)
-                  ((_connected == -1) ? network.connectSockets : network.sockets).Remove(this);
+                  ((_connected == -1 || _connected == -2) ? network.connectSockets : network.sockets).Remove(this);
             }
             else
             {
@@ -270,7 +288,7 @@ public:
 
          if(s == network.ns - 1)
             Network_DetermineMaxSocket();
-      
+
          if(s != -1)
          {
             FD_CLR(s, &network.readSet);
@@ -295,7 +313,7 @@ public:
    }
 
    // --- Transfer ---
-   bool Send(void * buffer, int size)
+   bool Send(const void * buffer, int size)
    {
    #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
       if(this)
@@ -305,7 +323,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();
@@ -319,7 +337,7 @@ public:
    #endif
             {
                //Print("~");
-            }               
+            }
 
             // This is what was making eCom jam...
             // select(s+1, null, &ws, &es, null);
@@ -344,12 +362,12 @@ public:
       return false;
    }
 
-   bool SendString(char * string)
+   bool SendString(const char * string)
    {
       return Send(string, (int)strlen(string));
    }
 
-   bool Sendf(char * format, ...)
+   bool Sendf(const char * format, ...)
    {
       bool result;
       va_list args;
@@ -362,7 +380,7 @@ public:
       return result;
    }
 
-   bool DatagramConnect(char * sendAddress, int port)
+   bool DatagramConnect(const char * sendAddress, int port)
    {
       SOCKET s = socket(AF_INET,SOCK_DGRAM,0);
       if(s != -1)
@@ -399,7 +417,7 @@ 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 = (int)(s+1);
                network.socketsSemaphore.Release();
@@ -414,11 +432,11 @@ public:
 
    virtual int ReceiveData(byte * buffer, int count, uint flags)
    {
-      return (int)recv(s, buffer, count, flags);
+      return (int)recv(s, (char *)buffer, count, flags);
    }
-   virtual int SendData(byte * buffer, int count, uint flags)
+   virtual int SendData(const byte * buffer, int count, uint flags)
    {
-      return (int)send(s, buffer, count, flags);
+      return (int)send(s, (const char *)buffer, count, flags);
    }
    virtual bool OnEstablishConnection(int s);
 
@@ -442,9 +460,10 @@ private:
       SOCKET s = this.s;
 
       if(mustLock) network.mutex.Wait();
+
       if(!service && _connected)
       {
-         (_connected == -1 ? network.connectSockets : network.sockets).Remove(this);
+         ((_connected == -1 || _connected == -2) ? network.connectSockets : network.sockets).Remove(this);
          _connected = 0;
       }
 
@@ -465,16 +484,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);
@@ -504,7 +523,7 @@ private:
       network.mutex.Release();
    }
 
-   bool _Connect(SOCKET s, char * address, int port)
+   bool _Connect(SOCKET s, const char * address, int port)
    {
       bool result = false;
       if(this)
@@ -530,7 +549,7 @@ private:
          _connected = -2;
 
          FD_SET(s, &network.writeSet);
-         if(s >= network.ns && !processAlone) 
+         if(s >= network.ns && !processAlone)
          {
             network.ns = (int)(s+1);
             network.socketsSemaphore.Release();
@@ -548,7 +567,7 @@ private:
             if(_connected == -1 || destroyed)
             {
                _connected = 0;
-               
+
                if(s == network.ns - 1)
                   Network_DetermineMaxSocket();
 #if 0
@@ -571,7 +590,7 @@ private:
             }
             else
                this.s = -1;
-            
+
             delete connectThread;
          }
          else
@@ -605,20 +624,20 @@ 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*/)
                count = ReceiveData(recvBuffer + recvBytes, recvBufferSize - recvBytes, 0);
             else
             {
-               int len = sizeof(a);
-               count = recvfrom(s, recvBuffer + recvBytes, 
+               SOCKLEN_TYPE len = sizeof(a);
+               count = (int)recvfrom(s, (char *)recvBuffer + recvBytes,
                   recvBufferSize - recvBytes, 0, (SOCKADDR *)&a, &len);
                strcpy(inetAddress, inet_ntoa(this.a.sin_addr));
                inetPort = ntohs((uint16)a.sin_port);
@@ -705,7 +724,7 @@ private:
 
    public bool Process()
    {
-      ProcessTimeOut(0);
+      return ProcessTimeOut(0);
    }
 
    public bool ProcessTimeOut(Seconds timeOut)
@@ -716,7 +735,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);
@@ -724,6 +743,7 @@ private:
       //FD_SET(s, &ws);
       FD_SET(s, &es);
 
+      incref this;
       selectResult = select((int)(s+1), &rs, &ws, &es, leftOver ? &tv : (timeOut ? &tvTO : null));
       mutex.Wait();
       if(s != -1 && _refCount && (leftOver || selectResult))
@@ -731,6 +751,7 @@ private:
          gotEvent |= ProcessSocket(&rs, &ws, &es);
       }
       mutex.Release();
+      delete this;
       return gotEvent;
    }
 
@@ -747,7 +768,7 @@ private:
    Thread connectThread;
    DisconnectCode disconnectCode;
    bool destroyed;
-   int _connected;
+   int _connected;         // -2: Initial value when calling Connect(), -1: Disconnected or otherwise destroyed while connecting, 1: succesfully connected, 0: no longer in any connect/sockets list
    bool disconnected;
 
    // Receiving Buffer