Switch over to more modern unix sharedmemory API (requested by fatbob)
[umurmur.git] / src / sharedmemory.c
index 4ede9d39258491db223ab73b16cf699a05f48279..6af3f752fe3c057c0e074d0261c739be0b97ee99 100644 (file)
@@ -1,23 +1,42 @@
 #include "sharedmemory.h"\r
+#include "sharedmemory_global.h"\r
 \r
 void Sharedmemory_init(void) \r
 {\r
 \r
-  key_t key = 0x53021d79;\r
-  int server_max_clients = getIntConf(MAX_CLIENTS);  \r
+  int bindport = getIntConf(BINDPORT);                                                         //MJP BUG commandline option for address and port dont work this way going to have \r
+  int server_max_clients = getIntConf(MAX_CLIENTS);   //to bring them across as prameters to Sharedmemory_init(void)\r
   int shmptr_size =  sizeof( shm_t  ) + (sizeof( shmclient_t ) * server_max_clients);\r
   \r
-  //key =  ftok( ".", 'S' );  //MJP BUG this needs fixing.. also how to handle multi copys of umurmur bound to diff ports or IPs  option to pass key on cmdline?  \r
-  shmid = shmget( key, shmptr_size, IPC_CREAT | 0666 );    \r
 \r
-  Log_info("SHM_KEY: 0x%x", key );\r
 \r
-        if( ( shmptr = ( shm_t *) shmat( shmid, 0, 0 ) ) == (shm_t *) (-1) )\r
-        {\r
-                perror("shmat");\r
-                exit(1); //MJP BUG should report error and just not use shm dont exit\r
-        }\r
-        memset( shmptr, 0, shmptr_size );                                     \r
+       sprintf( shm_file_name, "umurmurd:%i", bindport );\r
+\r
+  Log_info("SHM_FD: %s", shm_file_name  );\r
+\r
+shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0666 );\r
+if(shm_fd == -1)\r
+{\r
+    fprintf(stderr, "Open failed:%s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working\r
+    exit(1);\r
+}  \r
+\r
+if( ftruncate( shm_fd, shmptr_size ) == -1 )\r
+{\r
+    fprintf(stderr, "ftruncate : %s\n", strerror(errno));  //MJP BUG make this Log_ calls once I get this working\r
+    exit(1);\r
+}\r
+\r
+  shmptr = mmap(0, shmptr_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);\r
+  if (shmptr == (void *) -1) \r
+       {\r
+     fprintf(stderr, "mmap failed : %s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working\r
+     exit(1);\r
+  } \r
+\r
+\r
+  memset( shmptr, 0, shmptr_size );\r
+                                            \r
   shmptr->umurmurd_pid = getpid();\r
   shmptr->server_max_clients = server_max_clients;  \r
 }\r
@@ -25,47 +44,51 @@ void Sharedmemory_init(void)
 void Sharedmemory_update(void) \r
 {\r
 \r
- static size_t bt_end = sizeof(  bool_t  ) * 8,   //compute once\r
-               pa_end = sizeof(  float   ) * 4,\r
-               pc_end = sizeof( uint32_t ) * 2;\r
+  uint64_t now;\r
+  unsigned int cc = 0;\r
+  client_t *client_itr = NULL;\r
+  static size_t bt_end = sizeof(  bool_t  ) * 8,   //compute once\r
+                pa_end = sizeof(  float   ) * 4,\r
+                pc_end = sizeof( uint32_t ) * 2;\r
                 \r
- unsigned int cc = 0;\r
- client_t *client_itr = NULL;\r
- uint64_t now;\r
  \r
- memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients );\r
- shmptr->clientcount = Client_count();\r
\r
- if( shmptr->clientcount )\r
- {\r
-   Timer_init( &now );\r
- while( Client_iterate(&client_itr) != NULL ) {\r
-                                                                                               \r
-      if( client_itr->authenticated )\r
-      {        \r
-        channel_t *channel = client_itr->channel;\r
   memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients );\r
   shmptr->clientcount = Client_count();\r
+    \r
     if( shmptr->clientcount )\r
     {\r
+        Timer_init( &now );\r
+          while( Client_iterate(&client_itr) != NULL )\r
+          {                                                                                   \r
+            if( client_itr->authenticated )\r
+            {        \r
+              channel_t *channel = client_itr->channel;\r
         \r
-        strncpy( shmptr->client[cc].username, client_itr->username, 120 );\r
-        strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ), 45 );\r
-        shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );\r
-        shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );\r
-        strncpy( shmptr->client[cc].channel, channel->name, 120 );\r
+                strncpy( shmptr->client[cc].username, client_itr->username, 120 );\r
+                strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ), 45 );\r
+                shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );\r
+                shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );\r
+                strncpy( shmptr->client[cc].channel, channel->name, 120 );\r
         \r
-        shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;\r
-        shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;\r
+                shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;\r
+                shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;\r
         \r
-        memcpy( &shmptr->client[cc].bUDP, &client_itr->bUDP, bt_end );\r
-        memcpy( &shmptr->client[cc].UDPPingAvg, &client_itr->UDPPingAvg, pa_end );\r
-        memcpy( &shmptr->client[cc].UDPPackets, &client_itr->UDPPackets, pc_end );\r
-      }  \r
-      cc++;     \r
- }\r
-\r
-} \r
+                memcpy( &shmptr->client[cc].bUDP, &client_itr->bUDP, bt_end );\r
+                memcpy( &shmptr->client[cc].UDPPingAvg, &client_itr->UDPPingAvg, pa_end );\r
+                memcpy( &shmptr->client[cc].UDPPackets, &client_itr->UDPPackets, pc_end );\r
+            }  \r
+          cc++;     \r
+        }\r
+      } \r
+}\r
+void Sharedmemory_alivetick(void)\r
+{\r
+ shmptr->alive++;\r
 }\r
 \r
 void Sharedmemory_deinit(void) \r
 {\r
-  shmctl( shmid, IPC_RMID, 0 );   //Mark shmid for removal.\r
-  shmdt( shmptr );\r
+  close( shm_fd );\r
+  unlink( shm_file_name );\r
+  shmptr->umurmurd_pid = 0;\r
 }
\ No newline at end of file