-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.h
225 lines (186 loc) · 4.42 KB
/
protocol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef PROTOCOL_H_INCLUDED
#define PROTOCOL_H_INCLUDED
#include "stdint.h"
#include <netinet/in.h>
#include <pthread.h>
#define MSGINCOR -1
#define TOOMANYUSER -2
#define SMIPNPORT -3
#define SMNAME -4
#define NOSUCHUSER -5
#define BUFTOOSM -6
#define DISCONNECT -7
#define CANNOTLOGIN -8
#define TOOMANYCONNECTION -9
#define EMPTY -10
#define UNKNOWN -11
#define NOTFIND -12
#define MSGL 512
#define LMSGL 5120
#define MSGTYPE int8_t
#define TYPEA 1
#define TYPEB 0
#define MYMSG uint8_t
#define ERRCOD int8_t
#define STA int8_t
#define LOGIN 0x01
#define LOGIN_OK 0x02
#define GET_LIST 0x03
#define GET_LIST_OK 0x04
#define HELLO 0x10
#define HELLO_OK 0x20
#define MSG 0x30
#define ERROR 0xff
#define DIS 0
#define CONNECTING 1
#define CONNECTED 2
#define BUFMAX 100
#define MAXUSER 10
void err(char const place[]);
void err(char const place[],int code);
uint32_t copy2msg(MYMSG* destMsg,uint32_t length,uint8_t content[],uint32_t msgPos);
uint32_t copy2msg(MYMSG* destMsg,uint16_t content,uint32_t msgPos,bool hasLength);
uint32_t copy2msg(MYMSG* destMsg,uint32_t content,uint32_t msgPos,bool hasLength);
uint32_t fetchFmsg(MYMSG* sourMsg,uint32_t* result,uint32_t msgPos);
uint32_t fetchFmsg(MYMSG* sourMsg,uint16_t* result,uint32_t msgPos);
class restMsgBuf
{
public:
restMsgBuf* next;
MYMSG content[MSGL];
int msgLength;
restMsgBuf();
restMsgBuf(MYMSG* sourMsg, uint32_t length);
void insert(MYMSG* sourMsg,uint32_t length);
ERRCOD get(MYMSG* destMsg,uint32_t* length);
ERRCOD getTypeA(MYMSG* destMsg,uint32_t* length);
ERRCOD getTypeB(MYMSG* destMsg,uint32_t* length);
void print();
};
class msgReceiver
{
private:
uint8_t inBuf[LMSGL];
uint32_t inLength;
int conSocket;
restMsgBuf* rStart;
pthread_mutex_t mutex;
pthread_cond_t cond;
public:
msgReceiver(int conSocket);
~msgReceiver();
ERRCOD receiveTypeAMsg(MYMSG* destMsg, uint32_t* msgLength);
ERRCOD receiveTypeBMsg(MYMSG* destMsg, uint32_t* msgLength);
ERRCOD receiveMsg(MYMSG* destMsg,uint32_t* msgLength);
ERRCOD receiveMsg(MYMSG* destMsg);
};
class userInf
{
public:
char name[512];
uint32_t ipAddr;
uint16_t portNum;
uint32_t nameLength;
userInf* next;
userInf();
};
class recMsg
{
public:
char msgContent[512];
char fromName[512];
recMsg* next;
recMsg();
};
class clientList
{
private:
uint32_t userNum;
userInf* start;
public:
ERRCOD addUser(MYMSG* sourMsg, uint32_t ipAddr,userInf** thisUser);
ERRCOD delUser(char delName[]);
int getList(MYMSG* destMsg);
clientList();
~clientList();
};
class recMsgBuf
{
private:
uint32_t unreadNum;
recMsg* start;
public:
recMsgBuf();
~recMsgBuf();
ERRCOD addMsg(MYMSG* sourMsg,char name[]);
int getAllMsg(recMsg* destArr);
ERRCOD delAllMsg();
int getUnreadNum();
};
class clientConnected
{
public:
char name[512];
int socketFd;
bool connected;
msgReceiver* rec;
clientConnected* next;
clientConnected();
};
class clientConnectedList
{
private:
clientConnected* start;
public:
clientConnectedList();
~clientConnectedList();
ERRCOD makeConnection(char* screenName);
ERRCOD connectWith(char* screenName,int socket,msgReceiver** rec);
ERRCOD disconnectWith(char* screenName);
int connectionStatus(char* screenName);
int connectionStatus(char* screenName,int* destSocket, msgReceiver** rec);
};
class sendMsg
{
public:
char content[MSGL];
uint32_t msgLength;
sendMsg* next;
sendMsg();
};
class sendMsgBuf
{
public:
sendMsg* start;
bool terminate;
ERRCOD insertMsg(char* soutStr);
ERRCOD cutMsg(MYMSG* destMsg,uint32_t* length);
void thdStart();
void thdFinish();
bool isEmpty();
bool isTerminate();
sendMsgBuf();
~sendMsgBuf();
};
struct conInf
{
int* conSocket;
struct sockaddr_in* conAddr;
};
struct conInfwRec
{
int* conSocket;
struct sockaddr_in* conAddr;
msgReceiver* rec;
};
ERRCOD clientListParse(MYMSG* sourMsg, userInf* destStr, int* num);
int createLoginMsg(MYMSG* destMsg,char* userName, uint16_t portNum);
int createLoginOkMsg(MYMSG* destMsg);
int createGetListMsg(MYMSG* destMsg);
int createErrMsg(MYMSG* destMsg,uint8_t errCode);
int createHelloMsg(MYMSG* destMsg,char* sName);
int createHelloOkMsg(MYMSG* destMsg);
int createMsg(MYMSG* destMsg, char* msgContent);
int getArgNum(uint8_t);
MSGTYPE getMsgType(MYMSG* msg);
#endif // PROTOCOL_H_INCLUDED