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