MCPcopy Index your code
hub / github.com/antirez/smallchat / createClient

Function createClient

smallchat-server.c:77–91  ·  view source on GitHub ↗

Create a new client bound to 'fd'. This is called when a new client * connects. As a side effect updates the global Chat state. */

Source from the content-addressed store, hash-verified

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. */
77struct 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. */

Callers 1

mainFunction · 0.85

Calls 2

chatMallocFunction · 0.85
socketSetNonBlockNoDelayFunction · 0.85

Tested by

no test coverage detected