diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/core.c | 6 | ||||
-rw-r--r-- | src/common/socket.c | 4 | ||||
-rw-r--r-- | src/common/utils.c | 99 | ||||
-rw-r--r-- | src/common/utils.h | 21 |
4 files changed, 124 insertions, 6 deletions
diff --git a/src/common/core.c b/src/common/core.c index c53d2243b..d2f662551 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -18,6 +18,7 @@ #include "../common/sql.h" #include "../config/core.h" #include "../common/HPM.h" + #include "../common/utils.h" #endif #include <stdio.h> @@ -281,6 +282,7 @@ void usercheck(void) { void core_defaults(void) { #ifndef MINICORE hpm_defaults(); + HCache_defaults(); #endif console_defaults(); strlib_defaults(); @@ -332,7 +334,9 @@ int main (int argc, char **argv) { iTimer->init(); console->init(); - + + HCache->init(); + #ifndef MINICORE HPM->init(); #endif diff --git a/src/common/socket.c b/src/common/socket.c index a039006f0..ea8a2cfcc 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -592,6 +592,7 @@ static int create_session(int fd, RecvFunc func_recv, SendFunc func_send, ParseF session[fd]->func_send = func_send; session[fd]->func_parse = func_parse; session[fd]->rdata_tick = last_tick; + session[fd]->session_data = NULL; return 0; } @@ -605,7 +606,8 @@ static void delete_session(int fd) #endif aFree(session[fd]->rdata); aFree(session[fd]->wdata); - aFree(session[fd]->session_data); + if( session[fd]->session_data ) + aFree(session[fd]->session_data); aFree(session[fd]); session[fd] = NULL; } diff --git a/src/common/utils.c b/src/common/utils.c index 296df7e70..fd332253d 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1,10 +1,12 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder +// Copyright (c) Hercules Dev Team, licensed under GNU GPL. +// See the LICENSE file +// Portions Copyright (c) Athena Dev Teams #include "../common/cbasetypes.h" #include "../common/mmo.h" #include "../common/malloc.h" #include "../common/showmsg.h" +#include "../common/core.h" #include "socket.h" #include "utils.h" @@ -25,6 +27,9 @@ #include <sys/stat.h> #endif +#include <sys/stat.h> // cache purposes [Ind/Hercules] + +struct HCache_interface HCache_s; /// Dumps given buffer into file pointed to by a handle. void WriteDump(FILE* fp, const void* buffer, size_t length) @@ -281,3 +286,93 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B) return (unsigned int)floor(result); } + +/* [Ind/Hercules] Caching */ +bool HCache_check(const char *file) { + struct stat bufa, bufb; + FILE *first, *second; + char s_path[255], dT[1]; + time_t rtime; + + if( !(first = fopen(file,"rb")) ) + return false; + + if( file[0] == '.' && file[1] == '/' ) + file += 2; + else if( file[0] == '.' ) + file++; + + snprintf(s_path, 255, "./cache/%s", file); + + if( !(second = fopen(s_path,"rb")) ) { + fclose(first); + return false; + } + + if( fread(dT,sizeof(dT),1,second) != 1 || fread(&rtime,sizeof(rtime),1,second) != 1 || dT[0] != 'k' || HCache->recompile_time > rtime ) { + fclose(first); + fclose(second); + return false; + } + + fstat(fileno(first), &bufa); + fstat(fileno(first), &bufb); + + fclose(first); + fclose(second); + + if( bufa.st_mtime > bufb.st_mtime ) + return false; + + return true; +} + +FILE *HCache_open(const char *file, const char *opt) { + FILE *first; + char s_path[255]; + + if( file[0] == '.' && file[1] == '/' ) + file += 2; + else if( file[0] == '.' ) + file++; + + snprintf(s_path, 255, "./cache/%s", file); + + if( !(first = fopen(s_path,opt)) ) { + return NULL; + } + + if( opt[0] != 'r' ) { + char dT[1] = "k";/* 1-byte key to ensure our method is the latest, we can modify to ensure the method matches */ + fwrite(dT,sizeof(dT),1,first); + fwrite(&HCache->recompile_time,sizeof(HCache->recompile_time),1,first); + } + fseek(first, 20, SEEK_SET);/* skip first 20, might wanna store something else later */ + + return first; +} +void HCache_init(void) { + FILE *server; + + if( (server = fopen(SERVER_NAME,"rb")) ) { + struct stat buf; + + fstat(fileno(server), &buf); + HCache->recompile_time = buf.st_mtime; + fclose(server); + + HCache->enabled = true; + } else + ShowWarning("Unable to open '%s', caching capabilities have been disabled!\n",SERVER_NAME); +} +void HCache_defaults(void) { + + HCache = &HCache_s; + + HCache->init = HCache_init; + + HCache->check = HCache_check; + HCache->open = HCache_open; + HCache->recompile_time = 0; + HCache->enabled = false; +} diff --git a/src/common/utils.h b/src/common/utils.h index 8e39f2655..d245f94e2 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -1,11 +1,13 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder +// Copyright (c) Hercules Dev Team, licensed under GNU GPL. +// See the LICENSE file +// Portions Copyright (c) Athena Dev Teams #ifndef _UTILS_H_ #define _UTILS_H_ #include "../common/cbasetypes.h" #include <stdio.h> // FILE* +#include <time.h> // generate a hex dump of the first 'length' bytes of 'buffer' void WriteDump(FILE* fp, const void* buffer, size_t length); @@ -29,4 +31,19 @@ extern uint16 GetWord(uint32 val, int idx); extern uint16 MakeWord(uint8 byte0, uint8 byte1); extern uint32 MakeDWord(uint16 word0, uint16 word1); +/* [Ind/Hercules] Caching */ +struct HCache_interface { + void (*init) (void); + /* */ + bool (*check) (const char *file); + FILE *(*open) (const char *file, const char *opt); + /* */ + time_t recompile_time; + bool enabled; +}; + +struct HCache_interface *HCache; + +void HCache_defaults(void); + #endif /* _UTILS_H_ */ |