From 37a7546e284e9f330b137178d6c4646842bde97f Mon Sep 17 00:00:00 2001 From: ultramage Date: Wed, 7 Feb 2007 19:34:54 +0000 Subject: Cleaning up of the socket code - Shuffled includes around for files that were leeching off of socket.h - Added code that enforces standard shutdown behavior for sockets git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@9817 54d463be-8e91-2dee-dedb-b68131a5f0ec --- Changelog-Trunk.txt | 3 +++ src/char/int_guild.c | 1 + src/char/int_homun.c | 1 + src/char/int_party.c | 1 + src/char/int_pet.c | 1 + src/char/int_storage.c | 2 ++ src/char/inter.c | 1 + src/char_sql/int_homun.c | 1 + src/char_sql/int_party.c | 1 + src/char_sql/int_pet.c | 1 + src/char_sql/int_storage.c | 2 ++ src/char_sql/inter.c | 2 ++ src/char_sql/itemdb.c | 1 + src/common/lock.h | 2 ++ src/common/socket.c | 46 ++++++++++++++++++------------------ src/common/socket.h | 58 +++++++++++++--------------------------------- src/map/atcommand.c | 2 +- src/map/irc.c | 6 +++++ src/map/mercenary.c | 1 + 19 files changed, 66 insertions(+), 67 deletions(-) diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt index 9b645a7d5..0a4bc14b6 100644 --- a/Changelog-Trunk.txt +++ b/Changelog-Trunk.txt @@ -4,6 +4,9 @@ AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK. 2007/02/07 + * Cleaning up of the socket code [ultramage] + - Shuffled includes around for files that were leeching off of socket.h + - Added code that enforces standard shutdown behavior for sockets * Removed old script bonuses bMatk, bMatk1, bMatk2. * Fixed char-server not sending online notification when a party member logs in. diff --git a/src/char/int_guild.c b/src/char/int_guild.c index daa80e6d6..6886287e2 100644 --- a/src/char/int_guild.c +++ b/src/char/int_guild.c @@ -7,6 +7,7 @@ #include "../common/cbasetypes.h" #include "../common/mmo.h" +#include "../common/malloc.h" #include "../common/socket.h" #include "../common/db.h" #include "../common/lock.h" diff --git a/src/char/int_homun.c b/src/char/int_homun.c index 6d93a375a..943f5b8c8 100644 --- a/src/char/int_homun.c +++ b/src/char/int_homun.c @@ -6,6 +6,7 @@ #include #include "../common/mmo.h" +#include "../common/malloc.h" #include "../common/socket.h" #include "../common/db.h" #include "../common/lock.h" diff --git a/src/char/int_party.c b/src/char/int_party.c index 96a120aab..0612afb0b 100644 --- a/src/char/int_party.c +++ b/src/char/int_party.c @@ -7,6 +7,7 @@ #include "../common/cbasetypes.h" #include "../common/mmo.h" +#include "../common/malloc.h" #include "../common/socket.h" #include "../common/db.h" #include "../common/lock.h" diff --git a/src/char/int_pet.c b/src/char/int_pet.c index d1448c5a1..c49150a9c 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -6,6 +6,7 @@ #include #include "../common/mmo.h" +#include "../common/malloc.h" #include "../common/socket.h" #include "../common/db.h" #include "../common/lock.h" diff --git a/src/char/int_storage.c b/src/char/int_storage.c index 5f1f68275..e19268508 100644 --- a/src/char/int_storage.c +++ b/src/char/int_storage.c @@ -1,10 +1,12 @@ // Copyright (c) Athena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder +#include #include #include #include "../common/mmo.h" +#include "../common/malloc.h" #include "../common/socket.h" #include "../common/db.h" #include "../common/lock.h" diff --git a/src/char/inter.c b/src/char/inter.c index e5e397709..a42202971 100644 --- a/src/char/inter.c +++ b/src/char/inter.c @@ -1,6 +1,7 @@ // Copyright (c) Athena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder +#include #include #include diff --git a/src/char_sql/int_homun.c b/src/char_sql/int_homun.c index 6fce092ac..22edad8d0 100644 --- a/src/char_sql/int_homun.c +++ b/src/char_sql/int_homun.c @@ -6,6 +6,7 @@ #include #include "char.h" +#include "../common/malloc.h" #include "../common/strlib.h" #include "../common/showmsg.h" diff --git a/src/char_sql/int_party.c b/src/char_sql/int_party.c index 8553862fe..72e98f323 100644 --- a/src/char_sql/int_party.c +++ b/src/char_sql/int_party.c @@ -10,6 +10,7 @@ #include "../common/cbasetypes.h" #include "../common/db.h" +#include "../common/malloc.h" #include "../common/strlib.h" #include "../common/socket.h" #include "../common/showmsg.h" diff --git a/src/char_sql/int_pet.c b/src/char_sql/int_pet.c index 1b87de654..099b02aaa 100644 --- a/src/char_sql/int_pet.c +++ b/src/char_sql/int_pet.c @@ -9,6 +9,7 @@ #include #include "char.h" +#include "../common/malloc.h" #include "../common/strlib.h" #include "../common/showmsg.h" diff --git a/src/char_sql/int_storage.c b/src/char_sql/int_storage.c index 5ca2c25bb..72b56e5ef 100644 --- a/src/char_sql/int_storage.c +++ b/src/char_sql/int_storage.c @@ -4,11 +4,13 @@ // original code from athena // SQL conversion by Jioh L. Jung +#include #include #include #include "char.h" #include "itemdb.h" +#include "../common/malloc.h" #include "../common/showmsg.h" #define STORAGE_MEMINC 16 diff --git a/src/char_sql/inter.c b/src/char_sql/inter.c index 03ab85773..326bd2ca1 100644 --- a/src/char_sql/inter.c +++ b/src/char_sql/inter.c @@ -4,10 +4,12 @@ // original code from athena // SQL conversion by Jioh L. Jung +#include #include #include #include "char.h" +#include "../common/malloc.h" #include "../common/strlib.h" #include "../common/showmsg.h" #include "inter.h" diff --git a/src/char_sql/itemdb.c b/src/char_sql/itemdb.c index 3943c082d..5feea2492 100644 --- a/src/char_sql/itemdb.c +++ b/src/char_sql/itemdb.c @@ -10,6 +10,7 @@ #include "inter.h" #include "char.h" #include "utils.h" +#include "../common/malloc.h" #include "../common/showmsg.h" #define MAX_RANDITEM 2000 diff --git a/src/common/lock.h b/src/common/lock.h index e2247fc68..e90fbf745 100644 --- a/src/common/lock.h +++ b/src/common/lock.h @@ -4,6 +4,8 @@ #ifndef _LOCK_H_ #define _LOCK_H_ +#include + FILE* lock_fopen(const char* filename,int *info); int lock_fclose(FILE *fp,const char* filename,int *info); diff --git a/src/common/socket.c b/src/common/socket.c index 1dd85489d..dc7dbbca0 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -103,10 +103,9 @@ void set_defaultparse(int (*defaultparse)(int)) void set_nonblocking(int fd, int yes) { - // I don't think we need this // TCP_NODELAY BOOL Disables the Nagle algorithm for send coalescing. if(mode_neg) - setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof yes); + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,(char *)&yes, sizeof yes); // FIONBIO Use with a nonzero argp parameter to enable the nonblocking mode of socket s. // The argp parameter is zero if nonblocking is to be disabled. @@ -129,16 +128,16 @@ static void setsocketopts(int fd) setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof(yes)); // setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &wfifo_size , sizeof(rfifo_size )); // setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &rfifo_size , sizeof(rfifo_size )); -#ifdef __WIN32 -{ //set SO_LINGER option (from Freya) + + // force the socket into no-wait, graceful-close mode (should be the default, but better make sure) //(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/closesocket_2.asp) + { struct linger opt; - opt.l_onoff = 1; - opt.l_linger = 0; + opt.l_onoff = 0; // SO_DONTLINGER + opt.l_linger = 0; // Do not care if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&opt, sizeof(opt))) ShowWarning("setsocketopts: Unable to set SO_LINGER mode for connection %d!\n",fd); -} -#endif + } } /*====================================== @@ -148,14 +147,14 @@ static void setsocketopts(int fd) static void set_eof(int fd) { //Marks a connection eof and invokes the parse_function to disconnect it right away. [Skotlex] if (session_isActive(fd)) - session[fd]->eof=1; + session[fd]->eof = 1; } static int recv_to_fifo(int fd) { int len; - if( (fd < 0) || (fd >= FD_SETSIZE) || (NULL == session[fd]) || (session[fd]->eof) ) + if( !session_isActive(fd) ) return -1; len = recv(fd, (char *) session[fd]->rdata + session[fd]->rdata_size, RFIFOSPACE(fd), 0); @@ -218,8 +217,7 @@ static int send_from_fifo(int fd) return 0; } -/// Best effort -/// There's no warranty that the data will be sent. +/// Best effort - there's no warranty that the data will be sent. void flush_fifo(int fd) { if(session[fd] != NULL && session[fd]->func_send == send_from_fifo) @@ -229,9 +227,8 @@ void flush_fifo(int fd) void flush_fifos(void) { int i; - for(i=1;ifunc_send == send_from_fifo) + for(i = 1; i < fd_max; i++) + if(session[i] != NULL && session[i]->func_send == send_from_fifo) send_from_fifo(i); } @@ -327,7 +324,7 @@ int make_listen_bind(long ip,int port) exit(1); } - if(fd_max<=fd) fd_max=fd+1; + if(fd_max <= fd) fd_max = fd + 1; FD_SET(fd, &readfds ); CREATE(session[fd], struct socket_data, 1); @@ -489,7 +486,7 @@ int realloc_writefifo(int fd, size_t addition) return 0; } -int WFIFOSET(int fd,int len) +int WFIFOSET(int fd, int len) { size_t newreserve; struct socket_data *s = session[fd]; @@ -668,7 +665,7 @@ int do_parsepacket(void) { int i; struct socket_data *sd; - for(i = 1; i < fd_max; i++){ + for(i = 1; i < fd_max; i++) { sd = session[i]; if(!sd) continue; @@ -1017,7 +1014,7 @@ int RFIFOSKIP(int fd,int len) { struct socket_data *s; - if ( !session_isActive(fd) ) //Nullpo error here[Kevin] + if ( !session_isActive(fd) ) return 0; s = session[fd]; @@ -1195,9 +1192,9 @@ void socket_init(void) // initialise last send-receive tick last_tick = time(0); - // session[0] is for disconnected sessions of the map server, and as such, + // 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] - //##TODO "flush" this session periodically O.O [FlavioJS] + // ##TODO "flush" this session periodically O.O [FlavioJS] CREATE(session[0], struct socket_data, 1); CREATE(session[0]->rdata, unsigned char, 2*rfifo_size); CREATE(session[0]->wdata, unsigned char, 2*wfifo_size); @@ -1218,8 +1215,8 @@ void socket_init(void) int session_isValid(int fd) -{ //End of Exam has pointed out that fd==0 is actually an unconnected session! [Skotlex] - return ( (fd>0) && (fd 0) && (fd < FD_SETSIZE) && (session[fd] != NULL) ); } int session_isActive(int fd) @@ -1227,7 +1224,8 @@ int session_isActive(int fd) return ( session_isValid(fd) && !session[fd]->eof ); } -in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str) { +in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str) +{ struct hostent *h = gethostbyname(hostname); char ip_buf[16]; unsigned char ip2[4]; diff --git a/src/common/socket.h b/src/common/socket.h index 7cf0fcd91..65c18a5a6 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -4,10 +4,7 @@ #ifndef _SOCKET_H_ #define _SOCKET_H_ -#include "../common/cbasetypes.h" - #ifdef WIN32 - #define __USE_W32_SOCKETS #include typedef long in_addr_t; #else @@ -16,30 +13,24 @@ #include #endif -#include -#include -#include "../common/malloc.h" - -extern time_t last_tick; -extern time_t stall_time; +#include "../common/cbasetypes.h" // define declaration -#define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size) #ifdef TURBO #define RFIFOVAR(fd) rbPtr ## fd #define RFIFOHEAD(fd) uint8 *RFIFOVAR(fd) = session[fd]->rdata+session[fd]->rdata_pos #define RFIFOP(fd,pos) ( &RFIFOVAR(fd) + (pos) ) #else -//Make it a comment so it does not disrupts the rest of code. -#define RFIFOHEAD(fd) // -#define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) +#define RFIFOHEAD(fd) +#define RFIFOP(fd,pos) (session[fd]->rdata + session[fd]->rdata_pos + (pos)) #endif -// use function instead of macro. #define RFIFOB(fd,pos) (*(uint8*)RFIFOP(fd,pos)) #define RFIFOW(fd,pos) (*(uint16*)RFIFOP(fd,pos)) #define RFIFOL(fd,pos) (*(uint32*)RFIFOP(fd,pos)) -#define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) +#define RFIFOSPACE(fd) (session[fd]->max_rdata - session[fd]->rdata_size) +#define RFIFOREST(fd) (session[fd]->rdata_size - session[fd]->rdata_pos) +//#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size - session[fd]->rdata_pos - (len) < 0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos += (len))) #define RFIFOFLUSH(fd) \ do { \ if(session[fd]->rdata_size == session[fd]->rdata_pos){ \ @@ -47,31 +38,26 @@ extern time_t stall_time; } else { \ session[fd]->rdata_size -= session[fd]->rdata_pos; \ memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \ - session[fd]->rdata_pos=0; \ + session[fd]->rdata_pos = 0; \ } \ } while(0) - -//#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size-session[fd]->rdata_pos-(len)<0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos+=(len))) - -#define RBUFP(p,pos) (((uint8*)(p))+(pos)) +#define RBUFP(p,pos) (((uint8*)(p)) + (pos)) #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos))) #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos))) #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos))) -#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) #ifdef TURBO #define WFIFOVAR(fd) wbPtr ## fd #define WFIFOHEAD(fd, x) uint8 *WFIFOVAR(fd) = ( (fd) > 0 && session[fd] ? session[fd]->wdata+session[fd]->wdata_size : NULL ) #define WFIFOP(fd,pos) ( &WFIFOVAR(fd) + (pos) ) #else #define WFIFOHEAD(fd, size) do{ if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); }while(0) - #define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) #endif #define WFIFOB(fd,pos) (*(uint8*)WFIFOP(fd,pos)) #define WFIFOW(fd,pos) (*(uint16*)WFIFOP(fd,pos)) #define WFIFOL(fd,pos) (*(uint32*)WFIFOP(fd,pos)) -// use function instead of macro. +#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) //#define WFIFOSET(fd,len) (session[fd]->wdata_size = (session[fd]->wdata_size + (len) + 2048 < session[fd]->max_wdata) ? session[fd]->wdata_size + len : session[fd]->wdata_size) #define WBUFP(p,pos) (((uint8*)(p)) + (pos)) #define WBUFB(p,pos) (*(uint8*)((p) + (pos))) @@ -82,22 +68,6 @@ extern time_t stall_time; #define TOW(n) ((uint16)((n)&UINT16_MAX)) #define TOL(n) ((uint32)((n)&UINT32_MAX)) -//FD_SETSIZE must be modified on the project files/Makefile, since a change here won't affect -// dependant windows libraries. -/* -#ifdef __WIN32 -//The default FD_SETSIZE is kinda small for windows systems. - #ifdef FD_SETSIZE - #undef FD_SETSIZE - #endif -#define FD_SETSIZE 4096 -#endif -*/ -#ifdef __INTERIX -#define FD_SETSIZE 4096 -#endif // __INTERIX - -/* Removed Cygwin FD_SETSIZE declarations, now are directly passed on to the compiler through Makefile [Valaris] */ // Session type enum SessionType { @@ -110,7 +80,7 @@ enum SessionType { // Struct declaration -struct socket_data{ +struct socket_data { unsigned char eof; unsigned char *rdata, *wdata; size_t max_rdata, max_wdata; @@ -140,6 +110,9 @@ extern struct socket_data *session[FD_SETSIZE]; extern int fd_max; +extern time_t last_tick; +extern time_t stall_time; + ////////////////////////////////// // some checking on sockets extern int session_isValid(int fd); @@ -175,10 +148,11 @@ void set_defaultparse(int (*defaultparse)(int)); //hostname is the name to resolve. //ip is an array of char[4] where the individual parts of the ip are stored (optional) //ip_str is a char[16] where the whole ip is stored in string notation (optional) -in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str); +in_addr_t resolve_hostbyname(char* hostname, unsigned char* ip, char* ip_str); -int socket_getips(uint32 *ips, int max); +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 + #endif // _SOCKET_H_ diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 016801119..c7ad31805 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -9347,7 +9347,7 @@ int atcommand_homhungry(const int fd, struct map_session_data* sd, const char* c } /*========================================== - * modify homunculus hunger [orn] + * make the homunculus speak [orn] *------------------------------------------ */ int atcommand_homtalk(const int fd, struct map_session_data* sd, const char* command, const char* message) diff --git a/src/map/irc.c b/src/map/irc.c index 9989c3c31..55eafdcd9 100644 --- a/src/map/irc.c +++ b/src/map/irc.c @@ -1,4 +1,8 @@ +#ifndef _IRC_H_ +#define _IRC_H_ + #include +#include #include #include @@ -541,3 +545,5 @@ int irc_read_conf(char *file) { return 1; } + +#endif //_IRC_H_ diff --git a/src/map/mercenary.c b/src/map/mercenary.c index cb502d8af..f7799e500 100644 --- a/src/map/mercenary.c +++ b/src/map/mercenary.c @@ -5,6 +5,7 @@ #include #include "../common/cbasetypes.h" +#include "../common/malloc.h" #include "../common/socket.h" #include "../common/timer.h" #include "../common/nullpo.h" -- cgit v1.2.3-60-g2f50