5 # include <arpa/inet.h>
6 # include <sys/types.h>
7 # include <sys/socket.h>
13 #if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN)
14 void CONV shutdown_all( void )
17 sckt_shutdown( sckt_mnj );
22 sckt_shutdown( sckt_csa );
27 sckt_shutdown( dfpn_sckt );
28 dfpn_sckt = SCKT_NULL;
34 #if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN_CLIENT)||defined(DFPN)
36 str_WSAError( const char *str )
39 snprintf( str_message, SIZE_MESSAGE, "%s:%d", str, WSAGetLastError() );
40 str_message[SIZE_MESSAGE-1] = '\0';
51 client_next_game( tree_t * restrict ptree, const char *str_addr, int iport )
55 const char *str_name1, *str_name2;
56 char buf1[SIZE_PLAYERNAME], buf2[SIZE_PLAYERNAME];
60 str_buffer_cmdline[0] = '\0';
61 sckt_csa = sckt_connect( str_addr, iport );
62 if ( sckt_csa == SCKT_NULL ) { return -2; }
64 str_name1 = str_name2 = NULL;
65 iret = sckt_out( sckt_csa, "LOGIN %s %s\n",
66 client_str_id, client_str_pwd );
67 if ( iret < 0 ) { return iret; }
69 Out( "wait for next game-conditions...\n" );
74 iret = next_cmdline( 1 );
75 if ( iret < 0 ) { return iret; }
76 else if ( game_status & flag_quit ) { return 1; }
78 if ( ! strcmp( str_cmdline, "END Game_Summary" ) ) { break; }
79 else if ( ! strcmp( str_cmdline, "Your_Turn:-" ) ) { my_turn = white; }
80 else if ( ! memcmp( str_cmdline, "Name+:", 6 ) )
82 strncpy( buf1, str_cmdline+6, SIZE_PLAYERNAME-1 );
83 buf1[SIZE_PLAYERNAME-1] = '\0';
86 else if ( ! memcmp( str_cmdline, "Name-:", 6 ) )
88 strncpy( buf2, str_cmdline+6, SIZE_PLAYERNAME-1 );
89 buf2[SIZE_PLAYERNAME-1] = '\0';
94 iret = sckt_out( sckt_csa, "AGREE\n" );
95 if ( iret < 0 ) { return -2; }
97 iret = next_cmdline( 1 );
98 if ( iret < 0 ) { return iret; }
99 else if ( game_status & flag_quit ) { return 1; }
101 if ( ! memcmp( str_cmdline, "REJECT:", 7 ) )
106 else if ( ! memcmp( str_cmdline, "START:", 6 ) ) { break; }
108 str_error = str_server_err;
112 if ( ini_game( ptree, &min_posi_no_handicap, flag_history,
113 str_name1, str_name2 ) < 0 )
118 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
120 client_turn = my_turn;
123 Out( "Game Conditions (%dth):\n", client_ngame );
124 Out( " my turn:%c\n", ach_turn[my_turn] );
126 if ( my_turn == root_turn )
128 iret = com_turn_start( ptree, 0 );
129 if ( iret < 0 ) { return iret; }
137 #if defined(CSA_LAN)||defined(MNJ_LAN)|| defined(DFPN_CLIENT)|| defined(DFPN)
139 sckt_connect( const char *str_addr, int iport )
142 struct sockaddr_in sin;
150 if ( WSAStartup( MAKEWORD(1,1), &wsaData ) )
152 str_error = str_WSAError( "WSAStartup() failed." );
158 ul_addr = inet_addr( str_addr );
159 if ( ul_addr == INADDR_NONE )
161 phe = gethostbyname( str_addr );
164 str_error = str_WSAError( "gethostbyname() faild." );
170 ul_addr = *( (u_long *)phe->h_addr_list[0] );
173 sd = socket( AF_INET, SOCK_STREAM, 0 );
174 if ( sd == SCKT_NULL )
176 str_error = str_WSAError( "socket() faild." );
183 for ( count = 0;; count += 1 ) {
185 sin.sin_family = AF_INET;
186 sin.sin_addr.s_addr = ul_addr;
187 sin.sin_port = htons( (u_short)iport );
188 if ( connect( sd, (struct sockaddr *)&sin, sizeof(sin) ) != SOCKET_ERROR )
193 if ( ! count ) { Out( "connect() failed. try again " ); }
201 if ( count ) { Out( "\n" ); }
208 sckt_shutdown( sckt_t sd )
212 if ( sd == SCKT_NULL ) { return 1; }
213 out_warning( "shut down connection" );
216 if ( shutdown( sd, SD_SEND ) == SOCKET_ERROR )
218 str_error = str_WSAError( "shutdown() faild:" );
224 iret = recv( sd, str_message, SIZE_MESSAGE, 0 );
225 if ( iret == SOCKET_ERROR )
227 str_error = str_WSAError( "recv() failed:" );
231 else if ( ! iret ) { break; }
234 if ( closesocket( sd ) == SOCKET_ERROR )
236 str_error = str_WSAError( "closesocket() failed:" );
241 if ( WSACleanup() == SOCKET_ERROR )
243 str_error = str_WSAError( "WSACleanup() faild:" );
251 if ( shutdown( sd, SHUT_RD ) == -1 )
253 str_error = "shutdown() faild.";
258 iret = (int)recv( sd, str_message, SIZE_MESSAGE, 0 );
261 str_error = "recv() failed.";
264 else if ( ! iret ) { break; }
267 if ( close( sd ) == -1 )
269 str_error = "close() failed.";
278 int CONV sckt_recv_all( sckt_t sd )
285 iret = recv( sd, str_message, SIZE_MESSAGE, 0 );
286 if ( iret == SOCKET_ERROR || ! iret ) { break; }
288 iret = (int)recv( sd, str_message, SIZE_MESSAGE, 0 );
289 if ( iret == -1 || ! iret ) { break; }
298 sckt_check( sckt_t sd )
304 tv.tv_sec = tv.tv_usec = 0;
307 # if defined(_MSC_VER)
308 # pragma warning(disable:4127)
310 FD_SET( sd, &readfds );
311 # if defined(_MSC_VER)
312 # pragma warning(default:4127)
315 iret = select( (int)sd+1, &readfds, NULL, NULL, &tv );
316 if ( iret == SOCKET_ERROR )
318 str_error = str_WSAError( "select() with a socket connected failed:" );
327 sckt_in( sckt_t sd, char *str, int n )
334 Out( "wait for a message ... " );
336 tv.tv_sec = SEC_KEEP_ALIVE;
339 # if defined(_MSC_VER)
340 # pragma warning(disable:4127)
342 FD_SET( sd, &readfds );
343 # if defined(_MSC_VER)
344 # pragma warning(default:4127)
347 iret = select( (int)sd+1, &readfds, NULL, NULL, &tv );
348 if ( iret == SOCKET_ERROR )
350 str_error = str_WSAError( "select() with a socket connected failed." );
358 Out( "time out.\n" );
360 if ( sckt_out( sd, "\n" ) == SOCKET_ERROR )
362 str_error = str_WSAError( "send() failed:" );
367 iret = recv( sd, str, n-1, 0 );
368 if ( iret == SOCKET_ERROR )
370 str_error = str_WSAError( "recv() failed:" );
373 *( str + iret ) = '\0';
377 Out( "Connection closed.\n" );
381 Out( "%s[END]\n", str );
388 sckt_out( sckt_t sd, const char *fmt, ... )
394 va_start( arg, fmt );
395 nch = vsnprintf( buf, 256, fmt, arg );
397 if ( nch >= 256 || nch < 0 )
399 str_error = "buffer overflow at sckt_out()";
403 Out( "- now sending the message: %s", buf );
405 if ( send( sd, buf, nch, 0 ) == SOCKET_ERROR )
407 str_error = str_WSAError( "send() failed:" );
411 return get_elapsed( &time_last_send );
413 #endif /* CSA_LAN || MNJ_LAN || DFPN_CLIENT || DFPN */
416 #if defined(DFPN_CLIENT)
418 dfpn_client_out( const char *fmt, ... )
424 if ( dfpn_client_sckt == SCKT_NULL ) { return 1; }
426 va_start( arg, fmt );
427 nch = vsnprintf( buf, 256, fmt, arg );
430 if ( nch >= 256 || nch < 0 )
432 sckt_shutdown( dfpn_client_sckt );
433 dfpn_client_sckt = SCKT_NULL;
434 out_warning( "A connection to DFPN server is down." );
438 Out( "- send to DFPN server: %s", buf );
440 iret = send( dfpn_client_sckt, buf, nch, 0 );
441 if ( iret == SOCKET_ERROR )
443 sckt_shutdown( dfpn_client_sckt );
444 dfpn_client_sckt = SCKT_NULL;
445 out_warning( "A connection to DFPN server is down." );
451 #endif /* DFPN_CLIENT */