]> git.stg.codes - stg.git/blob - projects/sgauthstress/proto.h
A map of ip-to-user replaced with vector of pairs
[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     private:
39         BLOWFISH_CTX ctx;
40         struct sockaddr_in localAddr;
41         struct sockaddr_in serverAddr;
42         int timeout;
43
44         std::vector<std::pair<uint32_t, USER> > users;
45         std::vector<struct pollfd> pollFds;
46
47         bool running;
48         bool stopped;
49
50         pthread_t tid;
51
52         std::string errorStr;
53
54         std::map<std::string, PacketProcessor> processors;
55
56         static void * Runner(void * data);
57
58         void Run();
59         bool RecvPacket();
60         bool SendPacket(const void * buffer, size_t length, USER * user);
61         bool HandlePacket(const char * buffer, USER * user);
62
63         bool CONN_SYN_ACK_Proc(const void * buffer, USER * user);
64         bool ALIVE_SYN_Proc(const void * buffer, USER * user);
65         bool DISCONN_SYN_ACK_Proc(const void * buffer, USER * user);
66         bool FIN_Proc(const void * buffer, USER * user);
67         bool INFO_Proc(const void * buffer, USER * user);
68         bool ERR_Proc(const void * buffer, USER * user);
69
70         bool Send_CONN_SYN(USER * user);
71         bool Send_CONN_ACK(USER * user);
72         bool Send_DISCONN_SYN(USER * user);
73         bool Send_DISCONN_ACK(USER * user);
74         bool Send_ALIVE_ACK(USER * user);
75 };
76
77 #endif