--- deliantra/server/socket/request.C 2006/09/21 14:51:16 1.25 +++ deliantra/server/socket/request.C 2006/12/14 01:12:35 1.32 @@ -59,13 +59,11 @@ /* This block is basically taken from socket.c - I assume if it works there, * it should work here. */ -#ifndef WIN32 /* ---win32 exclude unix headers */ -# include -# include -# include -# include -# include -#endif /* win32 */ +#include +#include +#include +#include +#include #ifdef HAVE_UNISTD_H # include @@ -106,10 +104,11 @@ int x, y, mx, my; { - char buf[MAXSOCKBUF]; + SockList sl (MAXSOCKBUF); - 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); + sl.free (); } /* If we are using the Map1aCmd, we may in fact send @@ -165,7 +164,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; @@ -206,7 +205,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) @@ -318,7 +317,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 */ @@ -335,16 +334,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++; @@ -353,8 +355,11 @@ param = &buf[s]; - for (; buf[s] && buf[s] != ' '; s++); + for (; buf[s] && buf[s] != ' '; s++) + ; + buf[s++] = 0; + while (buf[s] == ' ') s++; @@ -525,8 +530,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); } /** @@ -542,9 +548,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 @@ -552,10 +556,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; } @@ -609,7 +614,7 @@ strcat (cmdback, "smoothing"); } - Write_String_To_Socket (ns, cmdback, strlen (cmdback)); + ns->send_packet (cmdback); } /* @@ -672,7 +677,7 @@ strcat (cmdback, temp); } - Write_String_To_Socket (ns, cmdback, strlen (cmdback)); + ns->send_packet (cmdback); } /** @@ -686,8 +691,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. @@ -705,12 +708,14 @@ ns->faces_sent[face] |= NS_FACESENT_SMOOTH; - sl.buf = reply; - strcpy ((char *) sl.buf, "smooth "); - sl.len = strlen ((char *) sl.buf); - SockList_AddShort (&sl, face); - SockList_AddShort (&sl, smoothface); + SockList sl (MAXSOCKBUF); + + sl << "smooth " + << uint16 (face) + << uint16 (smoothface); + Send_With_Handling (ns, &sl); + sl.free (); } /** @@ -726,10 +731,6 @@ SendSmooth (ns, facenbr); } - - - - /** * This handles the general commands from the client (ie, north, fire, cast, * etc.) @@ -792,9 +793,8 @@ NewPlayerCmd (uint8 * buf, int len, player *pl) { int time, repeat; + char command[MAX_BUF]; short packet; - unsigned char command[MAX_BUF]; - SockList sl; if (len < 7) { @@ -802,13 +802,13 @@ return; } - packet = GetShort_String (buf); - repeat = GetInt_String (buf + 2); + packet = 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; @@ -828,9 +828,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. @@ -840,17 +839,17 @@ */ pl->count = 0; - /* Send confirmation of command execution now */ - sl.buf = command; - strcpy ((char *) sl.buf, "comc "); - sl.len = 5; - SockList_AddShort (&sl, packet); if (FABS (pl->ob->speed) < 0.001) time = MAX_TIME * 100; else time = (int) (MAX_TIME / FABS (pl->ob->speed)); - SockList_AddInt (&sl, time); + + /* Send confirmation of command execution now */ + + SockList sl (MAXSOCKBUF); + sl << "comc " << uint16 (packet) << uint32 (time); Send_With_Handling (&pl->socket, &sl); + sl.free (); } @@ -972,11 +971,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); } } @@ -1058,40 +1057,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); \ } /** @@ -1103,13 +1094,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); + SockList sl (MAXSOCKBUF); + sl << "stats "; if (pl->ob != NULL) { @@ -1136,13 +1125,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; } } @@ -1208,7 +1197,7 @@ Send_With_Handling (&pl->socket, &sl); } - free (sl.buf); + sl.free (); } /** @@ -1217,24 +1206,18 @@ void esrv_new_player (player *pl, uint32 weight) { - SockList sl; - pl->last_weight = weight; - sl.buf = (unsigned char *) malloc (MAXSOCKBUF); + SockList sl (MAXSOCKBUF); + 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); + sl.free (); SET_FLAG (pl->ob, FLAG_CLIENT_SENT); } @@ -1248,7 +1231,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 @@ -1261,11 +1243,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 */ + SockList sl (MAXSOCKBUF); + + 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. */ @@ -1273,10 +1256,12 @@ { 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); + sl.free (); + ns->anims_sent[anim_num] = 1; } @@ -1300,8 +1285,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); } @@ -1418,12 +1405,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); + SockList sl (MAXSOCKBUF); + sl << "map "; cur = (char *) compactlayer (ns, (unsigned char *) sl.buf + sl.len, MAP_LAYERS, newmap); sl.len = cur - (char *) sl.buf; @@ -1445,7 +1429,7 @@ ns->sent_scroll = 0; } - free (sl.buf); + sl.free (); } @@ -1497,7 +1481,7 @@ * sent, it returns zero. */ static int -check_head (SockList & sl, NewSocket & ns, int ax, int ay, int layer) +check_head (SockList &sl, NewSocket &ns, int ax, int ay, int layer) { short face_num; @@ -1508,9 +1492,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; @@ -1540,7 +1525,7 @@ */ static int -update_space (SockList * sl, NewSocket * ns, maptile *mp, int mx, int my, int sx, int sy, int layer) +update_space (SockList &sl, NewSocket &ns, maptile *mp, int mx, int my, int sx, int sy, int layer) { object *ob, *head; uint16 face_num; @@ -1549,7 +1534,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 @@ -1724,22 +1708,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; } @@ -1764,7 +1748,7 @@ */ static inline int -update_smooth (SockList * sl, NewSocket * ns, maptile *mp, int mx, int my, int sx, int sy, int layer) +update_smooth (SockList &sl, NewSocket &ns, maptile *mp, int mx, int my, int sx, int sy, int layer) { object *ob; int smoothlevel; /* old face_num; */ @@ -1779,8 +1763,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 @@ -1790,12 +1774,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; } @@ -1846,32 +1832,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); + SockList sl (MAXSOCKBUF); + SockList esl (MAXSOCKBUF); + + 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) @@ -1880,9 +1859,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 @@ -1933,14 +1913,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 @@ -1987,7 +1964,7 @@ */ if (lastcell.count != -1) { - SockList_AddShort (&sl, mask); + sl << uint16 (mask); map_clearcell (&lastcell, 0, 0, 0, -1); } } @@ -2001,7 +1978,7 @@ oldlen = sl.len; - SockList_AddShort (&sl, mask); + sl << uint16 (mask); if (lastcell.count != -1) need_send = 1; @@ -2012,12 +1989,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; } @@ -2056,10 +2030,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; @@ -2073,10 +2047,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; @@ -2086,7 +2060,7 @@ uint8 stat_hp = 0; uint8 stat_width = 0; uint8 flags = 0; - tag_t player = 0; + UUID player = 0; // send hp information, if applicable if (object *op = GET_MAP_FACE_OBJ (m, nx, ny, 0)) @@ -2117,15 +2091,15 @@ mask |= 0x8; *last_ext |= 0x80; last_ext = sl.buf + sl.len; - SockList_AddChar (&sl, 5); - SockList_AddChar (&sl, stat_hp); + + sl << uint8 (5) << uint8 (stat_hp); if (stat_width > 1) { *last_ext |= 0x80; last_ext = sl.buf + sl.len; - SockList_AddChar (&sl, 6); - SockList_AddChar (&sl, stat_width); + + sl << uint8 (6) << uint8 (stat_width); } } @@ -2136,9 +2110,8 @@ mask |= 0x8; *last_ext |= 0x80; last_ext = sl.buf + sl.len; - SockList_AddChar (&sl, 0x47); - SockList_AddChar (&sl, 4); - SockList_AddInt (&sl, player); + + sl << uint8 (0x47) << uint8 (8) << (uint64)player; } if (lastcell.flags != flags) @@ -2148,25 +2121,25 @@ mask |= 0x8; *last_ext |= 0x80; last_ext = sl.buf + sl.len; - SockList_AddChar (&sl, 8); - SockList_AddChar (&sl, flags); + + sl << uint8 (8) << uint8 (flags); } } /* 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)) @@ -2179,17 +2152,17 @@ if (!(socket.faces_sent[pl->face->number] & NS_FACESENT_FACE)) esrv_send_face (&socket, pl->face->number, 0); - SockList_AddShort (&sl, pl->face->number); + sl << uint16 (pl->face->number); } } 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 +2198,6 @@ if (esl.len > estartlen) Send_With_Handling (&socket, &esl); - - free (esl.buf); } if (sl.len > startlen || socket.sent_scroll) @@ -2235,7 +2206,8 @@ socket.sent_scroll = 0; } - free (sl.buf); + sl.free (); + esl.free (); } /** @@ -2359,6 +2331,7 @@ } /* Is a valid space */ } } + esrv_map_doneredraw (&pl->contr->socket, &newmap); check_map_change (pl->contr); @@ -2377,7 +2350,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 +2358,22 @@ * 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; + SockList sl (MAXSOCKBUF); + 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); + sl.free (); } /** @@ -2413,21 +2383,21 @@ void send_spell_paths (NewSocket * ns, char *params) { - SockList sl; - int i; + SockList sl (MAXSOCKBUF); + + 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); + sl.free (); } /** @@ -2437,48 +2407,48 @@ 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); - SockList_AddChar (&sl, flags); - SockList_AddInt (&sl, spell->count); - if (flags & UPD_SP_MANA) - SockList_AddShort (&sl, spell->last_sp); - if (flags & UPD_SP_GRACE) - SockList_AddShort (&sl, spell->last_grace); - if (flags & UPD_SP_DAMAGE) - SockList_AddShort (&sl, spell->last_eat); - flags = 0; + SockList sl (MAXSOCKBUF); + + 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); + Send_With_Handling (&pl->socket, &sl); - free (sl.buf); + sl.free (); } } } @@ -2487,26 +2457,27 @@ 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); - SockList_AddInt (&sl, spell->count); + + SockList sl (MAXSOCKBUF); + + sl << "delspell " + << uint32 (spell->count); + Send_With_Handling (&pl->socket, &sl); - free (sl.buf); + sl.free (); } /* 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, SockList &sl, object *spell) { int len, i, skill = 0; @@ -2515,17 +2486,11 @@ LOG (llevError, "item number %d is a spell with no name.\n", spell->count); return; } - SockList_AddInt (sl, spell->count); - SockList_AddShort (sl, spell->level); - SockList_AddShort (sl, spell->casting_time); + /* store costs and damage in the object struct, to compare to later */ - spell->last_sp = SP_level_spellpoint_cost (pl->ob, spell, SPELL_MANA); + spell->last_sp = SP_level_spellpoint_cost (pl->ob, spell, SPELL_MANA); spell->last_grace = SP_level_spellpoint_cost (pl->ob, spell, SPELL_GRACE); - spell->last_eat = spell->stats.dam + SP_level_dam_adjust (pl->ob, spell); - /* send the current values */ - SockList_AddShort (sl, spell->last_sp); - SockList_AddShort (sl, spell->last_grace); - SockList_AddShort (sl, spell->last_eat); + spell->last_eat = spell->stats.dam + SP_level_dam_adjust (pl->ob, spell); /* figure out which skill it uses, if it uses one */ if (spell->skill) @@ -2537,27 +2502,19 @@ break; } } - SockList_AddChar (sl, skill); - - SockList_AddInt (sl, spell->path_attuned); - SockList_AddInt (sl, (spell->face) ? spell->face->number : 0); - len = strlen (spell->name); - SockList_AddChar (sl, (char) len); - memcpy (sl->buf + sl->len, spell->name, len); - sl->len += len; - - if (!spell->msg) - { - SockList_AddShort (sl, 0); - } - else - { - len = strlen (spell->msg); - SockList_AddShort (sl, len); - memcpy (sl->buf + sl->len, spell->msg, len); - sl->len += len; - } + /* send the current values */ + sl << uint32 (spell->count) + << uint16 (spell->level) + << uint16 (spell->casting_time) + << uint16 (spell->last_sp) + << uint16 (spell->last_grace) + << uint16 (spell->last_eat) + << uint8 (skill) + << uint32 (spell->path_attuned) + << uint32 (spell->face ? spell->face->number : 0) + << data8 (spell->name) + << data16 (spell->msg); } /** @@ -2567,18 +2524,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); + + SockList sl (MAXSOCKBUF); + + sl << "addspell "; + if (!spell) { for (spell = pl->ob->inv; spell != NULL; spell = spell->below) @@ -2598,13 +2556,15 @@ */ if (spell->type != SPELL) continue; + if (sl.len >= (MAXSOCKBUF - (26 + strlen (spell->name) + (spell->msg ? strlen (spell->msg) : 0)))) { Send_With_Handling (&pl->socket, &sl); strcpy ((char *) sl.buf, "addspell "); sl.len = strlen ((char *) sl.buf); } - append_spell (pl, &sl, spell); + + append_spell (pl, sl, spell); } } else if (spell->type != SPELL) @@ -2613,13 +2573,17 @@ return; } else - append_spell (pl, &sl, spell); - if (sl.len >= MAXSOCKBUF) + append_spell (pl, sl, spell); + + if (sl.length () >= MAXSOCKBUF) { LOG (llevError, "Buffer overflow in esrv_add_spells!\n"); fatal (0); } + /* finally, we can send the packet */ Send_With_Handling (&pl->socket, &sl); - free (sl.buf); + + sl.free (); } +