Switch over to more modern unix sharedmemory API (requested by fatbob)
[umurmur.git] / src / sharedmemory.c
1 #include "sharedmemory.h"\r
2 #include "sharedmemory_global.h"\r
3 \r
4 void Sharedmemory_init(void) \r
5 {\r
6 \r
7   int bindport = getIntConf(BINDPORT);                                                          //MJP BUG commandline option for address and port dont work this way going to have \r
8   int server_max_clients = getIntConf(MAX_CLIENTS);   //to bring them across as prameters to Sharedmemory_init(void)\r
9   int shmptr_size =  sizeof( shm_t  ) + (sizeof( shmclient_t ) * server_max_clients);\r
10   \r
11 \r
12 \r
13         sprintf( shm_file_name, "umurmurd:%i", bindport );\r
14 \r
15   Log_info("SHM_FD: %s", shm_file_name  );\r
16 \r
17 shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0666 );\r
18 if(shm_fd == -1)\r
19 {\r
20     fprintf(stderr, "Open failed:%s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working\r
21     exit(1);\r
22 }  \r
23 \r
24 if( ftruncate( shm_fd, shmptr_size ) == -1 )\r
25 {\r
26     fprintf(stderr, "ftruncate : %s\n", strerror(errno));  //MJP BUG make this Log_ calls once I get this working\r
27     exit(1);\r
28 }\r
29 \r
30   shmptr = mmap(0, shmptr_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);\r
31   if (shmptr == (void *) -1) \r
32         {\r
33      fprintf(stderr, "mmap failed : %s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working\r
34      exit(1);\r
35   } \r
36 \r
37 \r
38   memset( shmptr, 0, shmptr_size );\r
39                                              \r
40   shmptr->umurmurd_pid = getpid();\r
41   shmptr->server_max_clients = server_max_clients;  \r
42 }\r
43 \r
44 void Sharedmemory_update(void) \r
45 {\r
46 \r
47   uint64_t now;\r
48   unsigned int cc = 0;\r
49   client_t *client_itr = NULL;\r
50   static size_t bt_end = sizeof(  bool_t  ) * 8,   //compute once\r
51                 pa_end = sizeof(  float   ) * 4,\r
52                 pc_end = sizeof( uint32_t ) * 2;\r
53                 \r
54  \r
55     memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients );\r
56     shmptr->clientcount = Client_count();\r
57     \r
58       if( shmptr->clientcount )\r
59       {\r
60         Timer_init( &now );\r
61           while( Client_iterate(&client_itr) != NULL )\r
62           {                                                                                   \r
63             if( client_itr->authenticated )\r
64             {        \r
65               channel_t *channel = client_itr->channel;\r
66         \r
67                 strncpy( shmptr->client[cc].username, client_itr->username, 120 );\r
68                 strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ), 45 );\r
69                 shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );\r
70                 shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );\r
71                 strncpy( shmptr->client[cc].channel, channel->name, 120 );\r
72         \r
73                 shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;\r
74                 shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;\r
75         \r
76                 memcpy( &shmptr->client[cc].bUDP, &client_itr->bUDP, bt_end );\r
77                 memcpy( &shmptr->client[cc].UDPPingAvg, &client_itr->UDPPingAvg, pa_end );\r
78                 memcpy( &shmptr->client[cc].UDPPackets, &client_itr->UDPPackets, pc_end );\r
79             }  \r
80           cc++;     \r
81         }\r
82       } \r
83 }\r
84 void Sharedmemory_alivetick(void)\r
85 {\r
86  shmptr->alive++;\r
87 }\r
88 \r
89 void Sharedmemory_deinit(void) \r
90 {\r
91   close( shm_fd );\r
92   unlink( shm_file_name );\r
93   shmptr->umurmurd_pid = 0;\r
94 }