summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorshennetsind <ind@henn.et>2014-01-18 19:30:11 -0200
committershennetsind <ind@henn.et>2014-01-18 19:30:11 -0200
commit14f84fc044754d22740905b7fa90022b4a04b0d8 (patch)
tree761e2d387fd0bc65ec02964516a15c8d3b85a22d /src/common
parentcbde75bbe7c4a74506437f14b33dd94f8ad15b5a (diff)
downloadhercules-14f84fc044754d22740905b7fa90022b4a04b0d8.tar.gz
hercules-14f84fc044754d22740905b7fa90022b4a04b0d8.tar.bz2
hercules-14f84fc044754d22740905b7fa90022b4a04b0d8.tar.xz
hercules-14f84fc044754d22740905b7fa90022b4a04b0d8.zip
Socket interfaced
We designed this one with having as little changes required elsewhere in mind, thus the way most socket-related things are called has not changed. Plugins may now take a greater advantage of the socket features. Signed-off-by: shennetsind <ind@henn.et>
Diffstat (limited to 'src/common')
-rw-r--r--src/common/HPM.c6
-rw-r--r--src/common/core.c9
-rw-r--r--src/common/socket.c135
-rw-r--r--src/common/socket.h142
-rw-r--r--src/common/strlib.h1
-rw-r--r--src/common/timer.c2
-rw-r--r--src/common/timer.h2
7 files changed, 182 insertions, 115 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c
index e31437b72..cb70ddcd7 100644
--- a/src/common/HPM.c
+++ b/src/common/HPM.c
@@ -675,11 +675,7 @@ void hplugins_share_defaults(void) {
HPM->share(DB, "DB");
HPM->share(HPMiMalloc, "iMalloc");
/* socket */
- HPM->share(RFIFOSKIP,"RFIFOSKIP");
- HPM->share(WFIFOSET,"WFIFOSET");
- HPM->share(do_close,"do_close");
- HPM->share(make_connection,"make_connection");
- //session,fd_max and addr_ are shared from within socket.c
+ HPM->share(sockt,"sockt");
/* strlib */
HPM->share(strlib,"strlib");
HPM->share(sv,"sv");
diff --git a/src/common/core.c b/src/common/core.c
index 5c1e58801..86634ec4b 100644
--- a/src/common/core.c
+++ b/src/common/core.c
@@ -287,6 +287,7 @@ void core_defaults(void) {
sql_defaults();
timer_defaults();
db_defaults();
+ socket_defaults();
#endif
}
/*======================================
@@ -350,14 +351,14 @@ int main (int argc, char **argv) {
HPM->init();
#endif
- socket_init();
+ sockt->init();
do_init(argc,argv);
{// Main runtime cycle
int next;
while (runflag != CORE_ST_STOP) {
- next = timer->do_timer(timer->gettick_nocache());
- do_sockets(next);
+ next = timer->perform(timer->gettick_nocache());
+ sockt->perform(next);
}
}
@@ -368,7 +369,7 @@ int main (int argc, char **argv) {
HPM->final();
#endif
timer->final();
- socket_final();
+ sockt->final();
DB->final();
rathread_final();
#endif
diff --git a/src/common/socket.c b/src/common/socket.c
index 95333cdce..9a61b0827 100644
--- a/src/common/socket.c
+++ b/src/common/socket.c
@@ -10,6 +10,9 @@
#include "../common/strlib.h"
#include "../config/core.h"
#include "../common/HPM.h"
+
+#define _H_SOCKET_C_
+
#include "socket.h"
#include <stdio.h>
@@ -43,6 +46,19 @@
#endif
#endif
+/**
+ * Socket Interface Source
+ **/
+struct socket_interface sockt_s;
+
+#ifdef SEND_SHORTLIST
+ // Add a fd to the shortlist so that it'll be recognized as a fd that needs
+ // sending done on it.
+ void send_shortlist_add_fd(int fd);
+ // Do pending network sends (and eof handling) from the shortlist.
+ void send_shortlist_do_sends();
+#endif
+
/////////////////////////////////////////////////////////////////////
#if defined(WIN32)
/////////////////////////////////////////////////////////////////////
@@ -212,12 +228,6 @@ char* sErr(int code)
#endif
fd_set readfds;
-int fd_max;
-time_t last_tick;
-time_t stall_time = 60;
-
-uint32 addr_[16]; // ip addresses of local host (host byte order)
-int naddr_ = 0; // # of ip addresses
// Maximum packet size in bytes, which the client is able to handle.
// Larger packets cause a buffer overflow and stack corruption.
@@ -328,7 +338,7 @@ void setsocketopts(int fd, struct hSockOpt *opt) {
*--------------------------------------*/
void set_eof(int fd)
{
- if( session_isActive(fd) )
+ if( sockt->session_isActive(fd) )
{
#ifdef SEND_SHORTLIST
// Add this socket to the shortlist for eof handling.
@@ -342,7 +352,7 @@ int recv_to_fifo(int fd)
{
ssize_t len;
- if( !session_isActive(fd) )
+ if( !sockt->session_isActive(fd) )
return -1;
len = sRecv(fd, (char *) session[fd]->rdata + session[fd]->rdata_size, (int)RFIFOSPACE(fd), 0);
@@ -363,7 +373,7 @@ int recv_to_fifo(int fd)
}
session[fd]->rdata_size += len;
- session[fd]->rdata_tick = last_tick;
+ session[fd]->rdata_tick = sockt->last_tick;
#ifdef SHOW_SERVER_STATS
socket_data_i += len;
socket_data_qi += len;
@@ -379,7 +389,7 @@ int send_from_fifo(int fd)
{
ssize_t len;
- if( !session_isValid(fd) )
+ if( !sockt->session_isValid(fd) )
return -1;
if( session[fd]->wdata_size == 0 )
@@ -431,8 +441,8 @@ void flush_fifo(int fd)
void flush_fifos(void)
{
int i;
- for(i = 1; i < fd_max; i++)
- flush_fifo(i);
+ for(i = 1; i < sockt->fd_max; i++)
+ sockt->flush_fifo(i);
}
/*======================================
@@ -466,12 +476,12 @@ int connect_client(int listen_fd) {
#ifndef MINICORE
if( ip_rules && !connect_check(ntohl(client_address.sin_addr.s_addr)) ) {
- do_close(fd);
+ sockt->close(fd);
return -1;
}
#endif
- if( fd_max <= fd ) fd_max = fd + 1;
+ if( sockt->fd_max <= fd ) sockt->fd_max = fd + 1;
sFD_SET(fd,&readfds);
create_session(fd, recv_to_fifo, send_from_fifo, default_func_parse);
@@ -521,7 +531,7 @@ int make_listen_bind(uint32 ip, uint16 port)
exit(EXIT_FAILURE);
}
- if(fd_max <= fd) fd_max = fd + 1;
+ if(sockt->fd_max <= fd) sockt->fd_max = fd + 1;
sFD_SET(fd, &readfds);
create_session(fd, connect_client, null_send, null_parse);
@@ -566,13 +576,13 @@ int make_connection(uint32 ip, uint16 port, struct hSockOpt *opt) {
if( result == SOCKET_ERROR ) {
if( !( opt && opt->silent ) )
ShowError("make_connection: connect failed (socket #%d, %s)!\n", fd, error_msg());
- do_close(fd);
+ sockt->close(fd);
return -1;
}
//Now the socket can be made non-blocking. [Skotlex]
set_nonblocking(fd, 1);
- if (fd_max <= fd) fd_max = fd + 1;
+ if (sockt->fd_max <= fd) sockt->fd_max = fd + 1;
sFD_SET(fd,&readfds);
create_session(fd, recv_to_fifo, send_from_fifo, default_func_parse);
@@ -591,7 +601,7 @@ static int create_session(int fd, RecvFunc func_recv, SendFunc func_send, ParseF
session[fd]->func_recv = func_recv;
session[fd]->func_send = func_send;
session[fd]->func_parse = func_parse;
- session[fd]->rdata_tick = last_tick;
+ session[fd]->rdata_tick = sockt->last_tick;
session[fd]->session_data = NULL;
session[fd]->hdata = NULL;
session[fd]->hdatac = 0;
@@ -600,7 +610,7 @@ static int create_session(int fd, RecvFunc func_recv, SendFunc func_send, ParseF
static void delete_session(int fd)
{
- if( session_isValid(fd) ) {
+ if( sockt->session_isValid(fd) ) {
unsigned int i;
#ifdef SHOW_SERVER_STATS
socket_data_qi -= session[fd]->rdata_size - session[fd]->rdata_pos;
@@ -625,7 +635,7 @@ static void delete_session(int fd)
int realloc_fifo(int fd, unsigned int rfifo_size, unsigned int wfifo_size)
{
- if( !session_isValid(fd) )
+ if( !sockt->session_isValid(fd) )
return 0;
if( session[fd]->max_rdata != rfifo_size && session[fd]->rdata_size < rfifo_size) {
@@ -644,7 +654,7 @@ int realloc_writefifo(int fd, size_t addition)
{
size_t newsize;
- if( !session_isValid(fd) ) // might not happen
+ if( !sockt->session_isValid(fd) ) // might not happen
return 0;
if( session[fd]->wdata_size + addition > session[fd]->max_wdata )
@@ -672,7 +682,7 @@ int RFIFOSKIP(int fd, size_t len)
{
struct socket_data *s;
- if ( !session_isActive(fd) )
+ if ( !sockt->session_isActive(fd) )
return 0;
s = session[fd];
@@ -695,7 +705,7 @@ int WFIFOSET(int fd, size_t len)
size_t newreserve;
struct socket_data* s = session[fd];
- if( !session_isValid(fd) || s->wdata == NULL )
+ if( !sockt->session_isValid(fd) || s->wdata == NULL )
return 0;
// we have written len bytes to the buffer already before calling WFIFOSET
@@ -771,7 +781,7 @@ int do_sockets(int next)
#ifdef SEND_SHORTLIST
send_shortlist_do_sends();
#else
- for (i = 1; i < fd_max; i++)
+ for (i = 1; i < sockt->fd_max; i++)
{
if(!session[i])
continue;
@@ -786,7 +796,7 @@ int do_sockets(int next)
timeout.tv_usec = next%1000*1000;
memcpy(&rfd, &readfds, sizeof(rfd));
- ret = sSelect(fd_max, &rfd, NULL, NULL, &timeout);
+ ret = sSelect(sockt->fd_max, &rfd, NULL, NULL, &timeout);
if( ret == SOCKET_ERROR )
{
@@ -798,7 +808,7 @@ int do_sockets(int next)
return 0; // interrupted by a signal, just loop and try again
}
- last_tick = time(NULL);
+ sockt->last_tick = time(NULL);
#if defined(WIN32)
// on windows, enumerating all members of the fd_set is way faster if we access the internals
@@ -810,7 +820,7 @@ int do_sockets(int next)
}
#else
// otherwise assume that the fd_set is a bit-array and enumerate it in a standard way
- for( i = 1; ret && i < fd_max; ++i )
+ for( i = 1; ret && i < sockt->fd_max; ++i )
{
if(sFD_ISSET(i,&rfd) && session[i])
{
@@ -824,7 +834,7 @@ int do_sockets(int next)
#ifdef SEND_SHORTLIST
send_shortlist_do_sends();
#else
- for (i = 1; i < fd_max; i++)
+ for (i = 1; i < sockt->fd_max; i++)
{
if(!session[i])
continue;
@@ -840,12 +850,12 @@ int do_sockets(int next)
#endif
// parse input data on each socket
- for(i = 1; i < fd_max; i++)
+ for(i = 1; i < sockt->fd_max; i++)
{
if(!session[i])
continue;
- if (session[i]->rdata_tick && DIFF_TICK(last_tick, session[i]->rdata_tick) > stall_time) {
+ if (session[i]->rdata_tick && DIFF_TICK(sockt->last_tick, session[i]->rdata_tick) > sockt->stall_time) {
if( session[i]->flag.server ) {/* server is special */
if( session[i]->flag.ping != 2 )/* only update if necessary otherwise it'd resend the ping unnecessarily */
session[i]->flag.ping = 1;
@@ -873,7 +883,7 @@ int do_sockets(int next)
}
#ifdef SHOW_SERVER_STATS
- if (last_tick != socket_data_last_tick)
+ if (sockt->last_tick != socket_data_last_tick)
{
char buf[1024];
@@ -883,7 +893,7 @@ int do_sockets(int next)
#else
ShowMessage("\033[s\033[1;1H\033[2K%s\033[u", buf);
#endif
- socket_data_last_tick = last_tick;
+ socket_data_last_tick = sockt->last_tick;
socket_data_i = socket_data_ci = 0;
socket_data_o = socket_data_co = 0;
}
@@ -1148,9 +1158,9 @@ int socket_config_read(const char* cfgName)
continue;
if (!strcmpi(w1, "stall_time")) {
- stall_time = atoi(w2);
- if( stall_time < 3 )
- stall_time = 3;/* a minimum is required to refrain it from killing itself */
+ sockt->stall_time = atoi(w2);
+ if( sockt->stall_time < 3 )
+ sockt->stall_time = 3;/* a minimum is required to refrain it from killing itself */
}
#ifndef MINICORE
else if (!strcmpi(w1, "enable_ip_rules")) {
@@ -1218,9 +1228,9 @@ void socket_final(void)
aFree(access_deny);
#endif
- for( i = 1; i < fd_max; i++ )
+ for( i = 1; i < sockt->fd_max; i++ )
if(session[i])
- do_close(i);
+ sockt->close(i);
// session[0] のダミーデータを削除
aFree(session[0]->rdata);
@@ -1382,7 +1392,7 @@ void socket_init(void)
#endif
// Get initial local ips
- naddr_ = socket_getips(addr_,16);
+ sockt->naddr_ = socket_getips(sockt->addr_,16);
sFD_ZERO(&readfds);
#if defined(SEND_SHORTLIST)
@@ -1394,7 +1404,7 @@ void socket_init(void)
socket_config_read(SOCKET_CONF_FILENAME);
// initialise last send-receive tick
- last_tick = time(NULL);
+ sockt->last_tick = time(NULL);
// session[0] is now currently used for disconnected sessions of the map server, and as such,
// should hold enough buffer (it is a vacuum so to speak) as it is never flushed. [Skotlex]
@@ -1411,8 +1421,6 @@ void socket_init(void)
/* Hercules Plugin Manager */
HPM->share(session,"session");
- HPM->share(&fd_max,"fd_max");
- HPM->share(addr_,"addr");
}
bool session_isValid(int fd)
@@ -1422,7 +1430,7 @@ bool session_isValid(int fd)
bool session_isActive(int fd)
{
- return ( session_isValid(fd) && !session[fd]->flag.eof );
+ return ( sockt->session_isValid(fd) && !session[fd]->flag.eof );
}
// Resolves hostname into a numeric ip.
@@ -1526,7 +1534,7 @@ void send_shortlist_add_fd(int fd)
int i;
int bit;
- if( !session_isValid(fd) )
+ if( !sockt->session_isValid(fd) )
return;// out of range
i = fd/32;
@@ -1595,3 +1603,44 @@ void send_shortlist_do_sends()
}
}
#endif
+
+void socket_defaults(void) {
+ sockt = &sockt_s;
+
+ sockt->fd_max = 0;
+ /* */
+ sockt->stall_time = 60;
+ sockt->last_tick = 0;
+ /* */
+ memset(&sockt->addr_, 0, sizeof(sockt->addr_));
+ sockt->naddr_ = 0;
+ /* */
+ sockt->init = socket_init;
+ sockt->final = socket_final;
+ /* */
+ sockt->perform = do_sockets;
+ /* */
+ sockt->datasync = socket_datasync;
+ /* */
+ sockt->make_listen_bind = make_listen_bind;
+ sockt->make_connection = make_connection;
+ sockt->realloc_fifo = realloc_fifo;
+ sockt->realloc_writefifo = realloc_writefifo;
+ sockt->WFIFOSET = WFIFOSET;
+ sockt->RFIFOSKIP = RFIFOSKIP;
+ sockt->close = do_close;
+ /* */
+ sockt->session_isValid = session_isValid;
+ sockt->session_isActive = session_isActive;
+ /* */
+ sockt->flush_fifo = flush_fifo;
+ sockt->flush_fifos = flush_fifos;
+ sockt->set_nonblocking = set_nonblocking;
+ sockt->set_defaultparse = set_defaultparse;
+ sockt->host2ip = host2ip;
+ sockt->ip2str = ip2str;
+ sockt->str2ip = str2ip;
+ sockt->ntows = ntows;
+ sockt->getips = socket_getips;
+ sockt->set_eof = set_eof;
+}
diff --git a/src/common/socket.h b/src/common/socket.h
index 02817f653..6879d2e90 100644
--- a/src/common/socket.h
+++ b/src/common/socket.h
@@ -107,73 +107,93 @@ struct hSockOpt {
unsigned int setTimeo : 1;
};
-// Data prototype declaration
-
-struct socket_data **session;
-
-extern int fd_max;
-
-extern time_t last_tick;
-extern time_t stall_time;
-
-//////////////////////////////////
-// some checking on sockets
-extern bool session_isValid(int fd);
-extern bool session_isActive(int fd);
-//////////////////////////////////
-
-// Function prototype declaration
-
-int make_listen_bind(uint32 ip, uint16 port);
-int make_connection(uint32 ip, uint16 port, struct hSockOpt *opt);
-int realloc_fifo(int fd, unsigned int rfifo_size, unsigned int wfifo_size);
-int realloc_writefifo(int fd, size_t addition);
-int WFIFOSET(int fd, size_t len);
-int RFIFOSKIP(int fd, size_t len);
-
-int do_sockets(int next);
-void do_close(int fd);
-void socket_init(void);
-void socket_final(void);
-
-extern void flush_fifo(int fd);
-extern void flush_fifos(void);
-extern void set_nonblocking(int fd, unsigned long yes);
-
-void set_defaultparse(ParseFunc defaultparse);
+/// Use a shortlist of sockets instead of iterating all sessions for sockets
+/// that have data to send or need eof handling.
+/// Adapted to use a static array instead of a linked list.
+///
+/// @author Buuyo-tama
+#define SEND_SHORTLIST
-// hostname/ip conversion functions
-uint32 host2ip(const char* hostname);
-const char* ip2str(uint32 ip, char ip_str[16]);
-uint32 str2ip(const char* ip_str);
// Note: purposely returns four comma-separated arguments
#define CONVIP(ip) ((ip)>>24)&0xFF,((ip)>>16)&0xFF,((ip)>>8)&0xFF,((ip)>>0)&0xFF
#define MAKEIP(a,b,c,d) ((uint32)( ( ( (a)&0xFF ) << 24 ) | ( ( (b)&0xFF ) << 16 ) | ( ( (c)&0xFF ) << 8 ) | ( ( (d)&0xFF ) << 0 ) ))
-uint16 ntows(uint16 netshort);
-
-int socket_getips(uint32* ips, int max);
-
-extern uint32 addr_[16]; // ip addresses of local host (host byte order)
-extern int naddr_; // # of ip addresses
-void set_eof(int fd);
-
-/* [Ind/Hercules] - socket_datasync */
-void socket_datasync(int fd, bool send);
+/**
+ * This stays out of the interface.
+ **/
+struct socket_data **session;
-/// Use a shortlist of sockets instead of iterating all sessions for sockets
-/// that have data to send or need eof handling.
-/// Adapted to use a static array instead of a linked list.
-///
-/// @author Buuyo-tama
-#define SEND_SHORTLIST
+/**
+ * Socket.c interface, mostly for reading however.
+ **/
+struct socket_interface {
+ int fd_max;
+ /* */
+ time_t stall_time;
+ time_t last_tick;
+ /* */
+ uint32 addr_[16]; // ip addresses of local host (host byte order)
+ int naddr_; // # of ip addresses
+ /* */
+ void (*init) (void);
+ void (*final) (void);
+ /* */
+ int (*perform) (int next);
+ /* [Ind/Hercules] - socket_datasync */
+ void (*datasync) (int fd, bool send);
+ /* */
+ int (*make_listen_bind) (uint32 ip, uint16 port);
+ int (*make_connection) (uint32 ip, uint16 port, struct hSockOpt *opt);
+ int (*realloc_fifo) (int fd, unsigned int rfifo_size, unsigned int wfifo_size);
+ int (*realloc_writefifo) (int fd, size_t addition);
+ int (*WFIFOSET) (int fd, size_t len);
+ int (*RFIFOSKIP) (int fd, size_t len);
+ void (*close) (int fd);
+ /* */
+ bool (*session_isValid) (int fd);
+ bool (*session_isActive) (int fd);
+ /* */
+ void (*flush_fifo) (int fd);
+ void (*flush_fifos) (void);
+ void (*set_nonblocking) (int fd, unsigned long yes);
+ void (*set_defaultparse) (ParseFunc defaultparse);
+ /* hostname/ip conversion functions */
+ uint32 (*host2ip) (const char* hostname);
+ const char * (*ip2str) (uint32 ip, char ip_str[16]);
+ uint32 (*str2ip) (const char* ip_str);
+ /* */
+ uint16 (*ntows) (uint16 netshort);
+ /* */
+ int (*getips) (uint32* ips, int max);
+ /* */
+ void (*set_eof) (int fd);
+};
-#ifdef SEND_SHORTLIST
-// Add a fd to the shortlist so that it'll be recognized as a fd that needs
-// sending done on it.
-void send_shortlist_add_fd(int fd);
-// Do pending network sends (and eof handling) from the shortlist.
-void send_shortlist_do_sends();
-#endif
+struct socket_interface *sockt;
+
+void socket_defaults(void);
+
+/* the purpose of these macros is simply to not make calling them be an annoyance */
+#ifndef _H_SOCKET_C_
+ #define make_listen_bind(ip, port) ( sockt->make_listen_bind(ip, port) )
+ #define make_connection(ip, port, opt) ( sockt->make_connection(ip, port, opt) )
+ #define realloc_fifo(fd, rfifo_size, wfifo_size) ( sockt->realloc_fifo(fd, rfifo_size, wfifo_size) )
+ #define realloc_writefifo(fd, addition) ( sockt->realloc_writefifo(fd, addition) )
+ #define WFIFOSET(fd, len) ( sockt->WFIFOSET(fd, len) )
+ #define RFIFOSKIP(fd, len) ( sockt->RFIFOSKIP(fd, len) )
+ #define do_close(fd) ( sockt->close(fd) )
+ #define session_isValid(fd) ( sockt->session_isValid(fd) )
+ #define session_isActive(fd) ( sockt->session_isActive(fd) )
+ #define flush_fifo(fd) ( sockt->flush_fifo(fd) )
+ #define flush_fifos() ( sockt->flush_fifos() )
+ #define set_nonblocking(fd, yes) ( sockt->set_nonblocking(fd, yes) )
+ #define set_defaultparse(defaultparse) ( sockt->set_defaultparse(defaultparse) )
+ #define host2ip(hostname) ( sockt->host2ip(hostname) )
+ #define ip2str(ip, ip_str) ( sockt->ip2str(ip, ip_str) )
+ #define str2ip(ip_str) ( sockt->str2ip(ip_str) )
+ #define ntows(netshort) ( sockt->ntows(netshort) )
+ #define getips(ips, max) ( sockt->getips(ips, max) )
+ #define set_eof(fd) ( sockt->set_eof(fd) )
+#endif /* _H_SOCKET_C_ */
#endif /* _SOCKET_H_ */
diff --git a/src/common/strlib.h b/src/common/strlib.h
index d1aea8cac..5336260f6 100644
--- a/src/common/strlib.h
+++ b/src/common/strlib.h
@@ -190,4 +190,5 @@ void strlib_defaults(void);
#define strline(str,pos) (strlib->strline((str),(pos)))
#define bin2hex(output,input,count) (strlib->bin2hex((output),(input),(count)))
#endif /* STRLIB_C */
+
#endif /* _STRLIB_H_ */
diff --git a/src/common/timer.c b/src/common/timer.c
index 7f9e20dad..526854582 100644
--- a/src/common/timer.c
+++ b/src/common/timer.c
@@ -468,7 +468,7 @@ void timer_defaults(void) {
timer->addtick = timer_addtick;
timer->settick = timer_settick;
timer->get_uptime = timer_get_uptime;
- timer->do_timer = do_timer;
+ timer->perform = do_timer;
timer->init = timer_init;
timer->final = timer_final;
}
diff --git a/src/common/timer.h b/src/common/timer.h
index 9aa29861e..af1a2b036 100644
--- a/src/common/timer.h
+++ b/src/common/timer.h
@@ -56,7 +56,7 @@ struct timer_interface {
unsigned long (*get_uptime) (void);
- int (*do_timer) (int64 tick);
+ int (*perform) (int64 tick);
void (*init) (void);
void (*final) (void);
};