X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fsharedmemory.c;h=6af3f752fe3c057c0e074d0261c739be0b97ee99;hb=a738ff892738e239ad74f5aa686108298efa7cea;hp=4ede9d39258491db223ab73b16cf699a05f48279;hpb=2311400db89f8dd24cfc57d6f72328f539a9ce8d;p=umurmur.git diff --git a/src/sharedmemory.c b/src/sharedmemory.c index 4ede9d3..6af3f75 100644 --- a/src/sharedmemory.c +++ b/src/sharedmemory.c @@ -1,23 +1,42 @@ #include "sharedmemory.h" +#include "sharedmemory_global.h" void Sharedmemory_init(void) { - key_t key = 0x53021d79; - int server_max_clients = getIntConf(MAX_CLIENTS); + int bindport = getIntConf(BINDPORT); //MJP BUG commandline option for address and port dont work this way going to have + int server_max_clients = getIntConf(MAX_CLIENTS); //to bring them across as prameters to Sharedmemory_init(void) int shmptr_size = sizeof( shm_t ) + (sizeof( shmclient_t ) * server_max_clients); - //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? - shmid = shmget( key, shmptr_size, IPC_CREAT | 0666 ); - Log_info("SHM_KEY: 0x%x", key ); - if( ( shmptr = ( shm_t *) shmat( shmid, 0, 0 ) ) == (shm_t *) (-1) ) - { - perror("shmat"); - exit(1); //MJP BUG should report error and just not use shm dont exit - } - memset( shmptr, 0, shmptr_size ); + sprintf( shm_file_name, "umurmurd:%i", bindport ); + + Log_info("SHM_FD: %s", shm_file_name ); + +shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0666 ); +if(shm_fd == -1) +{ + fprintf(stderr, "Open failed:%s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working + exit(1); +} + +if( ftruncate( shm_fd, shmptr_size ) == -1 ) +{ + fprintf(stderr, "ftruncate : %s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working + exit(1); +} + + shmptr = mmap(0, shmptr_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (shmptr == (void *) -1) + { + fprintf(stderr, "mmap failed : %s\n", strerror(errno)); //MJP BUG make this Log_ calls once I get this working + exit(1); + } + + + memset( shmptr, 0, shmptr_size ); + shmptr->umurmurd_pid = getpid(); shmptr->server_max_clients = server_max_clients; } @@ -25,47 +44,51 @@ void Sharedmemory_init(void) void Sharedmemory_update(void) { - static size_t bt_end = sizeof( bool_t ) * 8, //compute once - pa_end = sizeof( float ) * 4, - pc_end = sizeof( uint32_t ) * 2; + uint64_t now; + unsigned int cc = 0; + client_t *client_itr = NULL; + static size_t bt_end = sizeof( bool_t ) * 8, //compute once + pa_end = sizeof( float ) * 4, + pc_end = sizeof( uint32_t ) * 2; - unsigned int cc = 0; - client_t *client_itr = NULL; - uint64_t now; - memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients ); - shmptr->clientcount = Client_count(); - - if( shmptr->clientcount ) - { - Timer_init( &now ); - while( Client_iterate(&client_itr) != NULL ) { - - if( client_itr->authenticated ) - { - channel_t *channel = client_itr->channel; + memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients ); + shmptr->clientcount = Client_count(); + + if( shmptr->clientcount ) + { + Timer_init( &now ); + while( Client_iterate(&client_itr) != NULL ) + { + if( client_itr->authenticated ) + { + channel_t *channel = client_itr->channel; - strncpy( shmptr->client[cc].username, client_itr->username, 120 ); - strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ), 45 ); - shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr ); - shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr ); - strncpy( shmptr->client[cc].channel, channel->name, 120 ); + strncpy( shmptr->client[cc].username, client_itr->username, 120 ); + strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ), 45 ); + shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr ); + shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr ); + strncpy( shmptr->client[cc].channel, channel->name, 120 ); - shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL; - shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL; + shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL; + shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL; - memcpy( &shmptr->client[cc].bUDP, &client_itr->bUDP, bt_end ); - memcpy( &shmptr->client[cc].UDPPingAvg, &client_itr->UDPPingAvg, pa_end ); - memcpy( &shmptr->client[cc].UDPPackets, &client_itr->UDPPackets, pc_end ); - } - cc++; - } - -} + memcpy( &shmptr->client[cc].bUDP, &client_itr->bUDP, bt_end ); + memcpy( &shmptr->client[cc].UDPPingAvg, &client_itr->UDPPingAvg, pa_end ); + memcpy( &shmptr->client[cc].UDPPackets, &client_itr->UDPPackets, pc_end ); + } + cc++; + } + } +} +void Sharedmemory_alivetick(void) +{ + shmptr->alive++; } void Sharedmemory_deinit(void) { - shmctl( shmid, IPC_RMID, 0 ); //Mark shmid for removal. - shmdt( shmptr ); + close( shm_fd ); + unlink( shm_file_name ); + shmptr->umurmurd_pid = 0; } \ No newline at end of file