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>
29 #include "stg/os_int.h"
30 #include "stg/blowfish.h"
31 #include "stg/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();
99 bool CheckHeader(const RS_PACKET_HEADER & header) const;
100 bool GetParams(char * buffer, UserData & data);
102 void ProcessPending();
103 void ProcessTimeouts();
104 bool Disconnect(const UserData & data) const;
105 bool Connect(const UserData & data) const;
108 STG_LOGGER & WriteServLog;
110 mutable std::string errorStr;
111 std::string scriptOnConnect;
112 std::string scriptOnDisconnect;
113 std::string password;
117 bool receiverStopped;
118 bool processorStopped;
119 std::vector<AliveData> users;
120 std::list<PendingData> pending;
123 pthread_t receiverThread;
124 pthread_t processorThread;
125 pthread_mutex_t mutex;
131 friend class DisconnectUser;
134 class DisconnectUser : public std::unary_function<const UserData &, void> {
136 DisconnectUser(LISTENER & l) : listener(l) {};
137 void operator()(const UserData & data)
139 listener.Disconnect(data);
144 //-----------------------------------------------------------------------------