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