Skip to content

Commit

Permalink
Implement Delayed Functions (Recv, Send, Connect, Disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
NTX authored and NTX committed Mar 11, 2012
1 parent 2e3a4ce commit ee8d66b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 13 deletions.
63 changes: 62 additions & 1 deletion Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,28 @@ void Application::LoadConfigs()
LoadStringConfig("Protocol.BindIp", CONFIG_STRING_BIND_IP, "127.0.0.1");
LoadStringConfig("Protocol.ProtocolName", CONFIG_STRING_PROTOCOL_NAME);
LoadStringConfig("Protocol.LibraryPath", CONFIG_STRING_LIBRARY_PATH);

LoadStringConfig("Protocol.RecvFunc", CONFIG_STRING_RECV_FUNC);
LoadStringConfig("Protocol.SendFunc", CONFIG_STRING_SEND_FUNC);
LoadStringConfig("Protocol.ConnectFunc", CONFIG_STRING_CONNECT_FUNC);
LoadStringConfig("Protocol.DisconnectFunc", CONFIG_STRING_DISCONNECT_FUNC);

LoadStringConfig("Protocol.LoadFunc", CONFIG_STRING_LOAD_FUNC);
LoadStringConfig("Protocol.UnloadFunc", CONFIG_STRING_UNLOAD_FUNC);

LoadStringConfig("Protocol.DelayedRecvFunc", CONFIG_STRING_DELAYED_RECV_FUNC);
LoadIntConfig("Protocol.DelayedRecvDelay", CONFIG_INT_DELAYED_RECV_DELAY, 300);

LoadStringConfig("Protocol.DelayedSendFunc", CONFIG_STRING_DELAYED_SEND_FUNC);
LoadIntConfig("Protocol.DelayedSendDelay", CONFIG_INT_DELAYED_SEND_DELAY, 300);

LoadStringConfig("Protocol.DelayedConnectFunc", CONFIG_STRING_DELAYED_CONNECT_FUNC);
LoadIntConfig("Protocol.DelayedConnectDelay", CONFIG_INT_DELAYED_CONNECT_DELAY, 300);

LoadStringConfig("Protocol.DelayedDisconnectFunc", CONFIG_STRING_DELAYED_DISCONNECT_FUNC);
LoadIntConfig("Protocol.DelayedDisconnectDelay", CONFIG_INT_DELAYED_DISCONNECT_DELAY, 300);


}

if (RunOptions.size() > 0)
Expand Down Expand Up @@ -198,6 +214,16 @@ void Application::outDebugParams() const
sLog->outString("CONFIG_STRING_SEND_FUNC='%s'", StringConfigs[CONFIG_STRING_SEND_FUNC].c_str());
sLog->outString("CONFIG_STRING_CONNECT_FUNC='%s'", StringConfigs[CONFIG_STRING_CONNECT_FUNC].c_str());
sLog->outString("CONFIG_STRING_DISCONNECT_FUNC='%s'", StringConfigs[CONFIG_STRING_DISCONNECT_FUNC].c_str());

sLog->outString("CONFIG_STRING_DELAYED_RECV_FUNC='%s'", StringConfigs[CONFIG_STRING_DELAYED_RECV_FUNC].c_str());
sLog->outString("CONFIG_INT_DELAYED_RECV_DELAY=%d", IntConfigs[CONFIG_INT_DELAYED_RECV_DELAY]);
sLog->outString("CONFIG_STRING_DELAYED_SEND_FUNC='%s'", StringConfigs[CONFIG_STRING_DELAYED_SEND_FUNC].c_str());
sLog->outString("CONFIG_INT_DELAYED_SEND_DELAY=%d", IntConfigs[CONFIG_INT_DELAYED_SEND_DELAY]);
sLog->outString("CONFIG_STRING_DELAYED_CONNECT_FUNC='%s'", StringConfigs[CONFIG_STRING_DELAYED_CONNECT_FUNC].c_str());
sLog->outString("CONFIG_INT_DELAYED_CONNECT_DELAY=%d", IntConfigs[CONFIG_INT_DELAYED_CONNECT_DELAY]);
sLog->outString("CONFIG_STRING_DELAYED_DISCONNECT_FUNC='%s'", StringConfigs[CONFIG_STRING_DELAYED_DISCONNECT_FUNC].c_str());
sLog->outString("CONFIG_INT_DELAYED_DISCONNECT_DELAY=%d", IntConfigs[CONFIG_INT_DELAYED_DISCONNECT_DELAY]);

sLog->outString("CONFIG_STRING_LOAD_FUNC='%s'", StringConfigs[CONFIG_STRING_LOAD_FUNC].c_str());
sLog->outString("CONFIG_STRING_UNLOAD_FUNC='%s'", StringConfigs[CONFIG_STRING_UNLOAD_FUNC].c_str());
}
Expand Down Expand Up @@ -313,6 +339,8 @@ bool Application::LoadLibrary()
if (!libLoader->open(StringConfigs[CONFIG_STRING_LIBRARY_PATH].c_str()))
return libLoaded = false;

// Normal Event Functions

if (StringConfigs[CONFIG_STRING_SEND_FUNC].length())
proto.OnSend = onSend(libLoader->findFunc(StringConfigs[CONFIG_STRING_SEND_FUNC].c_str())) ;

Expand All @@ -325,6 +353,22 @@ bool Application::LoadLibrary()
if (StringConfigs[CONFIG_STRING_DISCONNECT_FUNC].length())
proto.OnDisconnect = onDisconnect(libLoader->findFunc(StringConfigs[CONFIG_STRING_DISCONNECT_FUNC].c_str()));

// Delayed Event Functions

if (StringConfigs[CONFIG_STRING_DELAYED_SEND_FUNC].length())
proto.OnSendDelayed = onSend(libLoader->findFunc(StringConfigs[CONFIG_STRING_DELAYED_SEND_FUNC].c_str())) ;

if (StringConfigs[CONFIG_STRING_DELAYED_RECV_FUNC].length())
proto.OnRecieveDelayed = onRecieve(libLoader->findFunc(StringConfigs[CONFIG_STRING_DELAYED_RECV_FUNC].c_str())) ;

if (StringConfigs[CONFIG_STRING_DELAYED_CONNECT_FUNC].length())
proto.OnConnectDelayed = onConnect(libLoader->findFunc(StringConfigs[CONFIG_STRING_DELAYED_CONNECT_FUNC].c_str()));

if (StringConfigs[CONFIG_STRING_DELAYED_DISCONNECT_FUNC].length())
proto.OnDisconnectDelayed = onDisconnect(libLoader->findFunc(StringConfigs[CONFIG_STRING_DELAYED_DISCONNECT_FUNC].c_str()));

// Load / Unload

if (StringConfigs[CONFIG_STRING_LOAD_FUNC].length())
proto.OnLoad = onLoad(libLoader->findFunc(StringConfigs[CONFIG_STRING_LOAD_FUNC].c_str()));

Expand Down Expand Up @@ -490,11 +534,16 @@ uint32 Application::Update()
sLog->outDebug("[Recv Thread] Recieved New Connection From: %s FD: %d", inet_ntoa(peer.sin_addr), newSocket);
if (proto.OnConnect)
{
//handler->AddDelayedEvent(DelayedEvent(EVENT_CONNECT, newSocket, peer.sin_addr, 100));
handler->AddEvent(Event(EVENT_CONNECT, newSocket, peer.sin_addr));
if (threadMgr->GetThreadStatus(ProcessQueue) == THREAD_SUSPENDED)
sLog->outDebug("[Recv Thread] Resuming Queue thread: %d Status: %s", ProcessQueue, runStatus(threadMgr->ResumeThread(ProcessQueue)));
}
if (proto.OnConnectDelayed)
{
handler->AddDelayedEvent(DelayedEvent(EVENT_CONNECT_DELAYED, newSocket, peer.sin_addr, IntConfigs[CONFIG_INT_DELAYED_SEND_DELAY]));
if (threadMgr->GetThreadStatus(ProcessQueue) == THREAD_SUSPENDED)
sLog->outDebug("[Recv Thread] Resuming Queue thread: %d Status: %s", ProcessQueue, runStatus(threadMgr->ResumeThread(ProcessQueue)));
}
FD_SET(newSocket, &Recv);
}

Expand All @@ -517,6 +566,12 @@ uint32 Application::Update()
if (threadMgr->GetThreadStatus(ProcessQueue) == THREAD_SUSPENDED)
sLog->outDebug("[Recv Thread] Resuming Queue thread: %d Status: %s", ProcessQueue, runStatus(threadMgr->ResumeThread(ProcessQueue)));
}
if (proto.OnDisconnectDelayed)
{
handler->AddDelayedEvent(DelayedEvent(EVENT_DISCONNECT_DELAYED, sock->first, sock->second->GetAddr().sin_addr, IntConfigs[CONFIG_INT_DELAYED_DISCONNECT_DELAY]));
if (threadMgr->GetThreadStatus(ProcessQueue) == THREAD_SUSPENDED)
sLog->outDebug("[Recv Thread] Resuming Queue thread: %d Status: %s", ProcessQueue, runStatus(threadMgr->ResumeThread(ProcessQueue)));
}
}
else
if (recieved > 0)
Expand All @@ -527,6 +582,12 @@ uint32 Application::Update()
if (threadMgr->GetThreadStatus(ProcessQueue) == THREAD_SUSPENDED)
sLog->outDebug("[Recv Thread] Resuming Queue thread: %d Status: %s", ProcessQueue, runStatus(threadMgr->ResumeThread(ProcessQueue)));
}
if (proto.OnRecieveDelayed)
{
handler->AddDelayedEvent(DelayedEvent(EVENT_RECIEVE_DELAYED, sock->first, sock->second->GetAddr().sin_addr, IntConfigs[CONFIG_INT_DELAYED_DISCONNECT_DELAY], *data, recieved, sock->second->errnum()));
if (threadMgr->GetThreadStatus(ProcessQueue) == THREAD_SUSPENDED)
sLog->outDebug("[Recv Thread] Resuming Queue thread: %d Status: %s", ProcessQueue, runStatus(threadMgr->ResumeThread(ProcessQueue)));
}
}
else
switch (sock->second->errnum())
Expand Down
26 changes: 20 additions & 6 deletions Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,29 @@ typedef std::map<string, string, StringComparsionObject> Options;

struct Protocol
{
onConnect OnConnect;
onRecieve OnRecieve;
onSend OnSend;
onDisconnect OnDisconnect;
onLoad OnLoad;
onUnload OnUnload;
onConnect OnConnect;
onRecieve OnRecieve;
onSend OnSend;
onDisconnect OnDisconnect;

onConnect OnConnectDelayed;
onRecieve OnRecieveDelayed;
onSend OnSendDelayed;
onDisconnect OnDisconnectDelayed;

onLoad OnLoad;
onUnload OnUnload;
};

enum IntOptions
{
CONFIG_INT_BIND_PORT,
CONFIG_INT_LOG_LEVEL,
CONFIG_INT_FREEZE_DETECTOR_MAX_DIFF,
CONFIG_INT_DELAYED_SEND_DELAY,
CONFIG_INT_DELAYED_RECV_DELAY,
CONFIG_INT_DELAYED_CONNECT_DELAY,
CONFIG_INT_DELAYED_DISCONNECT_DELAY,
CONFIG_INT_MAX
};

Expand All @@ -54,9 +64,13 @@ enum StringOptions
CONFIG_STRING_PROTOCOL_NAME,
CONFIG_STRING_LIBRARY_PATH,
CONFIG_STRING_RECV_FUNC,
CONFIG_STRING_DELAYED_RECV_FUNC,
CONFIG_STRING_SEND_FUNC,
CONFIG_STRING_DELAYED_SEND_FUNC,
CONFIG_STRING_CONNECT_FUNC,
CONFIG_STRING_DELAYED_CONNECT_FUNC,
CONFIG_STRING_DISCONNECT_FUNC,
CONFIG_STRING_DELAYED_DISCONNECT_FUNC,
CONFIG_STRING_LOAD_FUNC,
CONFIG_STRING_UNLOAD_FUNC,
CONFIG_STRING_MAX
Expand Down
27 changes: 25 additions & 2 deletions Handling/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,34 @@ void PacketHandler::ProcessQueue()
delete &event.data;
break;
case EVENT_SEND:
break;
if (app->proto.OnSend)
app->proto.OnSend(event.data);
delete &event.data;
break;
case EVENT_DISCONNECT:
if (app->proto.OnDisconnect)
app->proto.OnDisconnect(event.address, event.fd);
break;
break;
case EVENT_CONNECT_DELAYED:
if (app->proto.OnConnectDelayed)
app->proto.OnConnectDelayed(event.address, event.fd);
break;
case EVENT_RECIEVE_DELAYED:
if (app->proto.OnRecieveDelayed)
app->proto.OnRecieveDelayed(event.address, event.fd, event.data);
delete &event.data;
break;
case EVENT_SEND_DELAYED:
if (app->proto.OnSendDelayed)
app->proto.OnSendDelayed(event.data);
delete &event.data;
break;
case EVENT_DISCONNECT_DELAYED:
if (app->proto.OnDisconnectDelayed)
app->proto.OnDisconnectDelayed(event.address, event.fd);
break;
default:
sLog->outError("Unimplemented EventType: %d", event.EventType);
}
//sLog->outDebug("Process Queue Update after diff: %lu milliseconds", getMsTimeDiffToNow(workBegan));
queue.pop();
Expand Down
7 changes: 6 additions & 1 deletion Handling/Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ enum Events
EVENT_CONNECT = 0,
EVENT_RECIEVE,
EVENT_SEND,
EVENT_DISCONNECT
EVENT_DISCONNECT,

EVENT_CONNECT_DELAYED,
EVENT_RECIEVE_DELAYED,
EVENT_SEND_DELAYED,
EVENT_DISCONNECT_DELAYED,
};

struct Event
Expand Down
53 changes: 50 additions & 3 deletions config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Server.LogLevel = 3
# 1 - Enabled
Server.Log = 1




# Server.Daemonize -- bool value
# invokes main application to run as daemon
# 0 - Disabled
Expand All @@ -27,6 +30,9 @@ Server.PidFile = /var/lock/ikaros.pid
# Default: .
#Server.WorkingDirectory = .




# Server.EnableFreezeDetector -- bool value
# enables Freeze Detector of handling thread
# 0 - Disabled
Expand All @@ -38,6 +44,9 @@ Server.EnableFreezeDetector = 0
# values are in milliseconds
Server.FreezeDetectorMaxDiff = 30000




# Protocol.BindPort -- short value ( 0 - 65535 )
# sets main listening socket bind port
Protocol.BindPort = 35000
Expand All @@ -51,6 +60,12 @@ Protocol.BindIp = 0.0.0.0
# sets protocol name
Protocol.ProtocolName = HTTP

# Protocol.LibraryPath -- string value
# sets path to library
Protocol.LibraryPath = ./libFTP.so



# Protocol.RecvFunc -- string value
# sets protocol onRecieve function name
Protocol.RecvFunc = processRecieve
Expand All @@ -67,6 +82,41 @@ Protocol.ConnectFunc = processConnect
# sets protocol onDisconnect function name
Protocol.DisconnectFunc = processDisconnect



# Protocol.DelayedRecvFunc -- string value
# sets protocol onRecieveDelayed function name
#Protocol.DelayedRecvFunc =

# Protocol.DelayedRecvDelay -- int value
# sets protocol onRecieveDelayed delay time
#Protocol.DelayedRecvDelay =

# Protocol.DelayedSendFunc -- string value
# sets protocol onSendDelayed function name
#Protocol.DelayedSendFunc =

# Protocol.DelayedSendDelay -- int value
# sets protocol onSendDelayed delay time
#Protocol.DelayedSendDelay =

# Protocol.DelayedConnectFunc -- string value
# sets protocol onConnectDelayed function name
#Protocol.DelayedConnectFunc =

# Protocol.DelayedConnectDelay -- int value
# sets protocol onConnectDelayed delay time
#Protocol.DelayedConnectDelay =

# Protocol.DelayedDisconnectFunc -- string value
# sets protocol onDisconnectDelayed function name
#Protocol.DelayedDisconnectFunc =

# Protocol.DelayedDisconnectDelay -- int value
# sets protocol onDisconnectDelayed delay time
#Protocol.DelayedDisconnectDelay =


# Protocol.LoadFunc -- string value
# sets protocol onLoad function name
# called when main application loads library
Expand All @@ -77,6 +127,3 @@ Protocol.LoadFunc = Init
# called before main application frees library
#Protocol.UnloadFunc =

# Protocol.LibraryPath -- string value
# sets path to library
Protocol.LibraryPath = ./libFTP.so

0 comments on commit ee8d66b

Please sign in to comment.