From ee8d66b6d2e48004f5d6ae6b0e81f8dee8132d73 Mon Sep 17 00:00:00 2001 From: NTX Date: Sun, 11 Mar 2012 11:40:49 +0100 Subject: [PATCH] Implement Delayed Functions (Recv, Send, Connect, Disconnect --- Application/Application.cpp | 63 ++++++++++++++++++++++++++++++++++++- Application/Application.h | 26 +++++++++++---- Handling/Handler.cpp | 27 ++++++++++++++-- Handling/Handler.h | 7 ++++- config.conf | 53 +++++++++++++++++++++++++++++-- 5 files changed, 163 insertions(+), 13 deletions(-) diff --git a/Application/Application.cpp b/Application/Application.cpp index 5064782..6070908 100644 --- a/Application/Application.cpp +++ b/Application/Application.cpp @@ -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) @@ -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()); } @@ -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())) ; @@ -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())); @@ -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); } @@ -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) @@ -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()) diff --git a/Application/Application.h b/Application/Application.h index ea08d32..2525f41 100644 --- a/Application/Application.h +++ b/Application/Application.h @@ -30,12 +30,18 @@ typedef std::map 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 @@ -43,6 +49,10 @@ 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 }; @@ -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 diff --git a/Handling/Handler.cpp b/Handling/Handler.cpp index 051fbb6..52f0d46 100644 --- a/Handling/Handler.cpp +++ b/Handling/Handler.cpp @@ -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(); diff --git a/Handling/Handler.h b/Handling/Handler.h index 36137a1..2d8b940 100644 --- a/Handling/Handler.h +++ b/Handling/Handler.h @@ -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 diff --git a/config.conf b/config.conf index 62a7c59..477a2f1 100644 --- a/config.conf +++ b/config.conf @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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