From ac359e696f162090840dab488a6ef4981d35bbda Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sat, 18 May 2013 17:37:33 -0700 Subject: Tweak a little memory management in char-server --- src/common/socket.hpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/common/socket.hpp') diff --git a/src/common/socket.hpp b/src/common/socket.hpp index d7c6b9c..d526379 100644 --- a/src/common/socket.hpp +++ b/src/common/socket.hpp @@ -7,9 +7,21 @@ # include +# include + +# include "dumb_ptr.hpp" # include "utils.hpp" # include "timer.t.hpp" +struct SessionData +{ +}; +struct SessionDeleter +{ + // defined per-server + void operator()(SessionData *sd); +}; + // Struct declaration struct socket_data @@ -23,7 +35,7 @@ struct socket_data /// Since this is a single-threaded application, it can't block /// These are the read/write queues - uint8_t *rdata, *wdata; + dumb_ptr rdata, wdata; size_t max_rdata, max_wdata; /// How much is actually in the queue size_t rdata_size, wdata_size; @@ -37,15 +49,15 @@ struct socket_data /// Only called when select() indicates the socket is ready /// If, after that, nothing is read, it sets eof // These could probably be hard-coded with a little work - void(*func_recv)(int); - void(*func_send)(int); + void (*func_recv)(int); + void (*func_send)(int); /// This is the important one /// Set to different functions depending on whether the connection /// is a player or a server/ladmin /// Can be set explicitly or via set_defaultparse - void(*func_parse)(int); + void (*func_parse)(int); /// Server-specific data type - void *session_data; + std::unique_ptr session_data; }; // save file descriptors for important stuff @@ -56,7 +68,7 @@ constexpr int CONNECT_TIMEOUT = 15; /// Everyone who has connected // note: call delete_session(i) to null out an element -extern struct socket_data *session[FD_SETSIZE]; +extern std::array, FD_SETSIZE> session; /// Maximum used FD, +1 extern int fd_max; @@ -102,7 +114,7 @@ size_t RFIFOREST(int fd) inline const void *RFIFOP(int fd, size_t pos) { - return session[fd]->rdata + session[fd]->rdata_pos + pos; + return &session[fd]->rdata[session[fd]->rdata_pos + pos]; } inline uint8_t RFIFOB(int fd, size_t pos) @@ -156,7 +168,7 @@ size_t WFIFOSPACE(int fd) inline void *WFIFOP(int fd, size_t pos) { - return session[fd]->wdata + session[fd]->wdata_size + pos; + return &session[fd]->wdata[session[fd]->wdata_size + pos]; } inline uint8_t& WFIFOB(int fd, size_t pos) -- cgit v1.2.3-60-g2f50