]> git.stg.codes - stg.git/blob - projects/rscriptd/listener.h
У класі підтримки протоколу сервера rscriptd методи шифрування винесені
[stg.git] / projects / rscriptd / listener.h
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include <pthread.h>
23
24 #include <string>
25 #include <vector>
26 #include <list>
27 #include <functional>
28
29 #include "os_int.h"
30 #include "blowfish.h"
31 #include "rs_packets.h"
32 #include "stg_logger.h"
33
34 struct UserData
35 {
36     std::string params;
37     std::string login;
38     uint32_t    ip;
39     uint32_t    id;
40 };
41
42 struct PendingData : public UserData
43 {
44     enum {CONNECT, ALIVE, DISCONNECT} type;
45 };
46
47 struct AliveData : public UserData
48 {
49     explicit AliveData(const UserData & data)
50         : UserData(data),
51           lastAlive(time(NULL))
52     {};
53     bool operator<(const std::string & rvalue) const { return login < rvalue; };
54     time_t      lastAlive;
55 };
56
57 class IsNotTimedOut : public std::unary_function<const AliveData &, bool> {
58     public:
59         IsNotTimedOut(double to) : timeout(to), now(time(NULL)) {}
60         bool operator()(const AliveData & data) const
61         {
62             return difftime(now, data.lastAlive) < timeout;
63         }
64     private:
65         double timeout;
66         time_t now;
67 };
68
69 class LISTENER
70 {
71 public:
72                         LISTENER();
73                         ~LISTENER(){};
74
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; };
80
81     bool                Start();
82     bool                Stop();
83     bool                IsRunning() const { return !receiverStopped && !processorStopped; };
84
85     const std::string & GetStrError() const { return errorStr; };
86     const std::string & GetVersion() const { return version; };
87
88 private:
89     // Threading stuff
90     static void *       Run(void * self);
91     static void *       RunProcessor(void * self);
92     void                Runner();
93     void                ProcessorRunner();
94     // Networking stuff
95     bool                PrepareNet();
96     bool                FinalizeNet();
97     bool                WaitPackets(int sd) const;
98     bool                RecvPacket();
99     // Parsing stuff
100     bool                CheckHeader(const RS_PACKET_HEADER & header) const;
101     bool                GetParams(char * buffer, UserData & data);
102     // Processing stuff
103     void                ProcessPending();
104     void                ProcessTimeouts();
105     bool                Disconnect(const UserData & data) const;
106     bool                Connect(const UserData & data) const;
107
108     BLOWFISH_CTX        ctxS;
109     STG_LOGGER &        WriteServLog;
110
111     mutable std::string errorStr;
112     std::string         scriptOnConnect;
113     std::string         scriptOnDisconnect;
114     std::string         password;
115     uint16_t            port;
116
117     bool                running;
118     bool                receiverStopped;
119     bool                processorStopped;
120     std::vector<AliveData> users;
121     std::list<PendingData> pending;
122     int                 userTimeout;
123
124     pthread_t           receiverThread;
125     pthread_t           processorThread;
126     pthread_mutex_t     mutex;
127
128     int                 listenSocket;
129
130     std::string         version;
131
132     friend class DisconnectUser;
133 };
134
135 class DisconnectUser : public std::unary_function<const UserData &, void> {
136     public:
137         DisconnectUser(LISTENER & l) : listener(l) {};
138         void operator()(const UserData & data)
139         {
140             listener.Disconnect(data);
141         };
142     private:
143         LISTENER & listener;
144 };
145 //-----------------------------------------------------------------------------