1+ #include < sys/socket.h>
2+ #include < sys/types.h>
3+ #include < cassert>
4+ #include < cstring>
5+ #include < netinet/in.h>
6+ #include < arpa/inet.h>
7+ #include < cstdlib>
8+ #include < cstdio>
9+ #include < unistd.h>
10+ #include < poll.h>
11+ #include < fcntl.h>
12+ #include < cerrno>
13+ #include < string>
14+
15+ #define BUFFER_SIZE 1024
16+
17+ #define B_GETDESK 1
18+ #define B_ENTER 2
19+ #define B_GAMESTATUS 3
20+ #define B_GAME 4
21+ #define B_GAME_RANGE 5
22+ #define B_GAME_RESULT 6
23+
24+ void ProcessMsgCenter (const char * msg);
25+ int now_game_status = 1 ;
26+ bool can_guess_num = true ;
27+
28+ int main (int argc, char * argv[]) {
29+
30+
31+ if (argc <= 2 )
32+ {
33+ printf (" Wrong number of parameters" );
34+ return 1 ;
35+ }
36+
37+ char * ip = argv[1 ];
38+ int port = atoi (argv[2 ]);
39+
40+ struct sockaddr_in host_address{};
41+ host_address.sin_port = htons (port);
42+ host_address.sin_family = AF_INET;
43+ inet_pton (AF_INET, ip, &host_address.sin_addr );
44+
45+ int host_sock = socket (PF_INET, SOCK_STREAM, 0 );
46+ assert (host_sock >= 0 );
47+
48+ int ret = connect (host_sock, (struct sockaddr *)&host_address, sizeof (host_address));
49+ assert (ret != -1 );
50+
51+ pollfd pds[2 ];
52+ pds[0 ].fd = 0 ;
53+ pds[0 ].events = POLLIN;
54+ pds[0 ].revents = 0 ;
55+ pds[1 ].fd = host_sock;
56+ pds[1 ].events = POLLIN | POLLRDHUP;
57+ pds[1 ].revents = 0 ;
58+
59+ assert (ret != -1 );
60+
61+ char buff[BUFFER_SIZE];
62+
63+ printf (" %s" , " 输入任意内容回车获取房间信息\n " );
64+
65+ while (1 )
66+ {
67+
68+ ret = poll (pds, 2 , -1 );
69+ if (ret <= 0 )
70+ {
71+ printf (" Poll error\n " );
72+ break ;
73+ }
74+
75+ if (pds[0 ].revents & POLLIN)
76+ {
77+ memset (buff, ' \0 ' , BUFFER_SIZE);
78+ int recv_length = read (pds[0 ].fd , buff, BUFFER_SIZE - 1 );
79+ buff[strlen (buff) - 1 ] = ' \0 ' ;
80+
81+ if (buff[0 ] == ' &' )
82+ {
83+ now_game_status++;
84+ continue ;
85+ }
86+
87+ if (now_game_status == 4 && !can_guess_num)
88+ {
89+
90+ printf (" 现在没有轮到你, 请等待\n " );
91+ continue ;
92+ }
93+ if (now_game_status == 4 )
94+ {
95+ can_guess_num = false ;
96+ }
97+ std::string the_msg = " {" + std::to_string (now_game_status) + " @" + buff + " }" ;
98+ send (host_sock, the_msg.c_str (), the_msg.length (), 0 );
99+ }
100+
101+ if (pds[0 ].revents & POLLRDHUP)
102+ {
103+ const char * msg = " lose connection" ;
104+ printf (" %s" , msg);
105+ }
106+
107+ if (pds[1 ].revents & POLLIN)
108+ {
109+ memset (buff, ' \0 ' , BUFFER_SIZE);
110+ read (pds[1 ].fd , buff, BUFFER_SIZE - 1 );
111+
112+
113+ ProcessMsgCenter (buff);
114+ }
115+
116+ }
117+
118+ return 0 ;
119+ }
120+
121+ void PlayGame () {
122+
123+ }
124+
125+ void EnterRoom (int msg_bool, int msg_length, char * true_msg) {
126+ if (msg_bool == 1 ) {
127+ printf (" 已有%d-游戏开始所需人数%d\n " , true_msg[0 ] - 48 , true_msg[2 ] - 48 );
128+ now_game_status = 3 ;
129+ }
130+ else
131+ {
132+ printf (" %s\n " ," 进入房间失败, 请进入其它房间\n " );
133+ }
134+
135+ }
136+
137+ void MsgProcess (int msg_type, int msg_type_bool, int msg_length, char * true_msg) {
138+ switch (msg_type)
139+ {
140+ case B_GETDESK:
141+ printf (" 请选择房间加入\n %s\n " , true_msg);
142+ now_game_status = 2 ;
143+ break ;
144+ case B_ENTER:
145+ EnterRoom (msg_type_bool, msg_length, true_msg);
146+ break ;
147+ case B_GAMESTATUS:
148+ printf (" 游戏开始\n " );
149+ now_game_status = 4 ;
150+ break ;
151+ case B_GAME:
152+ printf (" %s\n " ,true_msg);
153+ can_guess_num = msg_type_bool;
154+ break ;
155+ case B_GAME_RANGE:
156+ printf (" 当前范围%s\n " ,true_msg);
157+
158+ break ;
159+ case B_GAME_RESULT:
160+ printf (" 玩家%s胜利\n 输入任意内容回车获取房间信息\n " ,true_msg);
161+ now_game_status = 1 ;
162+ break ;
163+ }
164+ }
165+
166+ void GetInfoFromMsg (const char * msg) {
167+
168+ int msg_type = msg[0 ] - 48 ;
169+ int msg_type_bool = msg[1 ] - 48 ;
170+ char msg_length_char[3 ] = {};
171+ int msg_length_char_sub = 0 ;
172+ bool is_first_flag = true ;
173+ int msg_length = strlen (msg);
174+ for (int i = 0 ; i < msg_length; ++i)
175+ {
176+ if (is_first_flag &&msg[i] == ' @' )
177+ {
178+ msg_length_char[msg_length_char_sub] = msg[i + 1 ];
179+ msg_length_char_sub++;
180+ if (msg[i + 2 ] == ' @' )
181+ {
182+ break ;
183+ }
184+
185+ is_first_flag = false ;
186+ continue ;
187+ }
188+ if (!is_first_flag && msg[i] == ' @' )
189+ {
190+ msg_length_char[msg_length_char_sub] = msg[i - 1 ];
191+ break ;
192+ }
193+ }
194+
195+ int msg_true_length = atoi (msg_length_char);
196+ char true_msg[1024 ] = {};
197+ strcpy (true_msg, msg + msg_length - msg_true_length);
198+ MsgProcess (msg_type, msg_type_bool, msg_true_length, true_msg);
199+ }
200+
201+ void ProcessMsgCenter (const char * msg)
202+ {
203+ bool left_bracket_falg = false ;
204+ bool right_bracket_falg = false ;
205+
206+ static char recv_buffer [1024 ];
207+ static char recv_buffer_last[1024 ];
208+
209+ std::string the_msg = msg;
210+
211+ int the_msg_length = (int )the_msg.length ();
212+
213+ int begin_sub = 0 ;
214+ for (int i = 0 ; i < the_msg_length; ++i)
215+ {
216+
217+ if (the_msg[i] == ' {' )
218+ {
219+ left_bracket_falg = true ;
220+ begin_sub = i + 1 ;
221+ memset (recv_buffer_last, ' \0 ' , 1024 );
222+ }
223+ if (the_msg[i] == ' }' )
224+ {
225+ if (left_bracket_falg)
226+ {
227+ memset (recv_buffer, ' \0 ' , 1024 );
228+ strncpy (recv_buffer, msg + begin_sub, i - begin_sub);
229+
230+ if (i + 1 < the_msg_length)
231+ {
232+ strncpy (recv_buffer_last, msg + i + 1 , the_msg_length - i);
233+ GetInfoFromMsg (recv_buffer);
234+ ProcessMsgCenter (recv_buffer_last);
235+ }
236+ else
237+ {
238+ GetInfoFromMsg (recv_buffer);
239+ fflush (stdout);
240+ }
241+
242+ }
243+ }
244+
245+ }
246+ }
0 commit comments