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/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;
109 STG_LOGGER & WriteServLog;
111 mutable std::string errorStr;
112 std::string scriptOnConnect;
113 std::string scriptOnDisconnect;
114 std::string password;
118 bool receiverStopped;
119 bool processorStopped;
120 std::vector<AliveData> users;
121 std::list<PendingData> pending;
124 pthread_t receiverThread;
125 pthread_t processorThread;
126 pthread_mutex_t mutex;
132 friend class DisconnectUser;
135 class DisconnectUser : public std::unary_function<const UserData &, void> {
137 DisconnectUser(LISTENER & l) : listener(l) {};
138 void operator()(const UserData & data)
140 listener.Disconnect(data);
145 //-----------------------------------------------------------------------------