2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
31 #include "rs_packets.h"
32 #include "stg_logger.h"
42 struct PendingData : public UserData
44 enum {CONNECT, ALIVE, DISCONNECT} type;
47 struct AliveData : public UserData
49 explicit AliveData(const UserData & data)
53 bool operator<(const std::string & rvalue) const { return login < rvalue; };
57 class IsNotTimedOut : public std::unary_function<const AliveData &, bool> {
59 IsNotTimedOut(double to) : timeout(to), now(time(NULL)) {}
60 bool operator()(const AliveData & data) const
62 return difftime(now, data.lastAlive) < timeout;
75 void SetPort(uint16_t p) { port = p; };
76 void SetPassword(const std::string & p);
77 void SetUserTimeout(int t) { userTimeout = t; };
78 void SetScriptOnConnect(const std::string & script) { scriptOnConnect = script; };
79 void SetScriptOnDisconnect(const std::string & script) { scriptOnDisconnect = script; };
83 bool IsRunning() const { return !receiverStopped && !processorStopped; };
85 const std::string & GetStrError() const { return errorStr; };
86 const std::string & GetVersion() const { return version; };
90 static void * Run(void * self);
91 static void * RunProcessor(void * self);
93 void ProcessorRunner();
97 bool WaitPackets(int sd) const;
100 bool CheckHeader(const RS_PACKET_HEADER & header) const;
101 bool GetParams(char * buffer, UserData & data);
103 void ProcessPending();
104 void ProcessTimeouts();
105 bool Disconnect(const UserData & data) const;
106 bool Connect(const UserData & data) const;
108 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
109 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
112 STG_LOGGER & WriteServLog;
114 mutable std::string errorStr;
115 std::string scriptOnConnect;
116 std::string scriptOnDisconnect;
117 std::string password;
121 bool receiverStopped;
122 bool processorStopped;
123 std::vector<AliveData> users;
124 std::list<PendingData> pending;
127 pthread_t receiverThread;
128 pthread_t processorThread;
129 pthread_mutex_t mutex;
135 friend class DisconnectUser;
138 class DisconnectUser : public std::unary_function<const UserData &, void> {
140 DisconnectUser(LISTENER & l) : listener(l) {};
141 void operator()(const UserData & data)
143 listener.Disconnect(data);
148 //-----------------------------------------------------------------------------