#include <sstream>
#include <algorithm>
+#include "stg/scriptexecuter.h"
+#include "stg/locker.h"
+#include "stg/common.h"
+#include "stg/const.h"
#include "listener.h"
-#include "script_executer.h"
-#include "stg_locker.h"
-#include "common.h"
void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
//-----------------------------------------------------------------------------
LISTENER::LISTENER()
- : WriteServLog(GetStgLogger()),
+ : WriteServLog(STG::Logger::get()),
port(0),
running(false),
receiverStopped(true),
processorStopped(true),
userTimeout(0),
- listenSocket(0)
+ listenSocket(0),
+ version("rscriptd listener v.1.2")
{
-version = "rscriptd listener v.1.2";
-
pthread_mutex_init(&mutex, NULL);
}
//-----------------------------------------------------------------------------
printfd(__FILE__, "LISTENER::Stop()\n");
-usleep(500000);
+struct timespec ts = {0, 500000000};
+nanosleep(&ts, NULL);
if (!processorStopped)
{
//5 seconds to thread stops itself
for (int i = 0; i < 25 && !processorStopped; i++)
{
- usleep(200000);
+ struct timespec ts = {0, 200000000};
+ nanosleep(&ts, NULL);
}
//after 5 seconds waiting thread still running. now killing it
//5 seconds to thread stops itself
for (int i = 0; i < 25 && !receiverStopped; i++)
{
- usleep(200000);
+ struct timespec ts = {0, 200000000};
+ nanosleep(&ts, NULL);
}
//after 5 seconds waiting thread still running. now killing it
//-----------------------------------------------------------------------------
void * LISTENER::Run(void * d)
{
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
LISTENER * listener = static_cast<LISTENER *>(d);
listener->Runner();
//-----------------------------------------------------------------------------
void * LISTENER::RunProcessor(void * d)
{
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
LISTENER * listener = static_cast<LISTENER *>(d);
listener->ProcessorRunner();
while (running)
{
- usleep(500000);
+ struct timespec ts = {0, 500000000};
+ nanosleep(&ts, NULL);
if (!pending.empty())
ProcessPending();
ProcessTimeouts();
struct iovec iov[2];
char buffer[RS_MAX_PACKET_LEN];
-RS_PACKET_HEADER packetHead;
+RS::PACKET_HEADER packetHead;
iov[0].iov_base = reinterpret_cast<char *>(&packetHead);
iov[0].iov_len = sizeof(packetHead);
}
}
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
pending.push_back(data);
return false;
//-----------------------------------------------------------------------------
bool LISTENER::GetParams(char * buffer, UserData & data)
{
-RS_PACKET_TAIL packetTail;
+RS::PACKET_TAIL packetTail;
Decrypt(&ctxS, (char *)&packetTail, buffer, sizeof(packetTail) / 8);
return true;
}
-std::stringstream params;
+std::ostringstream params;
params << "\"" << data.login << "\" "
<< inet_ntostring(data.ip) << " "
<< data.id << " "
//-----------------------------------------------------------------------------
void LISTENER::ProcessPending()
{
-std::list<PendingData> localPending;
-
- {
- STG_LOCKER lock(&mutex, __FILE__, __LINE__);
- printfd(__FILE__, "Pending data size: %d\n", pending.size());
- localPending.swap(pending);
- }
-
-std::list<PendingData>::iterator it(localPending.begin());
-while (it != localPending.end())
+std::list<PendingData>::iterator it(pending.begin());
+size_t count = 0;
+printfd(__FILE__, "Pending: %d\n", pending.size());
+while (it != pending.end() && count < 256)
{
std::vector<AliveData>::iterator uit(
std::lower_bound(
);
if (it->type == PendingData::CONNECT)
{
+ printfd(__FILE__, "Connect packet\n");
if (uit == users.end() || uit->login != it->login)
{
+ printfd(__FILE__, "Connect new user '%s'\n", it->login.c_str());
// Add new user
Connect(*it);
users.insert(uit, AliveData(static_cast<UserData>(*it)));
}
else if (uit->login == it->login)
{
+ printfd(__FILE__, "Update existing user '%s'\n", it->login.c_str());
// Update already existing user
time(&uit->lastAlive);
uit->params = it->params;
}
+ else
+ {
+ printfd(__FILE__, "Hmmm... Strange connect for '%s'\n", it->login.c_str());
+ }
}
else if (it->type == PendingData::ALIVE)
{
+ printfd(__FILE__, "Alive packet\n");
if (uit != users.end() && uit->login == it->login)
{
+ printfd(__FILE__, "Alive user '%s'\n", it->login.c_str());
// Update existing user
time(&uit->lastAlive);
}
+ else
+ {
+ printfd(__FILE__, "Alive user '%s' is not found\n", it->login.c_str());
+ }
}
else if (it->type == PendingData::DISCONNECT)
{
+ printfd(__FILE__, "Disconnect packet\n");
if (uit != users.end() && uit->login == it->login.c_str())
{
+ printfd(__FILE__, "Disconnect user '%s'\n", it->login.c_str());
// Disconnect existing user
+ uit->params = it->params;
Disconnect(*uit);
users.erase(uit);
}
+ else
+ {
+ printfd(__FILE__, "Cannot find user '%s' for disconnect\n", it->login.c_str());
+ }
+ }
+ else
+ {
+ printfd(__FILE__, "Unknown packet type\n");
}
++it;
+ ++count;
}
+STG_LOCKER lock(&mutex);
+pending.erase(pending.begin(), it);
}
//-----------------------------------------------------------------------------
void LISTENER::ProcessTimeouts()
printfd(__FILE__, "Connect %s\n", data.login.c_str());
if (access(scriptOnConnect.c_str(), X_OK) == 0)
{
- if (ScriptExec(scriptOnConnect + " " + data.params))
+ if (ScriptExec((scriptOnConnect + " " + data.params).c_str()))
{
WriteServLog("Script %s cannot be executed for an unknown reason.", scriptOnConnect.c_str());
return true;
printfd(__FILE__, "Disconnect %s\n", data.login.c_str());
if (access(scriptOnDisconnect.c_str(), X_OK) == 0)
{
- if (ScriptExec(scriptOnDisconnect + " " + data.params))
+ if (ScriptExec((scriptOnDisconnect + " " + data.params).c_str()))
{
WriteServLog("Script %s cannot be executed for an unknown reson.", scriptOnDisconnect.c_str());
return true;
return false;
}
//-----------------------------------------------------------------------------
-bool LISTENER::CheckHeader(const RS_PACKET_HEADER & header) const
+bool LISTENER::CheckHeader(const RS::PACKET_HEADER & header) const
{
if (strncmp((char *)header.magic, RS_ID, RS_MAGIC_LEN))
{
return false;
}
//-----------------------------------------------------------------------------
-bool LISTENER::WaitPackets(int sd) const
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
- {
- if (errno != EINTR)
- {
- printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
- }
- return false;
- }
-
-if (res == 0) // Timeout
- {
- return false;
- }
-
-return true;
-}
-//-----------------------------------------------------------------------------
inline
void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
{