--- deliantra/server/socket/request.C 2006/12/13 18:08:02 1.27 +++ deliantra/server/socket/request.C 2006/12/14 01:21:58 1.33 @@ -104,10 +104,10 @@ int x, y, mx, my; { - char buf[MAXSOCKBUF]; + packet sl; - sprintf (buf, "map_scroll %d %d", dx, dy); - Write_String_To_Socket (ns, buf, strlen (buf)); + sl.printf ("map_scroll %d %d", dx, dy); + ns->send_packet (sl); } /* If we are using the Map1aCmd, we may in fact send @@ -163,7 +163,7 @@ memset (&socket.lastmap, 0, sizeof (socket.lastmap)); if (socket.newmapcmd == 1) - Write_String_To_Socket (&socket, "newmap", 6); + socket.send_packet ("newmap"); socket.update_look = 1; socket.look_position = 0; @@ -204,7 +204,7 @@ else snprintf (buf, MAX_BUF, "mapinfo current"); - Write_String_To_Socket (&socket, buf, strlen (buf)); + socket.send_packet (buf); } } else if (socket.current_x != ob->x || socket.current_y != ob->y) @@ -316,7 +316,7 @@ else snprintf (bigbuf, MAX_BUF, "mapinfo %s unsupported", token); - Write_String_To_Socket (&pl->socket, bigbuf, strlen (bigbuf)); + pl->socket.send_packet (bigbuf); } /** This is the Setup cmd - easy first implementation */ @@ -333,16 +333,19 @@ * The client then must sort this out */ - LOG (llevInfo, "Get SetupCmd:: %s\n", buf); + //LOG (llevInfo, "Get SetupCmd:: %s\n", buf); + strcpy (cmdback, "setup"); - for (s = 0; s < len;) + for (s = 0; s < len; ) { - cmd = &buf[s]; /* find the next space, and put a null there */ - for (; buf[s] && buf[s] != ' '; s++); + for (; buf[s] && buf[s] != ' '; s++) + ; + buf[s++] = 0; + while (buf[s] == ' ') s++; @@ -351,8 +354,11 @@ param = &buf[s]; - for (; buf[s] && buf[s] != ' '; s++); + for (; buf[s] && buf[s] != ' '; s++) + ; + buf[s++] = 0; + while (buf[s] == ' ') s++; @@ -523,8 +529,9 @@ safe_strcat (cmdback, "FALSE", &slen, HUGE_BUF); } } /* for processing all the setup commands */ + LOG (llevInfo, "SendBack SetupCmd:: %s\n", cmdback); - Write_String_To_Socket (ns, cmdback, strlen (cmdback)); + ns->send_packet (cmdback); } /** @@ -540,9 +547,7 @@ oldsettings = settings; if (ns->status != Ns_Add || add_player (ns)) - { - Write_String_To_Socket (ns, "addme_failed", 12); - } + ns->send_packet ("addme_failed"); else { /* Basically, the add_player copies the socket structure into @@ -550,10 +555,11 @@ * is not needed anymore. The write below should still work, as the * stuff in ns is still relevant. */ - Write_String_To_Socket (ns, "addme_success", 13); + ns->send_packet ("addme_success"); socket_info.nconns--; ns->status = Ns_Avail; } + settings = oldsettings; } @@ -607,7 +613,7 @@ strcat (cmdback, "smoothing"); } - Write_String_To_Socket (ns, cmdback, strlen (cmdback)); + ns->send_packet (cmdback); } /* @@ -670,7 +676,7 @@ strcat (cmdback, temp); } - Write_String_To_Socket (ns, cmdback, strlen (cmdback)); + ns->send_packet (cmdback); } /** @@ -684,8 +690,6 @@ SendSmooth (NewSocket * ns, uint16 face) { uint16 smoothface; - unsigned char reply[MAX_BUF]; - SockList sl; /* If we can't find a face, return and set it so we won't try to send this * again. @@ -703,11 +707,11 @@ ns->faces_sent[face] |= NS_FACESENT_SMOOTH; - sl.buf = reply; - strcpy ((char *) sl.buf, "smooth "); - sl.len = strlen ((char *) sl.buf); + packet sl; - sl << uint16 (face) << uint16 (smoothface); + sl << "smooth " + << uint16 (face) + << uint16 (smoothface); Send_With_Handling (ns, &sl); } @@ -725,10 +729,6 @@ SendSmooth (ns, facenbr); } - - - - /** * This handles the general commands from the client (ie, north, fire, cast, * etc.) @@ -791,9 +791,8 @@ NewPlayerCmd (uint8 * buf, int len, player *pl) { int time, repeat; - short packet; - unsigned char command[MAX_BUF]; - SockList sl; + char command[MAX_BUF]; + int pktlen; if (len < 7) { @@ -801,13 +800,13 @@ return; } - packet = GetShort_String (buf); - repeat = GetInt_String (buf + 2); + pktlen = net_uint16 (buf); + repeat = net_uint32 (buf + 2); + /* -1 is special - no repeat, but don't update */ if (repeat != -1) - { - pl->count = repeat; - } + pl->count = repeat; + if ((len - 4) >= MAX_BUF) len = MAX_BUF - 5; @@ -827,9 +826,8 @@ /* This should not happen anymore. */ if (pl->ob->speed_left < -1.0) - { - LOG (llevError, "Player has negative time - shouldn't do command.\n"); - } + LOG (llevError, "Player has negative time - shouldn't do command.\n"); + /* In c_new.c */ execute_newserver_command (pl->ob, (char *) command); /* Perhaps something better should be done with a left over count. @@ -839,18 +837,15 @@ */ pl->count = 0; - /* Send confirmation of command execution now */ - sl.buf = command; - strcpy ((char *) sl.buf, "comc "); - sl.len = 5; - if (FABS (pl->ob->speed) < 0.001) time = MAX_TIME * 100; else time = (int) (MAX_TIME / FABS (pl->ob->speed)); - sl << uint16 (packet) << uint32 (time); + /* Send confirmation of command execution now */ + packet sl; + sl << "comc " << uint16 (pktlen) << uint32 (time); Send_With_Handling (&pl->socket, &sl); } @@ -973,11 +968,11 @@ * requiing this minimal value reduces complexity of that code, and it * has been around for a long time. */ - if (!strcmp (" CF DX CLIENT", cp) || ns->sc_version < 1024) + if (ns->sc_version < 1026) { sprintf (version_warning, "drawinfo %d %s", NDI_RED, "**** VERSION WARNING ****\n**** CLIENT IS TOO OLD!! UPDATE THE CLIENT!! ****"); - Write_String_To_Socket (ns, version_warning, strlen (version_warning)); + ns->send_packet (version_warning); } } @@ -1059,40 +1054,32 @@ char buf[MAX_BUF]; sprintf (buf, "query %d %s", flags, text ? text : ""); - Write_String_To_Socket (ns, buf, strlen (buf)); + ns->send_packet (buf); } #define AddIfInt64(Old,New,Type) if (Old != New) {\ Old = New; \ - SockList_AddChar(&sl, Type); \ - SockList_AddInt64(&sl, New); \ + sl << uint8 (Type) << uint64 (New); \ } #define AddIfInt(Old,New,Type) if (Old != New) {\ Old = New; \ - SockList_AddChar(&sl, Type); \ - SockList_AddInt(&sl, New); \ + sl << uint8 (Type) << uint32 (New); \ } #define AddIfShort(Old,New,Type) if (Old != New) {\ Old = New; \ - SockList_AddChar(&sl, Type); \ - SockList_AddShort(&sl, New); \ + sl << uint8 (Type) << uint16 (New); \ } #define AddIfFloat(Old,New,Type) if (Old != New) {\ Old = New; \ - SockList_AddChar(&sl, Type); \ - SockList_AddInt(&sl,(long)(New*FLOAT_MULTI));\ + sl << uint8 (Type) << uint32 (New*FLOAT_MULTI); \ } #define AddIfString(Old,New,Type) if (Old == NULL || strcmp(Old,New)) {\ - if (Old) free(Old);\ - Old = strdup_local(New);\ - SockList_AddChar(&sl, Type); \ - SockList_AddChar(&sl, ( char )strlen(New)); \ - strcpy((char*)sl.buf + sl.len, New); \ - sl.len += strlen(New); \ + free(Old); Old = strdup (New);\ + sl << uint8 (Type) << data8 (New); \ } /** @@ -1104,13 +1091,11 @@ void esrv_update_stats (player *pl) { - SockList sl; char buf[MAX_BUF]; uint16 flags; - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "stats "); - sl.len = strlen ((char *) sl.buf); + packet sl; + sl << "stats "; if (pl->ob != NULL) { @@ -1137,13 +1122,13 @@ { if (pl->last_skill_ob[s] && pl->last_skill_exp[s] != pl->last_skill_ob[s]->stats.exp) { - /* Always send along the level if exp changes. This is only * 1 extra byte, but keeps processing simpler. */ - SockList_AddChar (&sl, (char) (s + CS_STAT_SKILLINFO)); - SockList_AddChar (&sl, (char) pl->last_skill_ob[s]->level); - SockList_AddInt64 (&sl, pl->last_skill_ob[s]->stats.exp); + sl << uint8 (s + CS_STAT_SKILLINFO) + << uint8 (pl->last_skill_ob[s]->level) + << uint64 (pl->last_skill_ob[s]->stats.exp); + pl->last_skill_exp[s] = pl->last_skill_ob[s]->stats.exp; } } @@ -1209,7 +1194,6 @@ Send_With_Handling (&pl->socket, &sl); } - free (sl.buf); } /** @@ -1218,24 +1202,17 @@ void esrv_new_player (player *pl, uint32 weight) { - SockList sl; - pl->last_weight = weight; - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); + packet sl; + sl << "player "; - strcpy ((char *) sl.buf, "player "); - sl.len = strlen ((char *) sl.buf); - SockList_AddInt (&sl, pl->ob->count); - SockList_AddInt (&sl, weight); - SockList_AddInt (&sl, pl->ob->face->number); - - SockList_AddChar (&sl, (char) strlen (pl->ob->name)); - strcpy ((char *) sl.buf + sl.len, pl->ob->name); - sl.len += strlen (pl->ob->name); + sl << uint32 (pl->ob->count) + << uint32 (weight) + << uint32 (pl->ob->face->number) + << data8 (pl->ob->name); Send_With_Handling (&pl->socket, &sl); - free (sl.buf); SET_FLAG (pl->ob, FLAG_CLIENT_SENT); } @@ -1249,7 +1226,6 @@ void esrv_send_animation (NewSocket * ns, short anim_num) { - SockList sl; int i; /* Do some checking on the anim_num we got. Note that the animations @@ -1262,11 +1238,12 @@ return; } - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "anim "); - sl.len = 5; - SockList_AddShort (&sl, anim_num); - SockList_AddShort (&sl, 0); /* flags - not used right now */ + packet sl; + + sl << "anim " + << uint16 (anim_num) + << uint16 (0); /* flags - not used right now */ + /* Build up the list of faces. Also, send any information (ie, the * the face itself) down to the client. */ @@ -1274,10 +1251,11 @@ { if (!(ns->faces_sent[animations[anim_num].faces[i]] & NS_FACESENT_FACE)) esrv_send_face (ns, animations[anim_num].faces[i], 0); - SockList_AddShort (&sl, animations[anim_num].faces[i]); /* flags - not used right now */ + sl << uint16 (animations[anim_num].faces[i]); /* flags - not used right now */ } + Send_With_Handling (ns, &sl); - free (sl.buf); + ns->anims_sent[anim_num] = 1; } @@ -1301,8 +1279,10 @@ return; abort (); } + newmap->cells[x][y].faces[newmap->cells[x][y].count] = face_num; newmap->cells[x][y].count++; + if (!(ns->faces_sent[face_num] & NS_FACESENT_FACE)) esrv_send_face (ns, face_num, 0); } @@ -1419,12 +1399,9 @@ { static long frames, bytes, tbytes, tframes; char *cur; - SockList sl; - - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "map "); - sl.len = strlen ((char *) sl.buf); + packet sl; + sl << "map "; cur = (char *) compactlayer (ns, (unsigned char *) sl.buf + sl.len, MAP_LAYERS, newmap); sl.len = cur - (char *) sl.buf; @@ -1446,7 +1423,6 @@ ns->sent_scroll = 0; } - free (sl.buf); } @@ -1498,7 +1474,7 @@ * sent, it returns zero. */ static int -check_head (SockList & sl, NewSocket & ns, int ax, int ay, int layer) +check_head (packet &sl, NewSocket &ns, int ax, int ay, int layer) { short face_num; @@ -1509,9 +1485,10 @@ if (face_num != ns.lastmap.cells[ax][ay].faces[layer]) { - SockList_AddShort (&sl, face_num); + sl << uint16 (face_num); if (face_num && !(ns.faces_sent[face_num] & NS_FACESENT_FACE)) esrv_send_face (&ns, face_num, 0); + heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS + layer] = NULL; ns.lastmap.cells[ax][ay].faces[layer] = face_num; return 1; @@ -1541,7 +1518,7 @@ */ static int -update_space (SockList * sl, NewSocket * ns, maptile *mp, int mx, int my, int sx, int sy, int layer) +update_space (packet &sl, NewSocket &ns, maptile *mp, int mx, int my, int sx, int sy, int layer) { object *ob, *head; uint16 face_num; @@ -1550,7 +1527,6 @@ /* If there is a multipart object stored away, treat that as more important. * If not, then do the normal processing. */ - head = heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + layer]; /* Check to see if this head is part of the set of objects @@ -1725,22 +1701,22 @@ * we already sent for a lower layer. In that case, don't send * this one. */ - if (face_num && layer + 1 < MAP_LAYERS && ns->lastmap.cells[sx][sy].faces[layer + 1] == face_num) - { - face_num = 0; - } + if (face_num && layer + 1 < MAP_LAYERS && ns.lastmap.cells[sx][sy].faces[layer + 1] == face_num) + face_num = 0; /* We've gotten what face we want to use for the object. Now see if * if it has changed since we last sent it to the client. */ - if (ns->lastmap.cells[sx][sy].faces[layer] != face_num) + if (ns.lastmap.cells[sx][sy].faces[layer] != face_num) { - ns->lastmap.cells[sx][sy].faces[layer] = face_num; - if (!(ns->faces_sent[face_num] & NS_FACESENT_FACE)) - esrv_send_face (ns, face_num, 0); - SockList_AddShort (sl, face_num); + ns.lastmap.cells[sx][sy].faces[layer] = face_num; + if (!(ns.faces_sent[face_num] & NS_FACESENT_FACE)) + esrv_send_face (&ns, face_num, 0); + + sl << uint16 (face_num); return 1; } + /* Nothing changed */ return 0; } @@ -1765,7 +1741,7 @@ */ static inline int -update_smooth (SockList * sl, NewSocket * ns, maptile *mp, int mx, int my, int sx, int sy, int layer) +update_smooth (packet &sl, NewSocket &ns, maptile *mp, int mx, int my, int sx, int sy, int layer) { object *ob; int smoothlevel; /* old face_num; */ @@ -1780,8 +1756,8 @@ else { smoothlevel = ob->smoothlevel; - if (smoothlevel && !(ns->faces_sent[ob->face->number] & NS_FACESENT_SMOOTH)) - SendSmooth (ns, ob->face->number); + if (smoothlevel && !(ns.faces_sent[ob->face->number] & NS_FACESENT_SMOOTH)) + SendSmooth (&ns, ob->face->number); } /* else not already head object or blank face */ /* We've gotten what face we want to use for the object. Now see if @@ -1791,12 +1767,14 @@ smoothlevel = 255; else if (smoothlevel < 0) smoothlevel = 0; - if (ns->lastmap.cells[sx][sy].smooth[layer] != smoothlevel) + + if (ns.lastmap.cells[sx][sy].smooth[layer] != smoothlevel) { - ns->lastmap.cells[sx][sy].smooth[layer] = smoothlevel; - SockList_AddChar (sl, (uint8) (smoothlevel & 0xFF)); + ns.lastmap.cells[sx][sy].smooth[layer] = smoothlevel; + sl << uint8 (smoothlevel); return 1; } + /* Nothing changed */ return 0; } @@ -1847,32 +1825,25 @@ int x, y, ax, ay, d, startlen, max_x, max_y, oldlen; sint16 nx, ny; int estartlen, eoldlen; - SockList sl; - SockList esl; /*For extended Map info */ uint16 mask, emask; uint8 eentrysize; uint16 ewhatstart, ewhatflag; uint8 extendedinfos; maptile *m; - NewSocket & socket = pl->contr->socket; + NewSocket &socket = pl->contr->socket; check_map_change (pl->contr); - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - if (socket.mapmode == Map1Cmd) - strcpy ((char *) sl.buf, "map1 "); - else - strcpy ((char *) sl.buf, "map1a "); - sl.len = strlen ((char *) sl.buf); + packet sl; + packet esl; + + sl << (socket.mapmode == Map1Cmd ? "map1 " : "map1a "); startlen = sl.len; /*Extendedmapinfo structure initialisation */ if (socket.ext_mapinfos) { - esl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) esl.buf, "mapextended "); - esl.len = strlen ((char *) esl.buf); extendedinfos = EMI_NOREDRAW; if (socket.EMI_smooth) @@ -1881,9 +1852,10 @@ ewhatstart = esl.len; ewhatflag = extendedinfos; /*The EMI_NOREDRAW bit could need to be taken away */ - SockList_AddChar (&esl, extendedinfos); eentrysize = getExtendedMapInfoSize (&socket); - SockList_AddChar (&esl, eentrysize); + esl << "mapextended " + << uint8 (extendedinfos) + << uint8 (eentrysize); estartlen = esl.len; } else @@ -1934,14 +1906,11 @@ oldlen = sl.len; - SockList_AddShort (&sl, mask); + sl << uint16 (mask); - if (check_head (sl, socket, ax, ay, 2)) - mask |= 0x4; - if (check_head (sl, socket, ax, ay, 1)) - mask |= 0x2; - if (check_head (sl, socket, ax, ay, 0)) - mask |= 0x1; + if (check_head (sl, socket, ax, ay, 2)) mask |= 0x4; + if (check_head (sl, socket, ax, ay, 1)) mask |= 0x2; + if (check_head (sl, socket, ax, ay, 0)) mask |= 0x1; /* If all we are doing is sending 0 (blank) faces, we don't * actually need to send that - just the coordinates @@ -1988,7 +1957,7 @@ */ if (lastcell.count != -1) { - SockList_AddShort (&sl, mask); + sl << uint16 (mask); map_clearcell (&lastcell, 0, 0, 0, -1); } } @@ -2002,7 +1971,7 @@ oldlen = sl.len; - SockList_AddShort (&sl, mask); + sl << uint16 (mask); if (lastcell.count != -1) need_send = 1; @@ -2013,12 +1982,9 @@ { /* Now check to see if any heads need to be sent */ - if (check_head (sl, socket, ax, ay, 2)) - mask |= 0x4; - if (check_head (sl, socket, ax, ay, 1)) - mask |= 0x2; - if (check_head (sl, socket, ax, ay, 0)) - mask |= 0x1; + if (check_head (sl, socket, ax, ay, 2)) mask |= 0x4; + if (check_head (sl, socket, ax, ay, 1)) mask |= 0x2; + if (check_head (sl, socket, ax, ay, 0)) mask |= 0x1; lastcell.count = count; } @@ -2057,10 +2023,10 @@ mask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4; eoldlen = esl.len; emask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4; - SockList_AddShort (&sl, mask); + sl << uint16 (mask); if (socket.ext_mapinfos) - SockList_AddShort (&esl, emask); + esl << uint16 (emask); unsigned char dummy; unsigned char *last_ext = &dummy; @@ -2074,10 +2040,10 @@ { *last_ext |= 0x80; last_ext = sl.buf + sl.len; - SockList_AddChar (&sl, d); + sl << uint8 (d); } else - SockList_AddChar (&sl, 255 - 64 * d); + sl << uint8 (255 - 64 * d); } lastcell.count = d; @@ -2154,19 +2120,19 @@ } /* Floor face */ - if (update_space (&sl, &socket, m, nx, ny, ax, ay, 2)) + if (update_space (sl, socket, m, nx, ny, ax, ay, 2)) mask |= 0x4; if (socket.EMI_smooth) - if (update_smooth (&esl, &socket, m, nx, ny, ax, ay, 2)) + if (update_smooth (esl, socket, m, nx, ny, ax, ay, 2)) emask |= 0x4; /* Middle face */ - if (update_space (&sl, &socket, m, nx, ny, ax, ay, 1)) + if (update_space (sl, socket, m, nx, ny, ax, ay, 1)) mask |= 0x2; if (socket.EMI_smooth) - if (update_smooth (&esl, &socket, m, nx, ny, ax, ay, 1)) + if (update_smooth (esl, socket, m, nx, ny, ax, ay, 1)) emask |= 0x2; if (nx == pl->x && ny == pl->y && pl->invisible & (pl->invisible < 50 ? 4 : 1)) @@ -2185,11 +2151,11 @@ else { /* Top face */ - if (update_space (&sl, &socket, m, nx, ny, ax, ay, 0)) + if (update_space (sl, socket, m, nx, ny, ax, ay, 0)) mask |= 0x1; if (socket.EMI_smooth) - if (update_smooth (&esl, &socket, m, nx, ny, ax, ay, 0)) + if (update_smooth (esl, socket, m, nx, ny, ax, ay, 0)) emask |= 0x1; } @@ -2225,8 +2191,6 @@ if (esl.len > estartlen) Send_With_Handling (&socket, &esl); - - free (esl.buf); } if (sl.len > startlen || socket.sent_scroll) @@ -2234,8 +2198,6 @@ Send_With_Handling (&socket, &sl); socket.sent_scroll = 0; } - - free (sl.buf); } /** @@ -2359,6 +2321,7 @@ } /* Is a valid space */ } } + esrv_map_doneredraw (&pl->contr->socket, &newmap); check_map_change (pl->contr); @@ -2377,7 +2340,7 @@ void send_plugin_custom_message (object *pl, char *buf) { - cs_write_string (&pl->contr->socket, buf, strlen (buf)); + pl->contr->socket.send_packet (buf); } /** @@ -2385,25 +2348,21 @@ * the params - we always send the same info no matter what. */ void -send_skill_info (NewSocket * ns, char *params) +send_skill_info (NewSocket *ns, char *params) { - SockList sl; - int i; + packet sl; + sl << "replyinfo skill_info\n"; + + for (int i = 1; i < NUM_SKILLS; i++) + sl.printf ("%d:%s\n", i + CS_STAT_SKILLINFO, &skill_names[i]); - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "replyinfo skill_info\n"); - for (i = 1; i < NUM_SKILLS; i++) - { - sprintf ((char *) sl.buf + strlen ((char *) sl.buf), "%d:%s\n", i + CS_STAT_SKILLINFO, &skill_names[i]); - } - sl.len = strlen ((char *) sl.buf); if (sl.len >= MAXSOCKBUF) { LOG (llevError, "Buffer overflow in send_skill_info!\n"); fatal (0); } + Send_With_Handling (ns, &sl); - free (sl.buf); } /** @@ -2413,21 +2372,20 @@ void send_spell_paths (NewSocket * ns, char *params) { - SockList sl; - int i; + packet sl; + + sl << "replyinfo spell_paths\n"; + + for (int i = 0; i < NRSPELLPATHS; i++) + sl.printf ("%d:%s\n", 1 << i, spellpathnames[i]); - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "replyinfo spell_paths\n"); - for (i = 0; i < NRSPELLPATHS; i++) - sprintf ((char *) sl.buf + strlen ((char *) sl.buf), "%d:%s\n", 1 << i, spellpathnames[i]); - sl.len = strlen ((char *) sl.buf); if (sl.len >= MAXSOCKBUF) { LOG (llevError, "Buffer overflow in send_spell_paths!\n"); fatal (0); } + Send_With_Handling (ns, &sl); - free (sl.buf); } /** @@ -2437,47 +2395,47 @@ void esrv_update_spells (player *pl) { - SockList sl; - int flags = 0; - object *spell; - if (!pl->socket.monitor_spells) return; - for (spell = pl->ob->inv; spell != NULL; spell = spell->below) + + for (object *spell = pl->ob->inv; spell; spell = spell->below) { if (spell->type == SPELL) { + int flags = 0; + /* check if we need to update it */ if (spell->last_sp != SP_level_spellpoint_cost (pl->ob, spell, SPELL_MANA)) { spell->last_sp = SP_level_spellpoint_cost (pl->ob, spell, SPELL_MANA); flags |= UPD_SP_MANA; } + if (spell->last_grace != SP_level_spellpoint_cost (pl->ob, spell, SPELL_GRACE)) { spell->last_grace = SP_level_spellpoint_cost (pl->ob, spell, SPELL_GRACE); flags |= UPD_SP_GRACE; } + if (spell->last_eat != spell->stats.dam + SP_level_dam_adjust (pl->ob, spell)) { spell->last_eat = spell->stats.dam + SP_level_dam_adjust (pl->ob, spell); flags |= UPD_SP_DAMAGE; } - if (flags != 0) + + if (flags) { - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "updspell "); - sl.len = strlen ((char *) sl.buf); + packet sl; - sl << uint8 (flags) << uint32 (spell->count); + sl << "updspell " + << uint8 (flags) + << uint32 (spell->count); if (flags & UPD_SP_MANA ) sl << uint16 (spell->last_sp); if (flags & UPD_SP_GRACE ) sl << uint16 (spell->last_grace); if (flags & UPD_SP_DAMAGE) sl << uint16 (spell->last_eat); - flags = 0; Send_With_Handling (&pl->socket, &sl); - free (sl.buf); } } } @@ -2486,28 +2444,26 @@ void esrv_remove_spell (player *pl, object *spell) { - SockList sl; - if (!pl->socket.monitor_spells) return; + if (!pl || !spell || spell->env != pl->ob) { LOG (llevError, "Invalid call to esrv_remove_spell"); return; } - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "delspell "); - sl.len = strlen ((char *) sl.buf); - sl << uint32 (spell->count); + packet sl; + + sl << "delspell " + << uint32 (spell->count); Send_With_Handling (&pl->socket, &sl); - free (sl.buf); } /* appends the spell *spell to the Socklist we will send the data to. */ static void -append_spell (player *pl, SockList &sl, object *spell) +append_spell (player *pl, packet &sl, object *spell) { int len, i, skill = 0; @@ -2554,18 +2510,19 @@ void esrv_add_spells (player *pl, object *spell) { - SockList sl; - if (!pl) { LOG (llevError, "esrv_add_spells, tried to add a spell to a NULL player"); return; } + if (!pl->socket.monitor_spells) return; - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); - strcpy ((char *) sl.buf, "addspell "); - sl.len = strlen ((char *) sl.buf); + + packet sl; + + sl << "addspell "; + if (!spell) { for (spell = pl->ob->inv; spell != NULL; spell = spell->below) @@ -2604,7 +2561,7 @@ else append_spell (pl, sl, spell); - if (sl.len >= MAXSOCKBUF) + if (sl.length () >= MAXSOCKBUF) { LOG (llevError, "Buffer overflow in esrv_add_spells!\n"); fatal (0); @@ -2612,5 +2569,6 @@ /* finally, we can send the packet */ Send_With_Handling (&pl->socket, &sl); - free (sl.buf); + } +