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