1 #include "sharedmemory.h"
\r
2 #include "sharedmemory_global.h"
\r
4 void Sharedmemory_init(void)
\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
13 sprintf( shm_file_name, "umurmurd:%i", bindport );
\r
15 Log_info("SHM_FD: %s", shm_file_name );
\r
17 shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0666 );
\r
20 fprintf(stderr, "Open failed:%s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working
\r
24 if( ftruncate( shm_fd, shmptr_size ) == -1 )
\r
26 fprintf(stderr, "ftruncate : %s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working
\r
30 shmptr = mmap(0, shmptr_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
\r
31 if (shmptr == (void *) -1)
\r
33 fprintf(stderr, "mmap failed : %s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working
\r
38 memset( shmptr, 0, shmptr_size );
\r
40 shmptr->umurmurd_pid = getpid();
\r
41 shmptr->server_max_clients = server_max_clients;
\r
44 void Sharedmemory_update(void)
\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
55 memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients );
\r
56 shmptr->clientcount = Client_count();
\r
58 if( shmptr->clientcount )
\r
61 while( Client_iterate(&client_itr) != NULL )
\r
63 if( client_itr->authenticated )
\r
65 channel_t *channel = client_itr->channel;
\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
73 shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;
\r
74 shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;
\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
84 void Sharedmemory_alivetick(void)
\r
89 void Sharedmemory_deinit(void)
\r
92 unlink( shm_file_name );
\r
93 shmptr->umurmurd_pid = 0;
\r