diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-01-09 00:18:53 -0800 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-01-09 00:43:27 -0800 |
commit | f0a87e4f7f5377498960429e96be5dff183c8326 (patch) | |
tree | 2661728f73f3c163e971d94cd123ef6e378264aa /src/common | |
parent | f2fd4885c2a906414e0f36acf95d252e5a9d5805 (diff) | |
download | tmwa-f0a87e4f7f5377498960429e96be5dff183c8326.tar.gz tmwa-f0a87e4f7f5377498960429e96be5dff183c8326.tar.bz2 tmwa-f0a87e4f7f5377498960429e96be5dff183c8326.tar.xz tmwa-f0a87e4f7f5377498960429e96be5dff183c8326.zip |
Make incoming packets read-only
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/socket.cpp | 19 | ||||
-rw-r--r-- | src/common/socket.hpp | 150 | ||||
-rw-r--r-- | src/common/timer.hpp | 8 |
3 files changed, 134 insertions, 43 deletions
diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 023a856..f2e6df6 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -24,11 +24,30 @@ int fd_max; static int currentuse; +static const uint32_t RFIFO_SIZE = 65536; +static const uint32_t WFIFO_SIZE = 65536; struct socket_data *session[FD_SETSIZE]; +/// clean up by discarding handled bytes +inline +void RFIFOFLUSH(int fd) +{ + memmove(session[fd]->rdata, RFIFOP(fd, 0), RFIFOREST(fd)); + session[fd]->rdata_size = RFIFOREST(fd); + session[fd]->rdata_pos = 0; +} + +/// how much room there is to read more data +inline +size_t RFIFOSPACE(int fd) +{ + return session[fd]->max_rdata - session[fd]->rdata_size; +} + + /// Discard all input static void null_parse(int fd); diff --git a/src/common/socket.hpp b/src/common/socket.hpp index ac7fbae..975b7f4 100644 --- a/src/common/socket.hpp +++ b/src/common/socket.hpp @@ -10,47 +10,6 @@ # include <cstdio> # include <ctime> -/// Check how much can be read -# define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) -/// Read from the queue -# define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) -# define RFIFOB(fd,pos) (*(uint8_t*)(RFIFOP(fd, pos))) -# define RFIFOW(fd,pos) (*(uint16_t*)(RFIFOP(fd, pos))) -# define RFIFOL(fd,pos) (*(uint32_t*)(RFIFOP(fd, pos))) -/// Done reading -void RFIFOSKIP(int fd, size_t len); -/// Internal - clean up by discarding handled bytes -# define RFIFOFLUSH(fd) (memmove(session[fd]->rdata,RFIFOP(fd,0),RFIFOREST(fd)),\ -session[fd]->rdata_size=RFIFOREST(fd),\ -session[fd]->rdata_pos=0) - -/// Used internally - how much room there is to read more data -# define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size) - -/// Read from an arbitrary buffer -# define RBUFP(p,pos) (((uint8_t*)(p))+(pos)) -# define RBUFB(p,pos) (*(uint8_t*)RBUFP((p),(pos))) -# define RBUFW(p,pos) (*(uint16_t*)RBUFP((p),(pos))) -# define RBUFL(p,pos) (*(uint32_t*)RBUFP((p),(pos))) - - - -/// Unused - check how much data can be written -# define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) -/// Write to the queue -# define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) -# define WFIFOB(fd,pos) (*(uint8_t*)(WFIFOP(fd,pos))) -# define WFIFOW(fd,pos) (*(uint16_t*)(WFIFOP(fd,pos))) -# define WFIFOL(fd,pos) (*(uint32_t*)(WFIFOP(fd,pos))) -/// Finish writing -void WFIFOSET(int fd, size_t len); - -/// Write to an arbitrary buffer -#define WBUFP(p,pos) (((uint8_t*)(p))+(pos)) -#define WBUFB(p,pos) (*(uint8_t*)WBUFP((p),(pos))) -#define WBUFW(p,pos) (*(uint16_t*)WBUFP((p),(pos))) -#define WBUFL(p,pos) (*(uint32_t*)WBUFP((p),(pos))) - // Struct declaration struct socket_data @@ -128,6 +87,115 @@ void set_defaultparse(void(*defaultparse)(int)); /// Wrappers to track number of free FDs void fclose_(FILE * fp); FILE *fopen_(const char *path, const char *mode); + bool free_fds(void); + + +/// Check how much can be read +inline +size_t RFIFOREST(int fd) +{ + return session[fd]->rdata_size - session[fd]->rdata_pos; +} +/// Read from the queue +inline +const void *RFIFOP(int fd, size_t pos) +{ + return session[fd]->rdata + session[fd]->rdata_pos + pos; +} +inline +uint8_t RFIFOB(int fd, size_t pos) +{ + return *static_cast<const uint8_t *>(RFIFOP(fd, pos)); +} +inline +uint16_t RFIFOW(int fd, size_t pos) +{ + return *static_cast<const uint16_t *>(RFIFOP(fd, pos)); +} +inline +uint32_t RFIFOL(int fd, size_t pos) +{ + return *static_cast<const uint32_t *>(RFIFOP(fd, pos)); +} + +/// Done reading +void RFIFOSKIP(int fd, size_t len); + +/// Read from an arbitrary buffer +inline +const void *RBUFP(const uint8_t *p, size_t pos) +{ + return p + pos; +} +inline +uint8_t RBUFB(const uint8_t *p, size_t pos) +{ + return *static_cast<const uint8_t *>(RBUFP(p, pos)); +} +inline +uint16_t RBUFW(const uint8_t *p, size_t pos) +{ + return *static_cast<const uint16_t *>(RBUFP(p, pos)); +} +inline +uint32_t RBUFL(const uint8_t *p, size_t pos) +{ + return *static_cast<const uint32_t *>(RBUFP(p, pos)); +} + + +/// Unused - check how much data can be written +inline +size_t WFIFOSPACE(int fd) +{ + return session[fd]->max_wdata - session[fd]->wdata_size; +} +/// Write to the queue +inline +void *WFIFOP(int fd, size_t pos) +{ + return session[fd]->wdata + session[fd]->wdata_size + pos; +} +inline +uint8_t& WFIFOB(int fd, size_t pos) +{ + return *static_cast<uint8_t *>(WFIFOP(fd, pos)); +} +inline +uint16_t& WFIFOW(int fd, size_t pos) +{ + return *static_cast<uint16_t *>(WFIFOP(fd, pos)); +} +inline +uint32_t& WFIFOL(int fd, size_t pos) +{ + return *static_cast<uint32_t *>(WFIFOP(fd, pos)); +} +/// Finish writing +void WFIFOSET(int fd, size_t len); + +/// Write to an arbitrary buffer +inline +void *WBUFP(uint8_t *p, size_t pos) +{ + return p + pos; +} +inline +uint8_t& WBUFB(uint8_t *p, size_t pos) +{ + return *static_cast<uint8_t *>(WBUFP(p, pos)); +} +inline +uint16_t& WBUFW(uint8_t *p, size_t pos) +{ + return *static_cast<uint16_t *>(WBUFP(p, pos)); +} +inline +uint32_t& WBUFL(uint8_t *p, size_t pos) +{ + return *static_cast<uint32_t *>(WBUFP(p, pos)); +} + #endif // SOCKET_HPP diff --git a/src/common/timer.hpp b/src/common/timer.hpp index fc4f8cb..db4dedd 100644 --- a/src/common/timer.hpp +++ b/src/common/timer.hpp @@ -10,9 +10,13 @@ enum TIMER_TYPE TIMER_INTERVAL, }; /// This is needed to produce a signed result when 2 ticks are subtracted -# define DIFF_TICK(a,b) ((int32_t)((a)-(b))) +inline +int32_t DIFF_TICK(int32_t a, int32_t b) +{ + return a - b; +} -// TODO replace with signed 64-bit to make code more clear and protect from the future +// TODO replace with std::chrono::time_point and std::chrono::duration typedef uint32_t tick_t; typedef uint32_t interval_t; typedef uint32_t timer_id; |