rename player -> user
[umurmur.git] / src / client.c
index a1f957d9e4234922b008b639550e6cf4f79717ad..baf6247d6ad1f644c16bf9e16fa59cc0e76b9650 100644 (file)
@@ -51,7 +51,6 @@ void Client_free(client_t *client);
 
 declare_list(clients);
 static int clientcount; /* = 0 */
-static int session = 1;
 static int maxBandwidth;
 
 int iCodecAlpha, iCodecBeta;
@@ -92,7 +91,7 @@ void Client_janitor()
        list_iterate(itr, &clients) {
                client_t *c;
                c = list_get_entry(itr, client_t, node);
-               Log_debug("Client %s BW available %d", c->playerName, c->availableBandwidth);
+               Log_debug("Client %s BW available %d", c->username, c->availableBandwidth);
                c->availableBandwidth += maxBandwidth;
                if (c->availableBandwidth > bwTop)
                        c->availableBandwidth = bwTop;
@@ -162,6 +161,23 @@ void recheckCodecVersions()
        
 }
 
+static int findFreeSessionId()
+{
+       int id;
+       client_t *itr = NULL;
+
+       for (id = 1; id < INT_MAX; id++) {
+               itr = NULL;
+               while ((itr = Client_iterate(&itr)) != NULL) {
+                       if (itr->sessionId == id)
+                               break;
+               }
+               if (itr == NULL) /* Found free id */
+                       return id;
+       }
+       return -1;
+}
+
 int Client_add(int fd, struct sockaddr_in *remote)
 {
        client_t *newclient;
@@ -183,7 +199,9 @@ int Client_add(int fd, struct sockaddr_in *remote)
        }
        newclient->availableBandwidth = maxBandwidth;
        Timer_init(&newclient->lastActivity);
-       newclient->sessionId = session++; /* XXX - more elaborate? */
+       newclient->sessionId = findFreeSessionId();
+       if (newclient->sessionId < 0)
+               Log_fatal("Could not find a free session ID");
        
        init_list_entry(&newclient->txMsgQueue);
        init_list_entry(&newclient->chan_node);
@@ -212,7 +230,7 @@ void Client_free(client_t *client)
 
        if (client->authenticated) {
                int leave_id;
-               leave_id = Chan_playerLeave(client);
+               leave_id = Chan_userLeave(client);
                if (leave_id > 0) { /* Remove temp channel */
                        sendmsg = Msg_create(ChannelRemove);
                        sendmsg->payload.channelRemove->channel_id = leave_id;
@@ -237,8 +255,8 @@ void Client_free(client_t *client)
                free(client->release);
        if (client->os)
                free(client->os);                       
-       if (client->playerName)
-               free(client->playerName);
+       if (client->username)
+               free(client->username);
        if (client->context)
                free(client->context);
        free(client);
@@ -483,7 +501,7 @@ int Client_send_message_except(client_t *client, message_t *msg)
                if (itr != client) {
                        if (count++ > 0)
                                Msg_inc_ref(msg); /* One extra reference for each new copy */
-                       Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->playerName, msg->refcount);
+                       Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
                        Client_send_message(itr, msg);
                }
        }
@@ -608,7 +626,9 @@ out:
 static inline void Client_send_voice(client_t *src, client_t *dst, uint8_t *data, int len, int poslen)
 {
        if (IS_AUTH(dst) && dst != src && !dst->deaf) {
-               if (poslen > 0 && strcmp(src->context, dst->context) == 0)
+               if (poslen > 0 && /* Has positional data */
+                       src->context != NULL && dst->context != NULL && /* ...both source and destination has context */
+                       strcmp(src->context, dst->context) == 0) /* ...and the contexts match */
                        Client_send_udp(dst, data, len);
                else
                        Client_send_udp(dst, data, len - poslen);