ecere/drivers/Android: Tweaks to run with latest Android SDK/NDK on Nexus 9
[sdk] / ecere / src / net / Service.ec
index b84ca1f..3b5af5a 100644 (file)
@@ -1,15 +1,23 @@
+#ifdef __statement
+#undef __statement
+#endif
+#define __statement(x) __extension__(x)  // To compile for Android/X86 (Need to add support to eC)
+                                         // Also had to add __extension__ to __swap16md macro in endian.h
 namespace net;
 
 #ifndef ECERE_NONET
 
 #if defined(__WIN32__)
-
+#define SOCKLEN_TYPE int
 #define WIN32_LEAN_AND_MEAN
+#define String _String
 #include <winsock.h>
-static WSADATA wsaData;
+#undef String
 
 #elif defined(__unix__) || defined(__APPLE__)
 default:
+#define SOCKLEN_TYPE socklen_t
+#define set _set
 #define uint _uint
 #include <sys/time.h>
 #include <unistd.h>
@@ -21,6 +29,7 @@ default:
 #include <sys/types.h>
 #include <sys/time.h>
 #include <arpa/inet.h>
+#undef set
 #undef uint
 private:
 
@@ -35,6 +44,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
+
 public class Service
 {
 public:
@@ -44,6 +61,8 @@ public:
 
    virtual void OnAccept();
 
+   s = -1;
+
    // --- Services ---
 
    bool Start()
@@ -51,6 +70,9 @@ public:
    #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
       SOCKET s;
 
+      if(this.s != -1)
+         return false; // Already started
+
    #ifdef DEBUG_SOCKETS
       Log("[P] [NStartService]\n");
    #endif
@@ -73,7 +95,6 @@ public:
          {
             if(!listen(s,5))
             {
-               // Fix up the links/offsets here...
                network.mutex.Wait();
                network.services.Add(this);
                this.s = s;
@@ -81,13 +102,14 @@ public:
                destroyed = false;
                sockets.Clear();
 
-               sockets.offset = (uint)&((Socket)0).prev;
+               // Fix up the links/offsets here...
+               sockets.offset = (uint)(uintptr)&((Socket)0).prev;
 
                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();
@@ -151,7 +173,7 @@ public:
          //FD_SET(s, &ws);
          FD_SET(s, &es);
 
-         selectResult = select(s+1, &rs, &ws, &es, &tvTO);
+         selectResult = select((int)(s+1), &rs, &ws, &es, &tvTO);
          if(selectResult > 0)
          {
             if(FD_ISSET(s, &rs))
@@ -162,7 +184,7 @@ public:
                {
                   SOCKET s;
                   SOCKADDR_IN a;
-                  int addrLen = sizeof(a);
+                  SOCKLEN_TYPE addrLen = sizeof(a);
                   s = accept(this.s,(SOCKADDR *)&a,&addrLen);
                   closesocket(s);
                }