]> git.stg.codes - stg.git/blob - projects/sgauthstress/proto.h
Users container changed from vector to list
[stg.git] / projects / sgauthstress / proto.h
1 #ifndef __PROTO_H__
2 #define __PROTO_H__
3
4 #include <netinet/ip.h>
5 #include <pthread.h>
6 #include <poll.h>
7
8 #include <string>
9 #include <list>
10 #include <vector>
11 #include <map>
12
13 #include "stg/os_int.h"
14 #include "stg/blowfish.h"
15
16 #include "user.h"
17
18 class PROTO;
19
20 typedef bool (PROTO::*PacketProcessor)(const void *, USER *);
21
22 class PROTO {
23     public:
24         PROTO(const std::string & server,
25               uint16_t port,
26               uint16_t localPort,
27               int timeout = 1);
28         ~PROTO();
29
30         bool Start();
31         bool Stop();
32
33         const std::string GetStrError() const { return errorStr; }
34
35         void AddUser(const USER & user, bool connect = false);
36
37         bool Connect(uint32_t ip);
38         bool Disconnect(uint32_t ip);
39
40         size_t UserCount() const { return users.size(); }
41     private:
42         BLOWFISH_CTX ctx;
43         struct sockaddr_in localAddr;
44         struct sockaddr_in serverAddr;
45         int timeout;
46
47         std::list<std::pair<uint32_t, USER> > users;
48         std::vector<struct pollfd> pollFds;
49
50         bool running;
51         bool stopped;
52
53         pthread_t tid;
54         pthread_mutex_t mutex;
55
56         std::string errorStr;
57
58         std::map<std::string, PacketProcessor> processors;
59
60         static void * Runner(void * data);
61
62         void Run();
63         bool RecvPacket();
64         bool SendPacket(const void * buffer, size_t length, USER * user);
65         bool HandlePacket(const char * buffer, size_t length, USER * user);
66
67         bool CONN_SYN_ACK_Proc(const void * buffer, USER * user);
68         bool ALIVE_SYN_Proc(const void * buffer, USER * user);
69         bool DISCONN_SYN_ACK_Proc(const void * buffer, USER * user);
70         bool FIN_Proc(const void * buffer, USER * user);
71         bool INFO_Proc(const void * buffer, USER * user);
72         bool ERR_Proc(const void * buffer, USER * user);
73
74         bool Send_CONN_SYN(USER * user);
75         bool Send_CONN_ACK(USER * user);
76         bool Send_DISCONN_SYN(USER * user);
77         bool Send_DISCONN_ACK(USER * user);
78         bool Send_ALIVE_ACK(USER * user);
79
80         bool RealConnect(USER * user);
81         bool RealDisconnect(USER * user);
82 };
83
84 #endif