Create a new client bound to 'fd'. This is called when a new client * connects. As a side effect updates the global Chat state. */
| 75 | /* Create a new client bound to 'fd'. This is called when a new client |
| 76 | * connects. As a side effect updates the global Chat state. */ |
| 77 | struct client *createClient(int fd) { |
| 78 | char nick[32]; // Used to create an initial nick for the user. |
| 79 | int nicklen = snprintf(nick,sizeof(nick),"user:%d",fd); |
| 80 | struct client *c = chatMalloc(sizeof(*c)); |
| 81 | socketSetNonBlockNoDelay(fd); // Pretend this will not fail. |
| 82 | c->fd = fd; |
| 83 | c->nick = chatMalloc(nicklen+1); |
| 84 | memcpy(c->nick,nick,nicklen); |
| 85 | assert(Chat->clients[c->fd] == NULL); // This should be available. |
| 86 | Chat->clients[c->fd] = c; |
| 87 | /* We need to update the max client set if needed. */ |
| 88 | if (c->fd > Chat->maxclient) Chat->maxclient = c->fd; |
| 89 | Chat->numclients++; |
| 90 | return c; |
| 91 | } |
| 92 | |
| 93 | /* Free a client, associated resources, and unbind it from the global |
| 94 | * state in Chat. */ |
no test coverage detected