1
0
Fork 0

WIP: rewrite the network stack to use libevent

This commit is contained in:
Exynox 2022-03-09 21:30:51 +02:00
parent d2f43a8620
commit 64596d344c
43 changed files with 415 additions and 2149 deletions

View File

@ -4,8 +4,8 @@
#define WORD_MAX 0xffff
enum EMisc
{
MAX_HOST_LENGTH = 15,
IP_ADDRESS_LENGTH = 15,
MAX_HOST_LENGTH = 253,
IP_ADDRESS_LENGTH = INET6_ADDRSTRLEN > INET_ADDRSTRLEN ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
LOGIN_MAX_LEN = 30,
PASSWD_MAX_LEN = 16,
PLAYER_PER_ACCOUNT = 4,

View File

@ -4,5 +4,4 @@
#define _IMPROVED_PACKET_ENCRYPTION_ // 패킷 암호화 개선
//#define __AUCTION__
#define __PET_SYSTEM__
#define __UDP_BLOCK__
#endif

View File

@ -11,27 +11,20 @@ include_directories(${PROJECT_BINARY_DIR}/src/)
include_directories(src/)
# Find dependencies
find_package(libmysql REQUIRED)
find_package(Boost REQUIRED)
add_executable(${PROJECT_NAME} ${sources})
# Link dependencies if found
if (libmysql_FOUND)
target_link_libraries (${PROJECT_NAME} ${MYSQL_LIBRARIES})
endif (libmysql_FOUND)
# Find dependencies
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES} ${Boost_SYSTEM_LIBRARY})
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} ${Boost_LIBRARIES} ${Boost_SYSTEM_LIBRARY})
endif (Boost_FOUND)
# Pthreads
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
# Pthreads
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package (Threads REQUIRED)
target_link_libraries (${PROJECT_NAME} Threads::Threads)
endif (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
# Libevent
find_package(Libevent CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE libevent::core libevent::extra libevent::pthreads)
target_link_libraries(${PROJECT_NAME} libpoly libsql libthecore)
target_link_libraries(${PROJECT_NAME} PRIVATE libpoly libsql libthecore)

View File

@ -1,4 +1,3 @@
#include "stdafx.h"
#include <common/billing.h>
@ -41,6 +40,71 @@ CPacketInfo g_item_info;
int g_item_count = 0;
int g_query_count[2];
static void AcceptConnection(
evconnlistener* listener,
evutil_socket_t fd,
sockaddr* address,
int socklen,
void* ctx)
{
// We got a new connection! We have to create a new peer.
auto* clientManager = (CClientManager*) ctx;
// Create a new buffer for the peer
event_base *base = evconnlistener_get_base(listener);
bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
// Create a new peer
CPeer* peer = clientManager->AddPeer(bev, address);
// Set the event handlers for this peer
bufferevent_setcb(bev, ReadHandler, WriteHandler, EventHandler, peer);
// Enable the events
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
static void AcceptError(evconnlistener *listener, void *ctx) {
struct event_base *base = evconnlistener_get_base(listener);
int err = EVUTIL_SOCKET_ERROR();
fprintf(stderr, "Got an error %d (%s) on the listener. "
"Shutting down.\n", err, evutil_socket_error_to_string(err));
event_base_loopexit(base, NULL);
}
static void ReadHandler(bufferevent *bev, void *ctx) {
auto* peer = (CPeer*) ctx;
if (peer == CClientManager::Instance().GetAuthPeer())
if (g_log)
sys_log(0, "AUTH_PEER_READ: size %d", peer->GetRecvLength());
CClientManager::Instance().ProcessPackets(peer);
}
static void WriteHandler(bufferevent *bev, void *ctx) {
auto* peer = (CPeer*) ctx;
if (peer == CClientManager::Instance().GetAuthPeer())
if (g_log)
sys_log(0, "AUTH_PEER_WRITE: size %d", peer->GetSendLength());
}
static void EventHandler(bufferevent *bev, short events, void *ctx) {
auto* peer = (CPeer*) ctx;
if (events & BEV_EVENT_ERROR)
sys_err("PEER libevent error, handle: %d", peer->GetHandle());
if (events & BEV_EVENT_EOF)
sys_log(0, "PEER disconnected: handle %d", peer->GetHandle());
// Either the socket was closed or an error occured, therefore we can disconnect this peer.
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR))
CClientManager::Instance().RemovePeer(peer);
}
CClientManager::CClientManager() :
m_pkAuthPeer(NULL),
m_iPlayerIDStart(0),
@ -51,7 +115,7 @@ CClientManager::CClientManager() :
m_pShopTable(NULL),
m_iRefineTableSize(0),
m_pRefineTable(NULL),
m_bShutdowned(FALSE),
m_bShutdowned(false),
m_iCacheFlushCount(0),
m_iCacheFlushCountLimit(200)
{
@ -75,16 +139,22 @@ void CClientManager::SetPlayerIDStart(int iIDStart)
void CClientManager::Destroy()
{
m_mChannelStatus.clear();
for (itertype(m_peerList) i = m_peerList.begin(); i != m_peerList.end(); ++i)
(*i)->Destroy();
// Close the peer connections and empty the peer list
for (auto &peer: m_peerList)
peer->Destroy();
m_peerList.clear();
if (m_fdAccept > 0)
{
socket_close(m_fdAccept);
m_fdAccept = -1;
}
// Free the libevent resources
if (m_listener) {
evconnlistener_free(m_listener);
m_listener = nullptr;
}
if (m_base) {
event_base_free(m_base);
m_base = nullptr;
}
}
bool CClientManager::Initialize()
@ -124,16 +194,33 @@ bool CClientManager::Initialize()
if (!CConfig::instance().GetValue("BIND_IP", szBindIP, 128))
strncpy(szBindIP, "0", sizeof(szBindIP));
m_fdAccept = socket_tcp_bind(szBindIP, tmpValue);
// Create a new libevent base and listen for new connections
event_enable_debug_mode();
m_base = event_base_new();
if (!m_base) {
sys_err("Libevent base initialization FAILED!");
return false;
}
if (m_fdAccept < 0)
{
perror("socket");
return false;
}
sockaddr_in sin = {};
sys_log(0, "ACCEPT_HANDLE: %u", m_fdAccept);
fdwatch_add_fd(m_fdWatcher, m_fdAccept, NULL, FDW_READ, false);
/* This is an INET address */
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(szBindIP);
sin.sin_port = htons(tmpValue);
m_listener = evconnlistener_new_bind(
m_base,
AcceptConnection, this,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1,
(const sockaddr*)&sin, sizeof(sin)
);
if (!m_listener) {
sys_err("Libevent listener initialization FAILED!");
return false;
}
evconnlistener_set_error_cb(m_listener, AcceptError);
if (!CConfig::instance().GetValue("BACKUP_LIMIT_SEC", &tmpValue))
tmpValue = 600;
@ -242,7 +329,7 @@ void CClientManager::MainLoop()
void CClientManager::Quit()
{
m_bShutdowned = TRUE;
m_bShutdowned = true;
}
void CClientManager::QUERY_BOOT(CPeer* peer, TPacketGDBoot * p)
@ -2745,14 +2832,17 @@ void CClientManager::ProcessPackets(CPeer * peer)
peer->RecvEnd(i);
}
void CClientManager::AddPeer(socket_t fd)
CPeer * CClientManager::AddPeer(bufferevent* bufev, sockaddr* addr)
{
CPeer * pPeer = new CPeer;
auto* pPeer = new CPeer;
if (pPeer->Accept(fd))
m_peerList.push_front(pPeer);
else
delete pPeer;
if (!pPeer->Accept(bufev, addr)) {
delete pPeer;
return nullptr;
}
m_peerList.push_front(pPeer);
return pPeer;
}
void CClientManager::RemovePeer(CPeer * pPeer)
@ -2807,6 +2897,11 @@ CPeer * CClientManager::GetPeer(IDENT ident)
return NULL;
}
CPeer * CClientManager::GetAuthPeer()
{
return m_pkAuthPeer;
}
CPeer * CClientManager::GetAnyPeer()
{
if (m_peerList.empty())
@ -3132,83 +3227,8 @@ int CClientManager::Process()
}
}
int num_events = fdwatch(m_fdWatcher, 0);
int idx;
CPeer * peer;
for (idx = 0; idx < num_events; ++idx) // ÀÎDz
{
peer = (CPeer *) fdwatch_get_client_data(m_fdWatcher, idx);
if (!peer)
{
if (fdwatch_check_event(m_fdWatcher, m_fdAccept, idx) == FDW_READ)
{
AddPeer(m_fdAccept);
fdwatch_clear_event(m_fdWatcher, m_fdAccept, idx);
}
else
{
sys_err("FDWATCH: peer null in event: ident %d", fdwatch_get_ident(m_fdWatcher, idx));
}
continue;
}
switch (fdwatch_check_event(m_fdWatcher, peer->GetFd(), idx))
{
case FDW_READ:
if (peer->Recv() < 0)
{
sys_err("Recv failed");
RemovePeer(peer);
}
else
{
if (peer == m_pkAuthPeer)
if (g_log)
sys_log(0, "AUTH_PEER_READ: size %d", peer->GetRecvLength());
ProcessPackets(peer);
}
break;
case FDW_WRITE:
if (peer == m_pkAuthPeer)
if (g_log)
sys_log(0, "AUTH_PEER_WRITE: size %d", peer->GetSendLength());
if (peer->Send() < 0)
{
sys_err("Send failed");
RemovePeer(peer);
}
break;
case FDW_EOF:
RemovePeer(peer);
break;
default:
sys_err("fdwatch_check_fd returned unknown result");
RemovePeer(peer);
break;
}
}
#ifdef __WIN32__
if (_kbhit()) {
int c = _getch();
switch (c) {
case 0x1b: // Esc
return 0; // shutdown
break;
default:
break;
}
}
#endif
// Process network events
event_base_loop(m_base, EVLOOP_NONBLOCK);
VCardProcess();
return 1;

View File

@ -1,10 +1,12 @@
// vim:ts=8 sw=4
#ifndef __INC_CLIENTMANAGER_H__
#define __INC_CLIENTMANAGER_H__
#include <unordered_map>
#include <unordered_set>
#include <event2/event.h>
#include <event2/listener.h>
#include <common/stl.h>
#include <common/building.h>
#include <common/auction_table.h>
@ -28,7 +30,13 @@ class CPacketInfo
size_t CreatePlayerSaveQuery(char * pszQuery, size_t querySize, TPlayerTable * pkTab);
class CClientManager : public CNetBase, public singleton<CClientManager>
static void AcceptConnection(evconnlistener *listener, evutil_socket_t fd, sockaddr *address, int socklen, void *ctx);
static void AcceptError(evconnlistener *listener, void *ctx);
static void ReadHandler(bufferevent *bev, void *ctx);
static void WriteHandler(bufferevent *bev, void *ctx);
static void EventHandler(bufferevent *bev, short events, void *ctx);
class CClientManager : public singleton<CClientManager>
{
public:
typedef std::list<CPeer *> TPeerList;
@ -165,6 +173,13 @@ class CClientManager : public CNetBase, public singleton<CClientManager>
char* GetCommand(char* str); //독일선물기능에서 명령어 얻는 함수
void ItemAward(CPeer * peer, char* login); //독일 선물 기능
CPeer * AddPeer(bufferevent* bufev, sockaddr* addr);
void RemovePeer(CPeer * pPeer);
CPeer * GetPeer(IDENT ident);
CPeer * GetAuthPeer();
void ProcessPackets(CPeer * peer);
protected:
void Destroy();
@ -190,17 +205,11 @@ class CClientManager : public CNetBase, public singleton<CClientManager>
bool MirrorMobTableIntoDB();
bool MirrorItemTableIntoDB();
void AddPeer(socket_t fd);
void RemovePeer(CPeer * pPeer);
CPeer * GetPeer(IDENT ident);
int AnalyzeQueryResult(SQLMsg * msg);
int AnalyzeErrorMsg(CPeer * peer, SQLMsg * msg);
int Process();
void ProcessPackets(CPeer * peer);
CLoginData * GetLoginData(DWORD dwKey);
CLoginData * GetLoginDataByLogin(const char * c_pszLogin);
CLoginData * GetLoginDataByAID(DWORD dwAID);
@ -386,7 +395,8 @@ class CClientManager : public CNetBase, public singleton<CClientManager>
private:
int m_looping;
socket_t m_fdAccept; // 접속 받는 소켓
event_base * m_base;
evconnlistener * m_listener;
TPeerList m_peerList;
CPeer * m_pkAuthPeer;

View File

@ -5,8 +5,6 @@
// 디비 커넥션 클래스의 목적은... 디비에 접속해서 쿼리보내고 결과 받아오는
// 모든 일들을 처리한다.
// 코드 by 꼬붕 후로그래머 아노아~ = _=)b
#include <mysql/mysql.h>
#include <libsql/include/CAsyncSQL.h>
#define SQL_SAFE_LENGTH(size) (size * 2 + 1)

View File

@ -70,7 +70,6 @@ int main()
#endif
CConfig Config;
CNetPoller poller;
CDBManager DBManager;
CClientManager ClientManager;
PlayerHB player_hb;
@ -370,12 +369,6 @@ int Start()
return false;
}
if (!CNetPoller::instance().Create())
{
sys_err("Cannot create network poller");
return false;
}
sys_log(0, "ClientManager initialization.. ");
if (!CClientManager::instance().Initialize())

View File

@ -1,54 +0,0 @@
#include "stdafx.h"
#include "NetBase.h"
#include "Config.h"
#include "ClientManager.h"
LPFDWATCH CNetBase::m_fdWatcher = NULL;
CNetBase::CNetBase()
{
}
CNetBase::~CNetBase()
{
}
CNetPoller::CNetPoller()
{
}
CNetPoller::~CNetPoller()
{
Destroy();
}
bool CNetPoller::Create()
{
sys_log(1, "NetPoller::Create()");
if (m_fdWatcher)
return true;
m_fdWatcher = fdwatch_new(512);
if (!m_fdWatcher)
{
Destroy();
return false;
}
return true;
}
void CNetPoller::Destroy()
{
sys_log(1, "NetPoller::Destroy()");
if (m_fdWatcher)
{
fdwatch_delete(m_fdWatcher);
m_fdWatcher = NULL;
}
thecore_destroy();
}

View File

@ -1,25 +0,0 @@
// vim: ts=8 sw=4
#ifndef __INC_NETWORKBASE_H__
#define __INC_NETWORKBASE_H__
class CNetBase
{
public:
CNetBase();
virtual ~CNetBase();
protected:
static LPFDWATCH m_fdWatcher;
};
class CNetPoller : public CNetBase, public singleton<CNetPoller>
{
public:
CNetPoller();
virtual ~CNetPoller();
bool Create();
void Destroy();
};
#endif

View File

@ -29,13 +29,7 @@ void CPeer::OnAccept()
static DWORD current_handle = 0;
m_dwHandle = ++current_handle;
sys_log(0, "Connection accepted. (host: %s handle: %u fd: %d)", m_host, m_dwHandle, m_fd);
}
void CPeer::OnConnect()
{
sys_log(0, "Connection established. (host: %s handle: %u fd: %d)", m_host, m_dwHandle, m_fd);
m_state = STATE_PLAYING;
sys_log(0, "Connection accepted. (host: %s handle: %u)", m_host, m_dwHandle);
}
void CPeer::OnClose()
@ -72,7 +66,7 @@ bool CPeer::PeekPacket(int & iBytesProceed, BYTE & header, DWORD & dwHandle, DWO
if (GetRecvLength() < iBytesProceed + 9)
return false;
const char * buf = (const char *) GetRecvBuffer();
const char * buf = (const char *) GetRecvBuffer(iBytesProceed + 9);
buf += iBytesProceed;
header = *(buf++);
@ -114,14 +108,6 @@ void CPeer::EncodeReturn(BYTE header, DWORD dwHandle)
EncodeHeader(header, dwHandle, 0);
}
int CPeer::Send()
{
if (m_state == STATE_CLOSE)
return -1;
return (CPeerBase::Send());
}
void CPeer::SetP2PPort(WORD wPort)
{
m_wP2PPort = wPort;

View File

@ -9,7 +9,6 @@ class CPeer : public CPeerBase
protected:
virtual void OnAccept();
virtual void OnClose();
virtual void OnConnect();
public:
#pragma pack(1)
@ -34,7 +33,6 @@ class CPeer : public CPeerBase
void EncodeReturn(BYTE header, DWORD dwHandle);
void ProcessInput();
int Send();
DWORD GetHandle();
DWORD GetUserCount();

View File

@ -1,7 +1,9 @@
#include "stdafx.h"
#include "PeerBase.h"
CPeerBase::CPeerBase() : m_fd(INVALID_SOCKET), m_BytesRemain(0), m_outBuffer(NULL), m_inBuffer(NULL)
#include <event2/buffer.h>
CPeerBase::CPeerBase() : m_bufferevent(nullptr)
{
}
@ -10,83 +12,53 @@ CPeerBase::~CPeerBase()
Destroy();
}
void CPeerBase::Disconnect()
{
if (m_fd != INVALID_SOCKET)
{
fdwatch_del_fd(m_fdWatcher, m_fd);
socket_close(m_fd);
m_fd = INVALID_SOCKET;
}
}
void CPeerBase::Destroy()
{
Disconnect();
if (m_outBuffer)
{
buffer_delete(m_outBuffer);
m_outBuffer = NULL;
}
if (m_inBuffer)
{
buffer_delete(m_inBuffer);
m_inBuffer = NULL;
}
if (m_bufferevent) {
bufferevent_free(m_bufferevent);
m_bufferevent = nullptr;
}
}
bool CPeerBase::Accept(socket_t fd_accept)
bool CPeerBase::Accept(bufferevent* bufev, sockaddr* addr)
{
struct sockaddr_in peer;
if (!bufev) {
sys_err("Cannot accept empty bufferevent!");
return false;
}
if ((m_fd = socket_accept(fd_accept, &peer)) == INVALID_SOCKET)
{
Destroy();
return false;
}
if (m_bufferevent != nullptr) {
sys_err("Peer is already initialized");
return false;
}
//socket_block(m_fd);
socket_sndbuf(m_fd, 233016);
socket_rcvbuf(m_fd, 233016);
// Save the bufferevent
m_bufferevent = bufev;
strncpy(m_host, inet_ntoa(peer.sin_addr), sizeof(m_host));
m_outBuffer = buffer_new(DEFAULT_PACKET_BUFFER_SIZE);
m_inBuffer = buffer_new(MAX_INPUT_LEN);
// Get the address of the conected peer
sockaddr_in* peer;
sockaddr_in6* peer6;
if (!m_outBuffer || !m_inBuffer)
{
Destroy();
return false;
}
switch (addr->sa_family) {
case AF_INET:
peer = (sockaddr_in*) addr;
inet_ntop(AF_INET, &(peer->sin_addr), m_host, INET_ADDRSTRLEN);
break;
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_READ, false);
case AF_INET6:
peer6 = (sockaddr_in6*) addr;
inet_ntop(AF_INET, &(peer6->sin6_addr), m_host, INET6_ADDRSTRLEN);
break;
default:
break;
}
// Trigger the OnAccept event
OnAccept();
sys_log(0, "ACCEPT FROM %s", inet_ntoa(peer.sin_addr));
return true;
}
bool CPeerBase::Connect(const char* host, WORD port)
{
strncpy(m_host, host, sizeof(m_host));
sys_log(0, "ACCEPT FROM %s", m_host);
if ((m_fd = socket_connect(host, port)) == INVALID_SOCKET)
return false;
m_outBuffer = buffer_new(DEFAULT_PACKET_BUFFER_SIZE);
if (!m_outBuffer)
{
Destroy();
return false;
}
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_READ, false);
OnConnect();
return true;
}
@ -97,118 +69,77 @@ void CPeerBase::Close()
void CPeerBase::EncodeBYTE(BYTE b)
{
if (!m_outBuffer)
{
sys_err("Not ready to write");
return;
}
buffer_write(m_outBuffer, &b, 1);
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_WRITE, true);
Encode(&b, sizeof(b));
}
void CPeerBase::EncodeWORD(WORD w)
{
if (!m_outBuffer)
{
sys_err("Not ready to write");
return;
}
buffer_write(m_outBuffer, &w, 2);
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_WRITE, true);
Encode(&w, sizeof(w));
}
void CPeerBase::EncodeDWORD(DWORD dw)
{
if (!m_outBuffer)
Encode(&dw, sizeof(dw));
}
void CPeerBase::Encode(const void* data, size_t size)
{
if (!m_bufferevent)
{
sys_err("Not ready to write");
sys_err("Bufferevent not ready!");
return;
}
buffer_write(m_outBuffer, &dw, 4);
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_WRITE, true);
if(bufferevent_write(m_bufferevent, data, size) != 0) {
sys_err("Buffer write error!");
return;
}
}
void CPeerBase::Encode(const void* data, DWORD size)
void CPeerBase::RecvEnd(size_t proceed_bytes)
{
if (!m_outBuffer)
{
sys_err("Not ready to write");
return;
}
if (!m_bufferevent)
{
sys_err("Bufferevent not ready!");
return;
}
buffer_write(m_outBuffer, data, size);
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_WRITE, true);
evbuffer *input = bufferevent_get_input(m_bufferevent);
evbuffer_drain(input, proceed_bytes);
}
int CPeerBase::Recv()
size_t CPeerBase::GetRecvLength()
{
if (!m_inBuffer)
{
sys_err("input buffer nil");
return -1;
}
if (!m_bufferevent)
{
sys_err("Bufferevent not ready!");
return 0;
}
buffer_adjust_size(m_inBuffer, MAX_INPUT_LEN >> 2);
int bytes_to_read = buffer_has_space(m_inBuffer);
ssize_t bytes_read = socket_read(m_fd, (char *) buffer_write_peek(m_inBuffer), bytes_to_read);
if (bytes_read < 0)
{
sys_err("socket_read failed %s", strerror(errno));
return -1;
}
else if (bytes_read == 0)
return 0;
buffer_write_proceed(m_inBuffer, bytes_read);
m_BytesRemain = buffer_size(m_inBuffer);
return 1;
evbuffer *input = bufferevent_get_input(m_bufferevent);
return evbuffer_get_length(input);
}
void CPeerBase::RecvEnd(int proceed_bytes)
const void * CPeerBase::GetRecvBuffer(ssize_t ensure_bytes)
{
buffer_read_proceed(m_inBuffer, proceed_bytes);
m_BytesRemain = buffer_size(m_inBuffer);
if (!m_bufferevent)
{
sys_err("Bufferevent not ready!");
return nullptr;
}
evbuffer *input = bufferevent_get_input(m_bufferevent);
return evbuffer_pullup(input, ensure_bytes);
}
int CPeerBase::GetRecvLength()
size_t CPeerBase::GetSendLength()
{
return m_BytesRemain;
}
const void * CPeerBase::GetRecvBuffer()
{
return buffer_read_peek(m_inBuffer);
}
int CPeerBase::GetSendLength()
{
return buffer_size(m_outBuffer);
}
int CPeerBase::Send()
{
if (buffer_size(m_outBuffer) <= 0)
return 0;
int iBufferLeft = fdwatch_get_buffer_size(m_fdWatcher, m_fd);
int iBytesToWrite = MIN(iBufferLeft, buffer_size(m_outBuffer));
if (iBytesToWrite == 0)
return 0;
int result = socket_write(m_fd, (const char *) buffer_read_peek(m_outBuffer), iBytesToWrite);
if (result == 0)
{
buffer_read_proceed(m_outBuffer, iBytesToWrite);
if (buffer_size(m_outBuffer) != 0)
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_WRITE, true);
}
return (result);
if (!m_bufferevent)
{
sys_err("Bufferevent not ready!");
return 0;
}
evbuffer *output = bufferevent_get_output(m_bufferevent);
return evbuffer_get_length(output);
}

View File

@ -1,61 +1,40 @@
// vim: ts=8 sw=4
#ifndef __INC_PEERBASE_H__
#define __INC_PEERBASE_H__
#include "NetBase.h"
#include <event2/bufferevent.h>
class CPeerBase : public CNetBase
{
public:
enum
{
MAX_HOST_LENGTH = 30,
MAX_INPUT_LEN = 1024 * 1024 * 2,
DEFAULT_PACKET_BUFFER_SIZE = 1024 * 1024 * 2
};
protected:
class CPeerBase {
protected:
virtual void OnAccept() = 0;
virtual void OnConnect() = 0;
virtual void OnClose() = 0;
public:
bool Accept(socket_t accept_fd);
bool Connect(const char* host, WORD port);
void Close();
public:
bool Accept(bufferevent* bufev, sockaddr* addr);
void Close();
public:
public:
CPeerBase();
virtual ~CPeerBase();
void Disconnect();
void Destroy();
void Destroy();
socket_t GetFd() { return m_fd; }
bufferevent * GetBufferevent() { return m_bufferevent; }
void EncodeBYTE(BYTE b);
void EncodeWORD(WORD w);
void EncodeDWORD(DWORD dw);
void Encode(const void* data, DWORD size);
int Send();
void EncodeBYTE(BYTE b);
void EncodeWORD(WORD w);
void EncodeDWORD(DWORD dw);
void Encode(const void* data, size_t size);
void RecvEnd(size_t proceed_bytes);
size_t GetRecvLength();
const void * GetRecvBuffer(ssize_t ensure_bytes);
int Recv();
void RecvEnd(int proceed_bytes);
int GetRecvLength();
const void * GetRecvBuffer();
int GetSendLength();
size_t GetSendLength();
const char * GetHost() { return m_host; }
protected:
char m_host[MAX_HOST_LENGTH + 1];
socket_t m_fd;
private:
int m_BytesRemain;
LPBUFFER m_outBuffer;
LPBUFFER m_inBuffer;
protected:
char m_host[IP_ADDRESS_LENGTH + 1];
bufferevent * m_bufferevent;
};
#endif

View File

@ -2047,7 +2047,7 @@ bool CHARACTER::Damage(LPCHARACTER pAttacker, int dam, EDamageType type) // retu
}
else
{
// 정신력이 모자라서 피가 더 깍여야할
// 정신력이 모자라서 피가 더 깍여야할??
PointChange(POINT_SP, -GetSP());
dam -= iSP * 100 / MAX(GetPoint(POINT_MANASHIELD), 1);
}
@ -3132,7 +3132,7 @@ class CFuncShoot
break;
}
m_bSucceed = TRUE;
m_bSucceed = true;
}
};

View File

@ -2282,7 +2282,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_HP, item->GetValue(0) * (100 + GetPoint(POINT_POTION_BONUS)) / 100);
EffectPacket(SE_HPUP_RED);
used = TRUE;
used = true;
}
}
@ -2292,7 +2292,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_SP, item->GetValue(1) * (100 + GetPoint(POINT_POTION_BONUS)) / 100);
EffectPacket(SE_SPUP_BLUE);
used = TRUE;
used = true;
}
}
@ -2302,7 +2302,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_HP, item->GetValue(3) * GetMaxHP() / 100);
EffectPacket(SE_HPUP_RED);
used = TRUE;
used = true;
}
}
@ -2312,7 +2312,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_SP, item->GetValue(4) * GetMaxSP() / 100);
EffectPacket(SE_SPUP_BLUE);
used = TRUE;
used = true;
}
}
@ -4133,7 +4133,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_HP, item->GetValue(0) * (100 + GetPoint(POINT_POTION_BONUS)) / 100);
EffectPacket(SE_HPUP_RED);
used = TRUE;
used = true;
}
}
@ -4143,7 +4143,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_SP, item->GetValue(1) * (100 + GetPoint(POINT_POTION_BONUS)) / 100);
EffectPacket(SE_SPUP_BLUE);
used = TRUE;
used = true;
}
}
@ -4153,7 +4153,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_HP, item->GetValue(3) * GetMaxHP() / 100);
EffectPacket(SE_HPUP_RED);
used = TRUE;
used = true;
}
}
@ -4163,7 +4163,7 @@ bool CHARACTER::UseItemEx(LPITEM item, TItemPos DestCell)
{
PointChange(POINT_SP, item->GetValue(4) * GetMaxSP() / 100);
EffectPacket(SE_SPUP_BLUE);
used = TRUE;
used = true;
}
}

View File

@ -61,7 +61,6 @@ void DESC::Initialize()
memset( &m_accountTable, 0, sizeof(m_accountTable) );
memset( &m_SockAddr, 0, sizeof(m_SockAddr) );
memset( &m_UDPSockAddr, 0, sizeof(m_UDPSockAddr) );
m_pLogFile = NULL;
@ -582,19 +581,6 @@ void DESC::BindAccountTable(TAccountTable * pAccountTable)
DESC_MANAGER::instance().ConnectAccount(m_accountTable.login, this);
}
void DESC::UDPGrant(const struct sockaddr_in & c_rSockAddr)
{
m_UDPSockAddr = c_rSockAddr;
TPacketGCBindUDP pack;
pack.header = HEADER_GC_BINDUDP;
pack.addr = m_UDPSockAddr.sin_addr.s_addr;
pack.port = m_UDPSockAddr.sin_port;
Packet(&pack, sizeof(TPacketGCBindUDP));
}
void DESC::Log(const char * format, ...)
{
if (!m_pLogFile)

View File

@ -111,9 +111,6 @@ class DESC
const struct sockaddr_in & GetAddr() { return m_SockAddr; }
void UDPGrant(const struct sockaddr_in & c_rSockAddr);
const struct sockaddr_in & GetUDPAddr() { return m_UDPSockAddr; }
void Log(const char * format, ...);
// 핸드쉐이크 (시간 동기화)
@ -222,7 +219,6 @@ class DESC
TAccountTable m_accountTable;
struct sockaddr_in m_SockAddr;
struct sockaddr_in m_UDPSockAddr;
FILE * m_pLogFile;
std::string m_stRelayName;

View File

@ -41,7 +41,7 @@ int IsValidIP(struct valid_ip* ip_table, const char *host)
snprintf(ip_addr, sizeof(ip_addr), "%s.%d", (ip_table + i)->ip, (ip_table + i)->network + j);
if (!strcmp(ip_addr, host))
return TRUE;
return true;
if (!j)
break;
@ -49,7 +49,7 @@ int IsValidIP(struct valid_ip* ip_table, const char *host)
while (j--);
}
return FALSE;
return false;
}
DESC_MANAGER::DESC_MANAGER() : m_bDestroyed(false)

View File

@ -38,8 +38,8 @@ LPEVENT event_create_ex(TEVENTFUNC func, event_info_data* info, long when)
new_event->func = func;
new_event->info = info;
new_event->q_el = cxx_q.Enqueue(new_event, when, thecore_heart->pulse);
new_event->is_processing = FALSE;
new_event->is_force_to_end = FALSE;
new_event->is_processing = false;
new_event->is_force_to_end = false;
return (new_event);
}
@ -60,10 +60,10 @@ void event_cancel(LPEVENT * ppevent)
if (event->is_processing)
{
event->is_force_to_end = TRUE;
event->is_force_to_end = true;
if (event->q_el)
event->q_el->bCancel = TRUE;
event->q_el->bCancel = true;
*ppevent = NULL;
return;
@ -82,7 +82,7 @@ void event_cancel(LPEVENT * ppevent)
return;
}
event->q_el->bCancel = TRUE;
event->q_el->bCancel = true;
*ppevent = NULL;
}
@ -92,7 +92,7 @@ void event_reset_time(LPEVENT event, long when)
if (!event->is_processing)
{
if (event->q_el)
event->q_el->bCancel = TRUE;
event->q_el->bCancel = true;
event->q_el = cxx_q.Enqueue(event, when, thecore_heart->pulse);
}
@ -127,7 +127,7 @@ int event_process(int pulse)
* 0 event
* .
*/
the_event->is_processing = TRUE;
the_event->is_processing = true;
if (!the_event->info)
{
@ -146,7 +146,7 @@ int event_process(int pulse)
else
{
the_event->q_el = cxx_q.Enqueue(the_event, new_time, pulse);
the_event->is_processing = FALSE;
the_event->is_processing = false;
}
}

View File

@ -39,7 +39,7 @@ TQueueElement * CEventQueue::Enqueue(LPEVENT pvData, int duration, int pulse)
pElem->pvData = pvData;
pElem->iStartTime = pulse;
pElem->iKey = duration + pulse;
pElem->bCancel = FALSE;
pElem->bCancel = false;
m_pq_queue.push(pElem);
return pElem;

View File

@ -116,7 +116,7 @@ template <typename T>
bool CGroupNode::GetValue(size_t i, const std::string & c_rstrColKey, T& tValue) const
{
if (i > m_map_rows.size())
return FALSE;
return false;
TMapRow::const_iterator row_it = m_map_rows.begin();
std::advance(row_it, i);
@ -124,13 +124,13 @@ bool CGroupNode::GetValue(size_t i, const std::string & c_rstrColKey, T& tValue)
itertype(m_map_columnNameToIndex) col_idx_it = m_map_columnNameToIndex.find(c_rstrColKey);
if (m_map_columnNameToIndex.end() == col_idx_it)
{
return FALSE;
return false;
}
int index = col_idx_it->second;
if (row_it->second.GetSize() <= index)
{
return FALSE;
return false;
}
return row_it->second.GetValue(index, tValue);
@ -142,18 +142,18 @@ bool CGroupNode::GetValue(const std::string & c_rstrRowKey, const std::string &
TMapRow::const_iterator row_it = m_map_rows.find(c_rstrRowKey);
if (m_map_rows.end() == row_it)
{
return FALSE;
return false;
}
itertype(m_map_columnNameToIndex) col_idx_it = m_map_columnNameToIndex.find(c_rstrColKey);
if (m_map_columnNameToIndex.end() == col_idx_it)
{
return FALSE;
return false;
}
int index = col_idx_it->second;
if (row_it->second.GetSize() <= index)
{
return FALSE;
return false;
}
return row_it->second.GetValue(index, tValue);
@ -165,12 +165,12 @@ bool CGroupNode::GetValue(const std::string & c_rstrRowKey, int index, T& tValue
TMapRow::const_iterator row_it = m_map_rows.find(c_rstrRowKey);
if (m_map_rows.end() == row_it)
{
return FALSE;
return false;
}
if (row_it->second.GetSize() <= index)
{
return FALSE;
return false;
}
return row_it->second.GetValue(index, tValue);
}

View File

@ -11,7 +11,6 @@ enum
INPROC_MAIN,
INPROC_DEAD,
INPROC_DB,
INPROC_UDP,
INPROC_P2P,
INPROC_AUTH,
INPROC_TEEN,
@ -297,27 +296,6 @@ protected:
DWORD m_dwHandle;
};
class CInputUDP : public CInputProcessor
{
public:
CInputUDP();
virtual bool Process(LPDESC d, const void * c_pvOrig, int iBytes, int & r_iBytesProceed);
virtual BYTE GetType() { return INPROC_UDP; }
void SetSockAddr(struct sockaddr_in & rSockAddr) { m_SockAddr = rSockAddr; };
protected:
virtual int Analyze(LPDESC d, BYTE bHeader, const char * c_pData);
protected:
void Handshake(LPDESC lpDesc, const char * c_pData);
void StateChecker(const char * c_pData);
protected:
struct sockaddr_in m_SockAddr;
CPacketInfoUDP m_packetInfoUDP;
};
class CInputP2P : public CInputProcessor
{
public:

View File

@ -1,194 +0,0 @@
#include "stdafx.h"
#include "constants.h"
#include "config.h"
#include "input.h"
#include "desc.h"
#include "desc_manager.h"
#include "item_manager.h"
#include "char_manager.h"
#include "protocol.h"
extern socket_t udp_socket;
#define HEADER_CG_STATE_CHECKER 1
#pragma pack(1)
typedef unsigned long ServerStateChecker_Key;
typedef unsigned long ServerStateChecker_Index;
typedef unsigned char ServerStateChecker_State;
struct ServerStateChecker_RequestPacket
{
BYTE header;
ServerStateChecker_Key key;
ServerStateChecker_Index index;
};
struct ServerStateChecker_ResponsePacket
{
BYTE header;
ServerStateChecker_Key key;
ServerStateChecker_Index index;
ServerStateChecker_State state;
};
#pragma pack()
///---------------------------------------------------------
CPacketInfoUDP::CPacketInfoUDP()
{
Set(1, sizeof(ServerStateChecker_RequestPacket), "ServerStateRequest", false);
}
CPacketInfoUDP::~CPacketInfoUDP()
{
Log("udp_packet_info.txt");
}
CInputUDP::CInputUDP()
{
memset( &m_SockAddr, 0, sizeof(m_SockAddr) );
BindPacketInfo(&m_packetInfoUDP);
}
void CInputUDP::Handshake(LPDESC pDesc, const char * c_pData)
{
TPacketCGHandshake * pInfo = (TPacketCGHandshake *) c_pData;
if (pDesc->GetHandshake() == pInfo->dwHandshake)
{
sys_log(0, "UDP: Grant %s:%d", inet_ntoa(m_SockAddr.sin_addr), m_SockAddr.sin_port);
pDesc->UDPGrant(m_SockAddr);
return;
}
else
sys_log(0, "UDP: Handshake differs %s", pDesc->GetHostName());
}
void CInputUDP::StateChecker(const char * c_pData)
{
// NOTE : TCP ¿¬°á·Î ¹Ù²Ù¸é¼­ »ç¿ë X
/*
struct ServerStateChecker_RequestPacket * p = (struct ServerStateChecker_RequestPacket *) c_pData;
ServerStateChecker_ResponsePacket rp;
int iTotal;
int * paiEmpireUserCount;
int iLocal;
DESC_MANAGER::instance().GetUserCount(iTotal, &paiEmpireUserCount, iLocal);
rp.header = 1;
rp.key = p->key;
rp.index = p->index;
if (g_bNoMoreClient)
rp.state = 0;
else
rp.state = iTotal > g_iFullUserCount ? 3 : iTotal > g_iBusyUserCount ? 2 : 1;
if (sendto(udp_socket, (const char*)&rp, sizeof(rp), 0, (const struct sockaddr *) &m_SockAddr, sizeof(m_SockAddr)) < 0)
{
sys_err("cannot sendto datagram socket : %s, %d", inet_ntoa(m_SockAddr.sin_addr), inet_ntoa(m_SockAddr.sin_addr));
return;
}
*/
}
int CInputUDP::Analyze(LPDESC pDesc, BYTE bHeader, const char * c_pData)
{
switch (bHeader)
{
/*
case HEADER_CG_HANDSHAKE:
Handshake(pDesc, c_pData);
break;
case HEADER_CG_STATE_CHECKER:
StateChecker(c_pData);
break;
*/
default:
sys_err("unknown UDP header %u", bHeader);
break;
}
return 0;
}
bool CInputUDP::Process(LPDESC pDesc, const void * c_pvOrig, int iBytes, int & r_iBytesProceed)
{
/*
const char * c_pData = static_cast<const char *> (c_pvOrig);
BYTE bLastHeader = 0;
int iLastPacketLen = 0;
int iPacketLen;
if (!m_pPacketInfo)
{
sys_err("No packet info has been binded to");
return true;
}
for (m_iBufferLeft = iBytes; m_iBufferLeft > 0;)
{
if (m_iBufferLeft < 5)
return true;
BYTE bHeader = (BYTE) *(c_pData);
const char * c_pszName;
if (!m_pPacketInfo->Get(bHeader, &iPacketLen, &c_pszName))
{
sys_err("UNKNOWN HEADER: %d, LAST HEADER: %d(%d), REMAIN BYTES: %d",
bHeader, bLastHeader, iLastPacketLen, m_iBufferLeft);
//printdata((BYTE *) c_pvOrig, iBytes);
return true;
}
//DWORD dwHandshake = *(DWORD *) (c_pData + 1);
//pDesc = DESC_MANAGER::instance().FindByHandshake(dwHandshake);
//if (!pDesc)
//{
//sys_err("No desc by handshake %u", dwHandshake);
//return true;
//}
//if (m_SockAddr.sin_addr.s_addr != pDesc->GetAddr().sin_addr.s_addr)
//{
//sys_err("Hostname Mismatch! %s != %s", inet_ntoa(m_SockAddr.sin_addr), pDesc->GetHostName());
//return true;
//}
if (m_iBufferLeft < iPacketLen)
return true;
int iExtraPacketSize = Analyze(pDesc, bHeader, c_pData);
if (iExtraPacketSize < 0)
return true;
iPacketLen += iExtraPacketSize;
c_pData += iPacketLen;
m_iBufferLeft -= iPacketLen;
r_iBytesProceed += iPacketLen;
iLastPacketLen = iPacketLen;
bLastHeader = bHeader;
//if (GetType() != pDesc->GetInputProcessor()->GetType())
//return false;
}
*/
return true;
}

View File

@ -418,7 +418,7 @@ static void __LocaleService_Init_JAPAN()
g_setQuestObjectDir.insert("locale/japan/quest/object");
g_stLocaleFilename = "locale/japan/sjis_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_sjis;
is_twobyte = is_twobyte_sjis;
@ -436,7 +436,7 @@ static void __LocaleService_Init_English()
g_setQuestObjectDir.insert("locale/english/quest/object");
g_stLocaleFilename = "locale/english/eng_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
}
@ -451,7 +451,7 @@ static void __LocaleService_Init_HongKong()
g_setQuestObjectDir.insert("locale/hongkong/quest/object");
g_stLocaleFilename = "locale/hongkong/big5_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_big5;
is_twobyte = is_twobyte_big5;
@ -468,7 +468,7 @@ static void __LocaleService_Init_NewCIBN()
g_setQuestObjectDir.insert("locale/newcibn/quest/object");
g_stLocaleFilename = "locale/newcibn/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_gb2312;
is_twobyte = is_twobyte_gb2312;
@ -486,7 +486,7 @@ static void __LocaleService_Init_Germany()
g_setQuestObjectDir.insert("locale/germany/quest/object");
g_stLocaleFilename = "locale/germany/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -503,7 +503,7 @@ static void __LocaleService_Init_Korea()
g_setQuestObjectDir.clear();
g_setQuestObjectDir.insert("locale/korea/quest/object");
g_iUseLocale = TRUE;
g_iUseLocale = true;
exp_table = exp_table_euckr;
}
@ -518,7 +518,7 @@ static void __LocaleService_Init_France()
g_setQuestObjectDir.insert("locale/france/quest/object");
g_stLocaleFilename = "locale/france/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -536,7 +536,7 @@ static void __LocaleService_Init_Italy()
g_setQuestObjectDir.insert("locale/italy/quest/object");
g_stLocaleFilename = "locale/italy/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -554,7 +554,7 @@ static void __LocaleService_Init_spain()
g_setQuestObjectDir.insert("locale/spain/quest/object");
g_stLocaleFilename = "locale/spain/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -572,7 +572,7 @@ static void __LocaleService_Init_greek()
g_setQuestObjectDir.insert("locale/greek/quest/object");
g_stLocaleFilename = "locale/greek/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -590,7 +590,7 @@ static void __LocaleService_Init_UK()
g_setQuestObjectDir.insert("locale/uk/quest/object");
g_stLocaleFilename = "locale/uk/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -608,7 +608,7 @@ static void __LocaleService_Init_Turkey()
g_setQuestObjectDir.insert("locale/turkey/quest/object");
g_stLocaleFilename = "locale/turkey/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -626,7 +626,7 @@ static void __LocaleService_Init_Poland()
g_setQuestObjectDir.insert("locale/poland/quest/object");
g_stLocaleFilename = "locale/poland/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -644,7 +644,7 @@ static void __LocaleService_Init_Portugal()
g_setQuestObjectDir.insert("locale/portugal/quest/object");
g_stLocaleFilename = "locale/portugal/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -664,7 +664,7 @@ static void __LocaleService_Init_Canada()
check_name = check_name_alphabet;
g_iUseLocale = TRUE;
g_iUseLocale = true;
}
static void __LocaleService_Init_Brazil()
@ -680,7 +680,7 @@ static void __LocaleService_Init_Brazil()
check_name = check_name_alphabet;
g_iUseLocale = TRUE;
g_iUseLocale = true;
}
static void __LocaleService_Init_YMIR()
@ -710,7 +710,7 @@ static void __LocaleService_Init_Russia()
g_setQuestObjectDir.insert("locale/russia/quest/object");
g_stLocaleFilename = "locale/russia/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -728,7 +728,7 @@ static void __LocaleService_Init_Denmark()
g_setQuestObjectDir.insert("locale/denmark/quest/object");
g_stLocaleFilename = "locale/denmark/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -746,7 +746,7 @@ static void __LocaleService_Init_Bulgaria()
g_setQuestObjectDir.insert("locale/bulgaria/quest/object");
g_stLocaleFilename = "locale/bulgaria/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -764,7 +764,7 @@ static void __LocaleService_Init_Croatia()
g_setQuestObjectDir.insert("locale/croatia/quest/object");
g_stLocaleFilename = "locale/croatia/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -782,7 +782,7 @@ static void __LocaleService_Init_Mexico()
g_setQuestObjectDir.insert("locale/mexico/quest/object");
g_stLocaleFilename = "locale/mexico/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -800,7 +800,7 @@ static void __LocaleService_Init_Arabia()
g_setQuestObjectDir.insert("locale/arabia/quest/object");
g_stLocaleFilename = "locale/arabia/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -818,7 +818,7 @@ static void __LocaleService_Init_Czech()
g_setQuestObjectDir.insert("locale/czech/quest/object");
g_stLocaleFilename = "locale/czech/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -836,7 +836,7 @@ static void __LocaleService_Init_Hungary()
g_setQuestObjectDir.insert("locale/hungary/quest/object");
g_stLocaleFilename = "locale/hungary/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -854,7 +854,7 @@ static void __LocaleService_Init_Romania()
g_setQuestObjectDir.insert("locale/romania/quest/object");
g_stLocaleFilename = "locale/romania/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -872,7 +872,7 @@ static void __LocaleService_Init_Netherlands()
g_setQuestObjectDir.insert("locale/netherlands/quest/object");
g_stLocaleFilename = "locale/netherlands/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
@ -892,7 +892,7 @@ static void __LocaleService_Init_Singapore()
check_name = check_name_alphabet;
g_iUseLocale = TRUE;
g_iUseLocale = true;
//exp_table = exp_table_newcibn; 2013 09 11 CYH europe °ú µ¿ÀÏÇÏ°Ô °£´Ù.
}
@ -909,7 +909,7 @@ static void __LocaleService_Init_Vietnam()
check_name = check_name_alphabet;
g_iUseLocale = TRUE;
g_iUseLocale = true;
exp_table = exp_table_newcibn;
}
@ -927,7 +927,7 @@ static void __LocaleService_Init_Thailand()
check_name = check_name_alphabet;
g_iUseLocale = TRUE;
g_iUseLocale = true;
}
static void __LocaleService_Init_USA()
@ -941,7 +941,7 @@ static void __LocaleService_Init_USA()
g_setQuestObjectDir.insert("locale/usa/quest/object");
g_stLocaleFilename = "locale/usa/locale_string.txt";
g_iUseLocale = TRUE;
g_iUseLocale = true;
check_name = check_name_alphabet;
}
@ -959,7 +959,7 @@ static void __LocaleService_Init_WE_Korea()
g_setQuestObjectDir.clear();
g_setQuestObjectDir.insert(g_stQuestDir + "/object");
g_iUseLocale = TRUE;
g_iUseLocale = true;
PK_PROTECT_LEVEL = 15;
}
@ -978,7 +978,7 @@ static void __LocaleService_Init_Taiwan()
check_name = check_name_big5;
is_twobyte = is_twobyte_big5;
g_iUseLocale = TRUE;
g_iUseLocale = true;
PK_PROTECT_LEVEL = 15;
}

View File

@ -96,7 +96,6 @@ int total_bytes_written = 0;
BYTE g_bLogLevel = 0;
socket_t tcp_socket = 0;
socket_t udp_socket = 0;
socket_t p2p_socket = 0;
LPFDWATCH main_fdw = NULL;
@ -617,15 +616,6 @@ int start(int argc, char **argv)
return 0;
}
#ifndef __UDP_BLOCK__
if ((udp_socket = socket_udp_bind(g_szPublicIP, mother_port)) == INVALID_SOCKET)
{
perror("socket_udp_bind: udp_socket");
return 0;
}
#endif
// if internal ip exists, p2p socket uses internal ip, if not use public ip
//if ((p2p_socket = socket_tcp_bind(*g_szInternalIP ? g_szInternalIP : g_szPublicIP, p2p_port)) == INVALID_SOCKET)
if ((p2p_socket = socket_tcp_bind(g_szPublicIP, p2p_port)) == INVALID_SOCKET)
@ -635,9 +625,6 @@ int start(int argc, char **argv)
}
fdwatch_add_fd(main_fdw, tcp_socket, NULL, FDW_READ, false);
#ifndef __UDP_BLOCK__
fdwatch_add_fd(main_fdw, udp_socket, NULL, FDW_READ, false);
#endif
fdwatch_add_fd(main_fdw, p2p_socket, NULL, FDW_READ, false);
db_clientdesc = DESC_MANAGER::instance().CreateConnectionDesc(main_fdw, db_addr, db_port, PHASE_DBCLIENT, true);
@ -691,9 +678,6 @@ void destroy()
sys_log(0, "<shutdown> Closing sockets...");
socket_close(tcp_socket);
#ifndef __UDP_BLOCK__
socket_close(udp_socket);
#endif
socket_close(p2p_socket);
sys_log(0, "<shutdown> fdwatch_delete()...");
@ -815,28 +799,6 @@ int io_loop(LPFDWATCH fdw)
DESC_MANAGER::instance().AcceptP2PDesc(fdw, p2p_socket);
fdwatch_clear_event(fdw, p2p_socket, event_idx);
}
/*
else if (FDW_READ == fdwatch_check_event(fdw, udp_socket, event_idx))
{
char buf[256];
struct sockaddr_in cliaddr;
socklen_t socklen = sizeof(cliaddr);
int iBytesRead;
if ((iBytesRead = socket_udp_read(udp_socket, buf, 256, (struct sockaddr *) &cliaddr, &socklen)) > 0)
{
static CInputUDP s_inputUDP;
s_inputUDP.SetSockAddr(cliaddr);
int iBytesProceed;
s_inputUDP.Process(NULL, buf, iBytesRead, iBytesProceed);
}
fdwatch_clear_event(fdw, udp_socket, event_idx);
}
*/
continue;
}

View File

@ -113,7 +113,6 @@ enum
HEADER_GC_KEY_AGREEMENT = 0xfb, // _IMPROVED_PACKET_ENCRYPTION_
HEADER_GC_TIME_SYNC = 0xfc,
HEADER_GC_PHASE = 0xfd,
HEADER_GC_BINDUDP = 0xfe,
HEADER_GC_HANDSHAKE = 0xff,
HEADER_GC_CHARACTER_ADD = 1,
@ -854,13 +853,6 @@ typedef struct packet_phase
BYTE phase;
} TPacketGCPhase;
typedef struct packet_bindudp
{
BYTE header;
DWORD addr;
WORD port;
} TPacketGCBindUDP;
enum
{
LOGIN_FAILURE_ALREADY = 1,

View File

@ -53,12 +53,4 @@ class CPacketInfoGG : public CPacketInfo
virtual ~CPacketInfoGG();
};
/// Implemented in input_udp.cpp
class CPacketInfoUDP : public CPacketInfo
{
public:
CPacketInfoUDP();
virtual ~CPacketInfoUDP();
};
#endif

View File

@ -243,18 +243,18 @@ namespace quest
if (!npc || npc->IsPC())
{
lua_pushboolean(L, TRUE);
lua_pushboolean(L, true);
return 1;
}
if (npc->GetQuestNPCID() == 0 || npc->GetQuestNPCID() == ch->GetPlayerID())
{
npc->SetQuestNPCID(ch->GetPlayerID());
lua_pushboolean(L, TRUE);
lua_pushboolean(L, true);
}
else
{
lua_pushboolean(L, FALSE);
lua_pushboolean(L, true);
}
return 1;

View File

@ -168,7 +168,7 @@ BOOL CTextFileLoader::SetChildNode(const char * c_szKey)
if (!m_pcurNode)
{
assert(!"Node to access has not set!");
return FALSE;
return false;
}
for (DWORD i = 0; i < m_pcurNode->ChildNodeVector.size(); ++i)
@ -177,11 +177,11 @@ BOOL CTextFileLoader::SetChildNode(const char * c_szKey)
if (0 == pGroupNode->strGroupName.compare(c_szKey))
{
m_pcurNode = pGroupNode;
return TRUE;
return true;
}
}
return FALSE;
return false;
}
BOOL CTextFileLoader::SetChildNode(const std::string & c_rstrKeyHead, DWORD dwIndex)
@ -196,18 +196,18 @@ BOOL CTextFileLoader::SetChildNode(DWORD dwIndex)
if (!m_pcurNode)
{
assert(!"Node to access has not set!");
return FALSE;
return false;
}
if (dwIndex >= m_pcurNode->ChildNodeVector.size())
{
assert(!"Node index to set is too large to access!");
return FALSE;
return false;
}
m_pcurNode = m_pcurNode->ChildNodeVector[dwIndex];
return TRUE;
return true;
}
BOOL CTextFileLoader::SetParentNode()
@ -215,30 +215,30 @@ BOOL CTextFileLoader::SetParentNode()
if (!m_pcurNode)
{
assert(!"Node to access has not set!");
return FALSE;
return false;
}
if (NULL == m_pcurNode->pParentNode)
{
assert(!"Current group node is already top!");
return FALSE;
return false;
}
m_pcurNode = m_pcurNode->pParentNode;
return TRUE;
return true;
}
BOOL CTextFileLoader::GetCurrentNodeName(std::string * pstrName)
{
if (!m_pcurNode)
return FALSE;
return false;
if (NULL == m_pcurNode->pParentNode)
return FALSE;
return false;
*pstrName = m_pcurNode->strGroupName;
return TRUE;
return true;
}
BOOL CTextFileLoader::IsToken(const std::string & c_rstrKey)
@ -246,7 +246,7 @@ BOOL CTextFileLoader::IsToken(const std::string & c_rstrKey)
if (!m_pcurNode)
{
assert(!"Node to access has not set!");
return FALSE;
return false;
}
return m_pcurNode->LocalTokenVectorMap.end() != m_pcurNode->LocalTokenVectorMap.find(c_rstrKey);
@ -257,95 +257,95 @@ BOOL CTextFileLoader::GetTokenVector(const std::string & c_rstrKey, TTokenVector
if (!m_pcurNode)
{
assert(!"Node to access has not set!");
return FALSE;
return false;
}
TTokenVectorMap::iterator it = m_pcurNode->LocalTokenVectorMap.find(c_rstrKey);
if (m_pcurNode->LocalTokenVectorMap.end() == it)
{
sys_log(2, " CTextFileLoader::GetTokenVector - Failed to find the key %s [%s :: %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
*ppTokenVector = &it->second;
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenBoolean(const std::string & c_rstrKey, BOOL * pData)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->empty())
{
sys_log(2, " CTextFileLoader::GetTokenBoolean - Failed to find the value %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
BOOL out = 0;
str_to_number(out, pTokenVector->at(0).c_str());
*pData = out;
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenByte(const std::string & c_rstrKey, BYTE * pData)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->empty())
{
sys_log(2, " CTextFileLoader::GetTokenByte - Failed to find the value %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
BYTE out = 0;
str_to_number(out, pTokenVector->at(0).c_str());
*pData = out;
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenWord(const std::string & c_rstrKey, WORD * pData)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->empty())
{
sys_log(2, " CTextFileLoader::GetTokenWord - Failed to find the value %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
WORD out = 0;
str_to_number(out, pTokenVector->at(0).c_str());
*pData = out;
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenInteger(const std::string & c_rstrKey, int * pData)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->empty())
{
sys_log(2, " CTextFileLoader::GetTokenInteger - Failed to find the value %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
int out = 0;
str_to_number(out, pTokenVector->at(0).c_str());
*pData = out;
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenDoubleWord(const std::string & c_rstrKey, DWORD * pData)
@ -357,66 +357,66 @@ BOOL CTextFileLoader::GetTokenFloat(const std::string & c_rstrKey, float * pData
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->empty())
{
sys_log(2, " CTextFileLoader::GetTokenFloat - Failed to find the value %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
*pData = atof(pTokenVector->at(0).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenVector2(const std::string & c_rstrKey, D3DXVECTOR2 * pVector2)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 2)
{
sys_log(2, " CTextFileLoader::GetTokenVector2 - This key should have 2 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pVector2->x = atof(pTokenVector->at(0).c_str());
pVector2->y = atof(pTokenVector->at(1).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenVector3(const std::string & c_rstrKey, D3DXVECTOR3 * pVector3)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 3)
{
sys_log(2, " CTextFileLoader::GetTokenVector3 - This key should have 3 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pVector3->x = atof(pTokenVector->at(0).c_str());
pVector3->y = atof(pTokenVector->at(1).c_str());
pVector3->z = atof(pTokenVector->at(2).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenVector4(const std::string & c_rstrKey, D3DXVECTOR4 * pVector4)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 4)
{
sys_log(2, " CTextFileLoader::GetTokenVector3 - This key should have 3 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pVector4->x = atof(pTokenVector->at(0).c_str());
@ -424,7 +424,7 @@ BOOL CTextFileLoader::GetTokenVector4(const std::string & c_rstrKey, D3DXVECTOR4
pVector4->z = atof(pTokenVector->at(2).c_str());
pVector4->w = atof(pTokenVector->at(3).c_str());
return TRUE;
return true;
}
@ -437,49 +437,49 @@ BOOL CTextFileLoader::GetTokenQuaternion(const std::string & c_rstrKey, D3DXQUAT
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 4)
{
sys_log(2, " CTextFileLoader::GetTokenVector3 - This key should have 3 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pQ->x = atof(pTokenVector->at(0).c_str());
pQ->y = atof(pTokenVector->at(1).c_str());
pQ->z = atof(pTokenVector->at(2).c_str());
pQ->w = atof(pTokenVector->at(3).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenDirection(const std::string & c_rstrKey, D3DVECTOR * pVector)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 3)
{
sys_log(2, " CTextFileLoader::GetTokenDirection - This key should have 3 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pVector->x = atof(pTokenVector->at(0).c_str());
pVector->y = atof(pTokenVector->at(1).c_str());
pVector->z = atof(pTokenVector->at(2).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenColor(const std::string & c_rstrKey, D3DXCOLOR * pColor)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 4)
{
sys_log(2, " CTextFileLoader::GetTokenColor - This key should have 4 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pColor->r = atof(pTokenVector->at(0).c_str());
@ -487,19 +487,19 @@ BOOL CTextFileLoader::GetTokenColor(const std::string & c_rstrKey, D3DXCOLOR * p
pColor->b = atof(pTokenVector->at(2).c_str());
pColor->a = atof(pTokenVector->at(3).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenColor(const std::string & c_rstrKey, D3DCOLORVALUE * pColor)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->size() != 4)
{
sys_log(2, " CTextFileLoader::GetTokenColor - This key should have 4 values %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
pColor->r = atof(pTokenVector->at(0).c_str());
@ -507,23 +507,23 @@ BOOL CTextFileLoader::GetTokenColor(const std::string & c_rstrKey, D3DCOLORVALUE
pColor->b = atof(pTokenVector->at(2).c_str());
pColor->a = atof(pTokenVector->at(3).c_str());
return TRUE;
return true;
}
BOOL CTextFileLoader::GetTokenString(const std::string & c_rstrKey, std::string * pString)
{
TTokenVector * pTokenVector;
if (!GetTokenVector(c_rstrKey, &pTokenVector))
return FALSE;
return false;
if (pTokenVector->empty())
{
sys_log(2, " CTextFileLoader::GetTokenString - Failed to find the value %s [%s : %s]", m_strFileName.c_str(), m_pcurNode->strGroupName.c_str(), c_rstrKey.c_str());
return FALSE;
return false;
}
*pString = pTokenVector->at(0);
return TRUE;
return true;
}

View File

@ -70,7 +70,7 @@ void skip_spaces(const char **string)
const char *one_argument(const char *argument, char *first_arg, size_t first_size)
{
char mark = FALSE;
bool mark = false;
size_t first_len = 0;
if (!argument || 0 == first_size)

View File

@ -16,14 +16,8 @@ add_library(${PROJECT_NAME} STATIC ${SOURCES})
# Find dependencies
find_package(libmysql REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE ${MYSQL_LIBRARIES})
find_package(Boost REQUIRED)
# Link dependencies if found
if (libmysql_FOUND)
target_link_libraries (${PROJECT_NAME} ${MYSQL_LIBRARIES})
endif (libmysql_FOUND)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} ${Boost_LIBRARIES})
endif (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES})

View File

@ -16,9 +16,5 @@ add_library(${PROJECT_NAME} STATIC ${SOURCES})
# Find dependencies
find_package(Boost REQUIRED)
# Link dependencies if found
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} ${Boost_LIBRARIES})
endif (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES})

View File

@ -1,95 +0,0 @@
#ifndef __INC_LIBTHECORE_FDWATCH_H__
#define __INC_LIBTHECORE_FDWATCH_H__
#if defined(WIN32) || defined(__linux__)
typedef struct fdwatch FDWATCH;
typedef struct fdwatch * LPFDWATCH;
enum EFdwatch
{
FDW_NONE = 0,
FDW_READ = 1,
FDW_WRITE = 2,
FDW_WRITE_ONESHOT = 4,
FDW_EOF = 8,
};
struct fdwatch
{
fd_set rfd_set;
fd_set wfd_set;
socket_t* select_fds;
int* select_rfdidx;
int nselect_fds;
fd_set working_rfd_set;
fd_set working_wfd_set;
int nfiles;
void** fd_data;
int* fd_rw;
};
#else
typedef struct fdwatch FDWATCH;
typedef struct fdwatch* LPFDWATCH;
enum EFdwatch
{
FDW_NONE = 0,
FDW_READ = 1,
FDW_WRITE = 2,
FDW_WRITE_ONESHOT = 4,
FDW_EOF = 8,
};
typedef struct kevent KEVENT;
typedef struct kevent* LPKEVENT;
typedef int KQUEUE;
struct fdwatch
{
KQUEUE kq;
int nfiles;
LPKEVENT kqevents;
int nkqevents;
LPKEVENT kqrevents;
int* fd_event_idx;
void** fd_data;
int* fd_rw;
};
#endif // WIN32
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
extern LPFDWATCH fdwatch_new(int nfiles);
extern void fdwatch_clear_fd(LPFDWATCH fdw, socket_t fd);
extern void fdwatch_delete(LPFDWATCH fdw);
extern int fdwatch_check_fd(LPFDWATCH fdw, socket_t fd);
extern int fdwatch_check_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx);
extern void fdwatch_clear_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx);
extern void fdwatch_add_fd(LPFDWATCH fdw, socket_t fd, void* client_data, int rw, int oneshot);
extern int fdwatch(LPFDWATCH fdw, struct timeval *timeout);
extern void * fdwatch_get_client_data(LPFDWATCH fdw, unsigned int event_idx);
extern void fdwatch_del_fd(LPFDWATCH fdw, socket_t fd);
extern int fdwatch_get_buffer_size(LPFDWATCH fdw, socket_t fd);
extern int fdwatch_get_ident(LPFDWATCH fdw, unsigned int event_idx);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,45 +0,0 @@
/*
* Filename: socket.c
* Description: ¼ÒÄÏ °ü·Ã ÇÔ¼ö Çì´õ.
*
* Author: ºñ¿± (server), myevan (Client)
*/
#ifndef __INC_LIBTHECORE_SOCKET_H__
#define __INC_LIBTHECORE_SOCKET_H__
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
#ifdef __WIN32__
typedef int socklen_t;
#else
#define INVALID_SOCKET -1
#endif
extern int socket_read(socket_t desc, char* read_point, size_t space_left);
extern int socket_write(socket_t desc, const char *data, size_t length);
extern int socket_udp_read(socket_t desc, char * read_point, size_t space_left, struct sockaddr * from, socklen_t * fromlen);
extern int socket_tcp_bind(const char * ip, int port);
extern int socket_udp_bind(const char * ip, int port);
extern socket_t socket_accept(socket_t s, struct sockaddr_in *peer);
extern void socket_close(socket_t s);
extern socket_t socket_connect(const char* host, WORD port);
extern void socket_nonblock(socket_t s);
extern void socket_block(socket_t s);
extern void socket_dontroute(socket_t s);
extern void socket_lingeroff(socket_t s);
extern void socket_lingeron(socket_t s);
extern void socket_sndbuf(socket_t s, unsigned int opt);
extern void socket_rcvbuf(socket_t s, unsigned int opt);
#ifdef __cplusplus
};
#endif
#endif

View File

@ -1,100 +1,16 @@
#ifndef __INC_LIBTHECORE_STDAFX_H__
#define __INC_LIBTHECORE_STDAFX_H__
#if defined(__GNUC__)
#define INLINE __inline__
#elif defined(_MSC_VER)
#define INLINE inline
#endif
#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <tchar.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <conio.h>
#include <process.h>
#include <limits.h>
#include <math.h>
#include <locale.h>
#include <io.h>
#include <direct.h>
#include <fcntl.h>
#include "xdirent.h"
#include "xgetopt.h"
#define S_ISDIR(m) (m & _S_IFDIR)
#define snprintf _snprintf
struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
#define __USE_SELECT__
#define PATH_MAX _MAX_PATH
// C runtime library adjustments
#define strncat(dst, src, size) strcat_s(dst, size, src)
#define strncpy(dst, src, size) strncpy_s(dst, size, src, _TRUNCATE)
#define strtoull(str, endptr, base) _strtoui64(str, endptr, base)
#define strtof(str, endptr) (float)strtod(str, endptr)
#define strcasecmp(s1, s2) stricmp(s1, s2)
#define strncasecmp(s1, s2, n) strnicmp(s1, s2, n)
#define atoll(str) _atoi64(str)
#define localtime_r(timet, result) localtime_s(result, timet)
#define strtok_r(s, delim, ptrptr) strtok_s(s, delim, ptrptr)
#include <boost/typeof/typeof.hpp>
#define typeof(t) BOOST_TYPEOF(t)
// dummy declaration of non-supported signals
#define SIGUSR1 30 /* user defined signal 1 */
#define SIGUSR2 31 /* user defined signal 2 */
inline void usleep(unsigned long usec) {
::Sleep(usec / 1000);
}
inline unsigned sleep(unsigned sec) {
::Sleep(sec * 1000);
return 0;
}
inline double rint(double x)
{
return ::floor(x+.5);
}
#else
#ifndef __FreeBSD__
#define __USE_SELECT__
#ifdef __CYGWIN__
#define _POSIX_SOURCE 1
#endif
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <cerrno>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <cassert>
#include <cctype>
#include <climits>
#include <dirent.h>
#include <sys/time.h>
@ -112,28 +28,10 @@ inline double rint(double x)
#include <pthread.h>
#include <semaphore.h>
#ifdef __FreeBSD__
#include <sys/event.h>
#endif
#endif
#ifndef false
#define false 0
#define true (!false)
#endif
#ifndef FALSE
#define FALSE false
#define TRUE (!FALSE)
#endif
#include "typedef.h"
#include "heart.h"
#include "fdwatch.h"
#include "socket.h"
#include "buffer.h"
#include "signal.h"
#include "signals.h"
#include "log.h"
#include "main.h"
#include "utils.h"

View File

@ -1,27 +1,23 @@
#ifndef __INC_LIBTHECORE_TYPEDEF_H__
#define __INC_LIBTHECORE_TYPEDEF_H__
typedef unsigned long int QWORD;
typedef unsigned char UBYTE;
typedef signed char sbyte;
typedef unsigned short sh_int;
#include <cstdint>
typedef uint64_t QWORD;
typedef uint8_t UBYTE;
typedef int8_t sbyte;
typedef uint16_t sh_int;
#ifndef __WIN32__
#ifndef __cplusplus
typedef unsigned char bool;
#endif
typedef unsigned int DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef long LONG;
typedef unsigned long ULONG;
typedef int INT;
typedef unsigned int UINT;
typedef int socket_t;
typedef uint32_t DWORD;
typedef uint32_t BOOL;
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef int32_t LONG;
typedef uint32_t ULONG;
typedef int32_t INT;
typedef uint32_t UINT;
#else

View File

@ -1,461 +0,0 @@
#define __LIBTHECORE__
#include "stdafx.h"
#ifndef __USE_SELECT__
LPFDWATCH fdwatch_new(int nfiles)
{
LPFDWATCH fdw;
int kq;
kq = kqueue();
if (kq == -1)
{
sys_err("%s", strerror(errno));
return NULL;
}
CREATE(fdw, FDWATCH, 1);
fdw->kq = kq;
fdw->nfiles = nfiles;
fdw->nkqevents = 0;
CREATE(fdw->kqevents, KEVENT, nfiles * 2);
CREATE(fdw->kqrevents, KEVENT, nfiles * 2);
CREATE(fdw->fd_event_idx, int, nfiles);
CREATE(fdw->fd_rw, int, nfiles);
CREATE(fdw->fd_data, void*, nfiles);
return (fdw);
}
void fdwatch_delete(LPFDWATCH fdw)
{
free(fdw->fd_data);
free(fdw->fd_rw);
free(fdw->kqevents);
free(fdw->kqrevents);
free(fdw->fd_event_idx);
free(fdw);
}
int fdwatch(LPFDWATCH fdw, struct timeval *timeout)
{
int i, r;
struct timespec ts;
if (fdw->nkqevents)
sys_log(2, "fdwatch: nkqevents %d", fdw->nkqevents);
if (!timeout)
{
ts.tv_sec = 0;
ts.tv_nsec = 0;
r = kevent(fdw->kq, fdw->kqevents, fdw->nkqevents, fdw->kqrevents, fdw->nfiles, &ts);
}
else
{
ts.tv_sec = timeout->tv_sec;
ts.tv_nsec = timeout->tv_usec;
r = kevent(fdw->kq, fdw->kqevents, fdw->nkqevents, fdw->kqrevents, fdw->nfiles, &ts);
}
fdw->nkqevents = 0;
if (r == -1)
return -1;
memset(fdw->fd_event_idx, 0, sizeof(int) * fdw->nfiles);
for (i = 0; i < r; i++)
{
int fd = fdw->kqrevents[i].ident;
if (fd >= fdw->nfiles)
sys_err("ident overflow %d nfiles: %d", fdw->kqrevents[i].ident, fdw->nfiles);
else
{
if (fdw->kqrevents[i].filter == EVFILT_WRITE)
fdw->fd_event_idx[fd] = i;
}
}
return (r);
}
void fdwatch_register(LPFDWATCH fdw, int flag, int fd, int rw)
{
if (flag == EV_DELETE)
{
if (fdw->fd_rw[fd] & FDW_READ)
{
fdw->kqevents[fdw->nkqevents].ident = fd;
fdw->kqevents[fdw->nkqevents].flags = flag;
fdw->kqevents[fdw->nkqevents].filter = EVFILT_READ;
++fdw->nkqevents;
}
if (fdw->fd_rw[fd] & FDW_WRITE)
{
fdw->kqevents[fdw->nkqevents].ident = fd;
fdw->kqevents[fdw->nkqevents].flags = flag;
fdw->kqevents[fdw->nkqevents].filter = EVFILT_WRITE;
++fdw->nkqevents;
}
}
else
{
fdw->kqevents[fdw->nkqevents].ident = fd;
fdw->kqevents[fdw->nkqevents].flags = flag;
fdw->kqevents[fdw->nkqevents].filter = (rw == FDW_READ) ? EVFILT_READ : EVFILT_WRITE;
++fdw->nkqevents;
}
}
void fdwatch_clear_fd(LPFDWATCH fdw, socket_t fd)
{
fdw->fd_data[fd] = NULL;
fdw->fd_rw[fd] = 0;
}
void fdwatch_add_fd(LPFDWATCH fdw, socket_t fd, void * client_data, int rw, int oneshot)
{
int flag;
if (fd >= fdw->nfiles)
{
sys_err("fd overflow %d", fd);
return;
}
if (fdw->fd_rw[fd] & rw)
return;
fdw->fd_rw[fd] |= rw;
sys_log(2, "FDWATCH_REGISTER fdw %p fd %d rw %d data %p", fdw, fd, rw, client_data);
if (!oneshot)
flag = EV_ADD;
else
{
sys_log(2, "ADD ONESHOT fd_rw %d", fdw->fd_rw[fd]);
flag = EV_ADD | EV_ONESHOT;
fdw->fd_rw[fd] |= FDW_WRITE_ONESHOT;
}
fdw->fd_data[fd] = client_data;
fdwatch_register(fdw, flag, fd, rw);
}
void fdwatch_del_fd(LPFDWATCH fdw, socket_t fd)
{
fdwatch_register(fdw, EV_DELETE, fd, 0);
fdwatch_clear_fd(fdw, fd);
}
void fdwatch_clear_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx)
{
assert(event_idx < fdw->nfiles * 2);
if (fdw->kqrevents[event_idx].ident != fd)
return;
fdw->kqrevents[event_idx].ident = 0;
}
int fdwatch_check_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx)
{
assert(event_idx < fdw->nfiles * 2);
if (fdw->kqrevents[event_idx].ident != fd)
return 0;
if (fdw->kqrevents[event_idx].flags & EV_ERROR)
return FDW_EOF;
if (fdw->kqrevents[event_idx].flags & EV_EOF)
return FDW_EOF;
if (fdw->kqrevents[event_idx].filter == EVFILT_READ)
{
if (fdw->fd_rw[fd] & FDW_READ)
return FDW_READ;
}
else if (fdw->kqrevents[event_idx].filter == EVFILT_WRITE)
{
if (fdw->fd_rw[fd] & FDW_WRITE)
{
if (fdw->fd_rw[fd] & FDW_WRITE_ONESHOT)
fdw->fd_rw[fd] &= ~FDW_WRITE;
return FDW_WRITE;
}
}
else
sys_err("fdwatch_check_event: Unknown filter %d (descriptor %d)", fdw->kqrevents[event_idx].filter, fd);
return 0;
}
int fdwatch_get_ident(LPFDWATCH fdw, unsigned int event_idx)
{
assert(event_idx < fdw->nfiles * 2);
return fdw->kqrevents[event_idx].ident;
}
int fdwatch_get_buffer_size(LPFDWATCH fdw, socket_t fd)
{
int event_idx = fdw->fd_event_idx[fd];
if (fdw->kqrevents[event_idx].filter == EVFILT_WRITE)
return fdw->kqrevents[event_idx].data;
return 0;
}
void * fdwatch_get_client_data(LPFDWATCH fdw, unsigned int event_idx)
{
int fd;
assert(event_idx < fdw->nfiles * 2);
fd = fdw->kqrevents[event_idx].ident;
if (fd >= fdw->nfiles)
return NULL;
return (fdw->fd_data[fd]);
}
#else // ifndef __USE_SELECT__
#ifdef __WIN32__
static int win32_init_refcount = 0;
static bool win32_init()
{
if (win32_init_refcount > 0)
{
win32_init_refcount++;
return true;
}
WORD wVersion = MAKEWORD(2, 0);
WSADATA wsaData;
if (WSAStartup(wVersion, &wsaData) != 0)
return false;
win32_init_refcount++;
return true;
}
static void win32_deinit()
{
if (--win32_init_refcount <= 0)
WSACleanup();
}
#endif
LPFDWATCH fdwatch_new(int nfiles)
{
LPFDWATCH fdw;
#ifdef __WIN32__
if (!win32_init())
return NULL;
#endif
// nfiles value is limited to FD_SETSIZE (64)
CREATE(fdw, FDWATCH, 1);
fdw->nfiles = MIN(nfiles, FD_SETSIZE);
FD_ZERO(&fdw->rfd_set);
FD_ZERO(&fdw->wfd_set);
CREATE(fdw->select_fds, socket_t, nfiles);
CREATE(fdw->select_rfdidx, int, nfiles);
fdw->nselect_fds = 0;
CREATE(fdw->fd_rw, int, nfiles);
CREATE(fdw->fd_data, void*, nfiles);
return (fdw);
}
void fdwatch_delete(LPFDWATCH fdw)
{
free(fdw->fd_data);
free(fdw->fd_rw);
free(fdw->select_fds);
free(fdw->select_rfdidx);
free(fdw);
#ifdef __WIN32__
win32_deinit();
#endif
}
static int fdwatch_get_fdidx(LPFDWATCH fdw, socket_t fd) {
int i;
for (i = 0; i < fdw->nselect_fds; ++i) {
if (fdw->select_fds[i] == fd) {
return i;
}
}
return -1;
}
void fdwatch_add_fd(LPFDWATCH fdw, socket_t fd, void* client_data, int rw, int oneshot)
{
int idx = fdwatch_get_fdidx(fdw, fd);
if (idx < 0) {
if (fdw->nselect_fds >= fdw->nfiles) {
return;
}
idx = fdw->nselect_fds;
fdw->select_fds[fdw->nselect_fds++] = fd;
fdw->fd_rw[idx] = rw;
} else {
fdw->fd_rw[idx] |= rw;
}
fdw->fd_data[idx] = client_data;
if (rw & FDW_READ)
FD_SET(fd, &fdw->rfd_set);
if (rw & FDW_WRITE)
FD_SET(fd, &fdw->wfd_set);
}
void fdwatch_del_fd(LPFDWATCH fdw, socket_t fd)
{
if (fdw->nselect_fds <= 0) {
return;
}
int idx = fdwatch_get_fdidx(fdw, fd);
if (idx < 0) {
return;
}
--fdw->nselect_fds;
fdw->select_fds[idx] = fdw->select_fds[fdw->nselect_fds];
fdw->fd_data[idx] = fdw->fd_data[fdw->nselect_fds];
fdw->fd_rw[idx] = fdw->fd_rw[fdw->nselect_fds];
FD_CLR(fd, &fdw->rfd_set);
FD_CLR(fd, &fdw->wfd_set);
}
int fdwatch(LPFDWATCH fdw, struct timeval *timeout)
{
int r, i, event_idx;
struct timeval tv;
fdw->working_rfd_set = fdw->rfd_set;
fdw->working_wfd_set = fdw->wfd_set;
if (!timeout)
{
tv.tv_sec = 0;
tv.tv_usec = 0;
r = select(0, &fdw->working_rfd_set, &fdw->working_wfd_set, (fd_set*) 0, &tv);
}
else
{
tv = *timeout;
r = select(0, &fdw->working_rfd_set, &fdw->working_wfd_set, (fd_set*) 0, &tv);
}
if (r == -1)
return -1;
event_idx = 0;
for (i = 0; i < fdw->nselect_fds; ++i)
{
if (fdwatch_check_fd(fdw, fdw->select_fds[i]))
fdw->select_rfdidx[event_idx++] = i;
}
return event_idx;
}
int fdwatch_check_fd(LPFDWATCH fdw, socket_t fd)
{
int idx = fdwatch_get_fdidx(fdw, fd);
if (idx < 0) {
return 0;
}
int result = 0;
if ((fdw->fd_rw[idx] & FDW_READ) && FD_ISSET(fd, &fdw->working_rfd_set)) {
result |= FDW_READ;
}
if ((fdw->fd_rw[idx] & FDW_WRITE) && FD_ISSET(fd, &fdw->working_wfd_set)) {
result |= FDW_WRITE;
}
return result;
}
void * fdwatch_get_client_data(LPFDWATCH fdw, unsigned int event_idx)
{
int idx = fdw->select_rfdidx[event_idx];
if (idx < 0 || fdw->nfiles <= idx) {
return NULL;
}
return fdw->fd_data[idx];
}
int fdwatch_get_ident(LPFDWATCH fdw, unsigned int event_idx)
{
int idx = fdw->select_rfdidx[event_idx];
if (idx < 0 || fdw->nfiles <= idx) {
return 0;
}
return (int)fdw->select_fds[idx];
}
void fdwatch_clear_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx)
{
int idx = fdw->select_rfdidx[event_idx];
if (idx < 0 || fdw->nfiles <= idx) {
return;
}
socket_t rfd = fdw->select_fds[idx];
if (fd != rfd) {
return;
}
FD_CLR(fd, &fdw->working_rfd_set);
FD_CLR(fd, &fdw->working_wfd_set);
}
int fdwatch_check_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx)
{
int idx = fdw->select_rfdidx[event_idx];
if (idx < 0 || fdw->nfiles <= idx) {
return 0;
}
socket_t rfd = fdw->select_fds[idx];
if (fd != rfd) {
return 0;
}
int result = fdwatch_check_fd(fdw, fd);
if (result & FDW_READ) {
return FDW_READ;
} else if (result & FDW_WRITE) {
return FDW_WRITE;
}
return 0;
}
int fdwatch_get_buffer_size(LPFDWATCH fdw, socket_t fd)
{
return INT_MAX; // XXX TODO
}
#endif

View File

@ -42,7 +42,7 @@ void log_file_rotate(LPLOGFILE logfile);
void log_file_check(LPLOGFILE logfile);
void log_file_set_dir(const char *dir);
static unsigned int log_level_bits = 0;
static unsigned int log_level_bits = 7;
void log_set_level(unsigned int bit)
{

View File

@ -8,11 +8,9 @@
#include "stdafx.h"
#include "memory.h"
extern void GOST_Init();
LPHEART thecore_heart = NULL;
volatile int shutdowned = FALSE;
volatile int shutdowned = false;
volatile int tics = 0;
unsigned int thecore_profiler[NUM_PF];
@ -67,7 +65,7 @@ int thecore_init(int fps, HEARTFUNC heartbeat_func)
void thecore_shutdown()
{
shutdowned = TRUE;
shutdowned = true;
}
int thecore_idle(void)

View File

@ -35,7 +35,7 @@ RETSIGTYPE checkpointing(int sig)
RETSIGTYPE hupsig(int sig)
{
shutdowned = TRUE;
shutdowned = true;
sys_err("SIGHUP, SIGINT, SIGTERM signal has been received. shutting down.");
}

View File

@ -1,555 +0,0 @@
/*
* Filename: socket.c
* Description: .
*
* Author: aka. Cronan
*/
#define __LIBTHECORE__
#include "stdafx.h"
/* Forwards */
void socket_lingeron(socket_t s);
void socket_lingeroff(socket_t s);
void socket_timeout(socket_t s, long sec, long usec);
void socket_reuse(socket_t s);
void socket_keepalive(socket_t s);
int socket_udp_read(socket_t desc, char * read_point, size_t space_left, struct sockaddr * from, socklen_t * fromlen)
{
/*
ssize_t recvfrom(int s, void * buf, size_t len, int flags, struct sockaddr * from, socklen_t * fromlen);
*/
ssize_t ret;
ret = recvfrom(desc, read_point, space_left, 0, from, fromlen);
return (ret);
}
int socket_read(socket_t desc, char* read_point, size_t space_left)
{
int ret;
ret = recv(desc, read_point, space_left, 0);
if (ret > 0)
return ret;
if (ret == 0) // 정상적으로 접속 끊김
return -1;
#ifdef EINTR /* Interrupted system call - various platforms */
if (errno == EINTR)
return (0);
#endif
#ifdef EAGAIN /* POSIX */
if (errno == EAGAIN)
return (0);
#endif
#ifdef EWOULDBLOCK /* BSD */
if (errno == EWOULDBLOCK)
return (0);
#endif /* EWOULDBLOCK */
#ifdef EDEADLK /* Macintosh */
if (errno == EDEADLK)
return (0);
#endif
#ifdef __WIN32__
int wsa_error = WSAGetLastError();
if (wsa_error == WSAEWOULDBLOCK) {
return 0;
}
sys_log(0, "socket_read: WSAGetLastError returned %d", wsa_error);
#endif
sys_err("about to lose connection");
return -1;
}
int socket_write_tcp(socket_t desc, const char *txt, int length)
{
int bytes_written = send(desc, txt, length, 0);
// 성공
if (bytes_written > 0)
return (bytes_written);
if (bytes_written == 0)
return -1;
#ifdef EAGAIN /* POSIX */
if (errno == EAGAIN)
return 0;
#endif
#ifdef EWOULDBLOCK /* BSD */
if (errno == EWOULDBLOCK)
return 0;
#endif
#ifdef EDEADLK /* Macintosh */
if (errno == EDEADLK)
return 0;
#endif
#ifdef __WIN32__
int wsa_error = WSAGetLastError();
if (wsa_error == WSAEWOULDBLOCK) {
return 0;
}
sys_log(0, "socket_write_tcp: WSAGetLastError returned %d", wsa_error);
#endif
/* Looks like the error was fatal. Too bad. */
return -1;
}
int socket_write(socket_t desc, const char *data, size_t length)
{
size_t total;
int bytes_written;
total = length;
do
{
if ((bytes_written = socket_write_tcp(desc, data, total)) < 0)
{
#ifdef EWOULDBLOCK
if (errno == EWOULDBLOCK)
errno = EAGAIN;
#endif
if (errno == EAGAIN)
sys_err("socket write would block, about to close!");
else
sys_err("write to desc error"); // '보통' 상대편으로 부터 접속이 끊긴 것이다.
return -1;
}
else
{
data += bytes_written;
total -= bytes_written;
}
}
while (total > 0);
return 0;
}
int socket_bind(const char * ip, int port, int protocol)
{
int s;
#ifdef __WIN32
SOCKADDR_IN sa;
#else
struct sockaddr_in sa;
#endif
if ((s = socket(AF_INET, protocol, 0)) < 0)
{
sys_err("socket: %s", strerror(errno));
return -1;
}
socket_reuse(s);
#ifndef __linux__
#if defined(WIN32)
// Winsock2: SO_DONTLINGER, SO_KEEPALIVE, SO_LINGER, and SO_OOBINLINE are
// not supported on sockets of type SOCK_DGRAM
if (protocol == SOCK_STREAM) {
socket_lingeroff(s);
}
#else
socket_lingeroff(s);
#endif
#endif
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
//윈도우 서버는 개발용으로만 쓰기 때문에 BIND ip를 INADDR_ANY로 고정
//(테스트의 편의성을 위해)
#ifndef __WIN32__
sa.sin_addr.s_addr = inet_addr(ip);
#else
sa.sin_addr.s_addr = INADDR_ANY;
#endif
sa.sin_port = htons((unsigned short) port);
if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
{
sys_err("bind: %s", strerror(errno));
return -1;
}
#ifndef __linux__
socket_nonblock(s);
#endif
if (protocol == SOCK_STREAM)
{
sys_log(0, "SYSTEM: BINDING TCP PORT ON [%d] (fd %d)", port, s);
listen(s, SOMAXCONN);
}
else
sys_log(0, "SYSTEM: BINDING UDP PORT ON [%d] (fd %d)", port, s);
return s;
}
int socket_tcp_bind(const char * ip, int port)
{
return socket_bind(ip, port, SOCK_STREAM);
}
int socket_udp_bind(const char * ip, int port)
{
return socket_bind(ip, port, SOCK_DGRAM);
}
void socket_close(socket_t s)
{
#ifdef __WIN32__
closesocket(s);
#else
close(s);
#endif
}
socket_t socket_accept(socket_t s, struct sockaddr_in *peer)
{
socket_t desc;
socklen_t i;
i = sizeof(*peer);
if ((desc = accept(s, (struct sockaddr *) peer, &i)) == -1)
{
sys_err("accept: %s (fd %d)", strerror(errno), s);
return -1;
}
if (desc >= 65500)
{
sys_err("SOCKET FD 65500 LIMIT! %d", desc);
socket_close(s);
return -1;
}
#ifndef __linux__
socket_nonblock(desc);
socket_lingeroff(desc);
#endif
return (desc);
}
socket_t socket_connect(const char* host, WORD port)
{
socket_t s = 0;
struct sockaddr_in server_addr;
int rslt;
/* 소켓주소 구조체 초기화 */
memset(&server_addr, 0, sizeof(server_addr));
if (isdigit(*host))
server_addr.sin_addr.s_addr = inet_addr(host);
else
{
struct hostent *hp;
if ((hp = gethostbyname(host)) == NULL)
{
sys_err("socket_connect(): can not connect to %s:%d", host, port);
return -1;
}
memcpy((char* ) &server_addr.sin_addr, hp->h_addr, sizeof(server_addr.sin_addr));
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
return -1;
}
socket_keepalive(s);
socket_sndbuf(s, 233016);
socket_rcvbuf(s, 233016);
socket_timeout(s, 10, 0);
socket_lingeron(s);
/* 연결요청 */
if ((rslt = connect(s, (struct sockaddr *) &server_addr, sizeof(server_addr))) < 0)
{
socket_close(s);
#ifdef __WIN32__
switch (WSAGetLastError())
#else
switch (rslt)
#endif
{
#ifdef __WIN32__
case WSAETIMEDOUT:
#else
case EINTR:
#endif
sys_err("HOST %s:%d connection timeout.", host, port);
break;
#ifdef __WIN32__
case WSAECONNREFUSED:
#else
case ECONNREFUSED:
#endif
sys_err("HOST %s:%d port is not opened. connection refused.", host, port);
break;
#ifdef __WIN32__
case WSAENETUNREACH:
#else
case ENETUNREACH:
#endif
sys_err("HOST %s:%d is not reachable from this host.", host, port);
break;
default:
sys_err("HOST %s:%d, could not connect.", host, port);
break;
}
perror("connect");
return (-1);
}
return (s);
}
#ifndef __WIN32__
#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif
void socket_nonblock(socket_t s)
{
int flags;
flags = fcntl(s, F_GETFL, 0);
flags |= O_NONBLOCK;
if (fcntl(s, F_SETFL, flags) < 0)
{
sys_err("fcntl: nonblock: %s", strerror(errno));
return;
}
}
void socket_block(socket_t s)
{
int flags;
flags = fcntl(s, F_GETFL, 0);
flags &= ~O_NONBLOCK;
if (fcntl(s, F_SETFL, flags) < 0)
{
sys_err("fcntl: nonblock: %s", strerror(errno));
return;
}
}
#else
void socket_nonblock(socket_t s)
{
unsigned long val = 1;
ioctlsocket(s, FIONBIO, &val);
}
void socket_block(socket_t s)
{
unsigned long val = 0;
ioctlsocket(s, FIONBIO, &val);
}
#endif
void socket_dontroute(socket_t s)
{
int set;
if (setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (const char *) &set, sizeof(int)) < 0)
{
sys_err("setsockopt: dontroute: %s", strerror(errno));
socket_close(s);
return;
}
}
void socket_lingeroff(socket_t s)
{
#if defined(__WIN32__)
int linger;
linger = 0;
#else
struct linger linger;
linger.l_onoff = 0;
linger.l_linger = 0;
#endif
if (setsockopt(s, SOL_SOCKET, SO_LINGER, (const char*) &linger, sizeof(linger)) < 0)
{
sys_err("setsockopt: linger: %s", strerror(errno));
socket_close(s);
return;
}
}
void socket_lingeron(socket_t s)
{
#if defined(__WIN32__)
int linger;
linger = 0;
#else
struct linger linger;
linger.l_onoff = 1;
linger.l_linger = 0;
#endif
if (setsockopt(s, SOL_SOCKET, SO_LINGER, (const char*) &linger, sizeof(linger)) < 0)
{
sys_err("setsockopt: linger: %s", strerror(errno));
socket_close(s);
return;
}
}
void socket_rcvbuf(socket_t s, unsigned int opt)
{
socklen_t optlen;
optlen = sizeof(opt);
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (const char*) &opt, optlen) < 0)
{
sys_err("setsockopt: rcvbuf: %s", strerror(errno));
socket_close(s);
return;
}
opt = 0;
optlen = sizeof(opt);
if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*) &opt, &optlen) < 0)
{
sys_err("getsockopt: rcvbuf: %s", strerror(errno));
socket_close(s);
return;
}
sys_log(1, "SYSTEM: %d: receive buffer changed to %d", s, opt);
}
void socket_sndbuf(socket_t s, unsigned int opt)
{
socklen_t optlen;
optlen = sizeof(opt);
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (const char*) &opt, optlen) < 0)
{
sys_err("setsockopt: sndbuf: %s", strerror(errno));
return;
}
opt = 0;
optlen = sizeof(opt);
if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*) &opt, &optlen) < 0)
{
sys_err("getsockopt: sndbuf: %s", strerror(errno));
return;
}
sys_log(1, "SYSTEM: %d: send buffer changed to %d", s, opt);
}
// sec : seconds, usec : microseconds
void socket_timeout(socket_t s, long sec, long usec)
{
#ifndef __WIN32__
struct timeval rcvopt;
struct timeval sndopt;
socklen_t optlen = sizeof(rcvopt);
rcvopt.tv_sec = sndopt.tv_sec = sec;
rcvopt.tv_usec = sndopt.tv_usec = usec;
#else
socklen_t rcvopt, sndopt;
socklen_t optlen = sizeof(rcvopt);
sndopt = rcvopt = (sec * 1000) + (usec / 1000);
#endif
#ifndef __linux__
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*) &rcvopt, optlen) < 0)
{
sys_err("setsockopt: timeout: %s", strerror(errno));
socket_close(s);
return;
}
if (getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*) &rcvopt, &optlen) < 0)
{
sys_err("getsockopt: timeout: %s", strerror(errno));
socket_close(s);
return;
}
#endif
optlen = sizeof(sndopt);
#ifndef __linux__
if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*) &sndopt, optlen) < 0)
{
sys_err("setsockopt: timeout: %s", strerror(errno));
socket_close(s);
return;
}
if (getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*) &sndopt, &optlen) < 0)
{
sys_err("getsockopt: timeout: %s", strerror(errno));
socket_close(s);
return;
}
#endif
#ifndef __WIN32__
sys_log(1, "SYSTEM: %d: TIMEOUT RCV: %d.%d, SND: %d.%d", s, rcvopt.tv_sec, rcvopt.tv_usec, sndopt.tv_sec, sndopt.tv_usec);
#endif
}
void socket_reuse(socket_t s)
{
int opt = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char*) &opt, sizeof(opt)) < 0)
{
sys_err("setsockopt: reuse: %s", strerror(errno));
socket_close(s);
return;
}
}
void socket_keepalive(socket_t s)
{
int opt = 1;
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (const char*) &opt, sizeof(opt)) < 0)
{
perror("setsockopt: keepalive");
socket_close(s);
return;
}
}

View File

@ -262,7 +262,7 @@ void parse_token(char *src, char *token, char *value)
struct tm * tm_calc(const struct tm * curr_tm, int days)
{
char yoon = FALSE;
bool yoon = false;
static struct tm new_tm;
int monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
@ -281,10 +281,10 @@ struct tm * tm_calc(const struct tm * curr_tm, int days)
if (!((new_tm.tm_year + 1900) % 100))
{
if (!((new_tm.tm_year + 1900) % 400))
yoon = TRUE;
yoon = true;
}
else
yoon = TRUE;
yoon = true;
}
if (yoon)