void Runtime::debugStep(TextEditInput *edit, TextEditHelpWidget *help, bool cont) { if (g_debugee != -1) { char buf[OS_PATHNAME_SIZE + 1]; int size; net_print(g_debugee, cont ? "c\n" : "n\n"); pause(PAUSE_DEBUG_STEP); net_print(g_debugee, "l\n"); size = net_input(g_debugee, buf, sizeof(buf), "\n"); if (size > 0) { edit->gotoLine(buf); net_print(g_debugee, "v\n"); help->reload(NULL); do { size = net_input(g_debugee, buf, sizeof(buf), "\1\n"); if (buf[0] == '\1') { break; } if (size > 0) { help->append(buf, size); help->append("\n", 1); } } while (size > 0); } } }
void dumpStack(socket_t socket) { for (int i = prog_stack_count - 1; i > -1; i--) { stknode_t node = prog_stack[i]; switch (node.type) { case kwFUNC: net_print(socket, "FUNC\n"); break; case kwPROC: net_print(socket, "SUB\n"); break; } } }
/* Sends the player the actions he must do. Usefull as callback */ STATIC TEG_STATUS token_loque( int fd, char *unused ) { PSPLAYER pJ; PLAY_DEBUG("token_loque()"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) { goto error; } switch( pJ->estado ) { case PLAYER_STATUS_FICHAS: net_printf( fd, "%s=%d,%d\n", TOKEN_FICHAS, g_game.turno->numjug,g_game.fichas); break; case PLAYER_STATUS_FICHAS2: net_printf( fd, "%s=%d,%d\n", TOKEN_FICHAS2, g_game.turno->numjug,g_game.fichas); break; case PLAYER_STATUS_FICHASC: case PLAYER_STATUS_CANJE: aux_token_fichasc( g_game.turno ); break; case PLAYER_STATUS_ATAQUE: case PLAYER_STATUS_TROPAS: net_printf( fd, "%s=%d\n", TOKEN_TURNO, g_game.turno->numjug ); break; default: break; } return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_LOQUE"\n"); return TEG_STATUS_PARSEERROR; }
/* Puts the player in Game Over state */ TEG_STATUS token_surrender( int fd, char *unused ) { PSPLAYER pJ; PLAY_DEBUG("token_surrender\n"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; if( !pJ->is_player ) goto error; if( pJ->estado < PLAYER_STATUS_HABILITADO ) goto error; con_text_out(M_INF,_("Player %s(%d) abandoned the game\n"),pJ->name,pJ->numjug); netall_printf("%s=%d\n", TOKEN_SURRENDER, pJ->numjug ); player_del_soft( pJ ); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_SURRENDER"\n"); return TEG_STATUS_PARSEERROR; }
/* Parses the armies the player must place the rest of the times */ STATIC TEG_STATUS token_fichasc( int fd, char *str ) { PSPLAYER pJ; int total_armies; PLAY_DEBUG("token_fichasc()\n"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; if( pJ->estado != PLAYER_STATUS_FICHASC && pJ->estado != PLAYER_STATUS_CANJE ) goto error; total_armies = pJ->fichasc_armies + cont_tot(pJ->fichasc_conts); if( pJ->hizo_canje ) total_armies += cuantos_x_canje( pJ->tot_exchanges ); if( aux_token_fichas( fd, str, total_armies, pJ->fichasc_conts) == TEG_STATUS_SUCCESS ) { pJ->estado = PLAYER_STATUS_IDLE; fichasc_next(); pJ->hizo_canje = FALSE; pJ->fichasc_armies = 0; pJ->fichasc_conts = 0; return TEG_STATUS_SUCCESS; } error: net_print(fd,TOKEN_ERROR"="TOKEN_FICHASC"\n"); return TEG_STATUS_PARSEERROR; }
/* Called when a player finish his turn */ STATIC TEG_STATUS token_turn( int fd, char *unused ) { PSPLAYER pJ; PLAY_DEBUG("token_turn()"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) { goto error; } if( pJ->estado < PLAYER_STATUS_TURNOSTART || pJ->estado > PLAYER_STATUS_TURNOEND ) goto error; if( pJ != g_game.turno ) { con_text_out(M_ERR,_("BUG: The server believes that player `%s' does not have the turn"),pJ->name); goto error; } pJ->estado = PLAYER_STATUS_IDLE; if( turno_end( pJ ) != TEG_STATUS_SUCCESS ) goto error; /* give turn to the next player */ if( turno_next() != TEG_STATUS_SUCCESS ) goto error; return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_TURNO"\n"); return TEG_STATUS_PARSEERROR; }
/* a player request to play with a robot. the robot will be launched in the server-side */ STATIC TEG_STATUS token_robot( int fd, char *str ) { PSPLAYER pJ; int newfd; PLAY_DEBUG("token_robot()\n"); if( player_whoisfd(fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; if( ! pJ->is_player ) goto error; if( JUEGO_EMPEZADO ) goto error; if( launch_robot(&newfd, "--connected") != TEG_STATUS_SUCCESS ) goto error; fd_add( newfd ); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_ROBOT"\n"); return TEG_STATUS_PARSEERROR; }
void Runtime::debugStop() { if (g_debugee != -1) { net_print(g_debugee, "q\n"); net_disconnect(g_debugee); g_debugee = -1; appLog("Closed debug session"); } }
/* Ask for a card after the attack */ STATIC TEG_STATUS token_card( int fd, char *str ) { PSPLAYER pJ; PCOUNTRY pP; PLAY_DEBUG("token_card()\n"); /* Veo si puede sacar una tarjeta... */ if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; if( pJ->estado < PLAYER_STATUS_TURNOSTART || pJ->estado >= PLAYER_STATUS_TARJETA) goto error; // player may not have more than TEG_MAX_TARJETAS cards if( pJ->tot_cards >= TEG_MAX_TARJETAS ) goto error; // must have conquered a country to get a card if( pJ->turno_conq < 1 ) goto error; // after the 3rd exchange, the player must have conquered two countries if( pJ->tot_exchanges > 3 && pJ->turno_conq < 2 ) goto error; /* Puede sacar tarjeta */ pJ->tot_cards++; pP = get_random_country( tarjeta_es_libre ); pJ->estado = PLAYER_STATUS_TARJETA; tarjeta_sacar( &pP->tarjeta, pJ->numjug ); /* * Me fijo si el player es due�o del pa�s que dice la tarjeta. Si es as� * le agrego 2 fichas automaticamente como dice el reglamento. * Check if the card that the player gets is for a country that this player * owns. If yes, automatically place two armies according to the rules. */ if( pP->numjug == pJ->numjug ) { pP->ejercitos += 2; pJ->tot_armies += 2; tarjeta_usar( &pP->tarjeta ); if( ! g_game.fog_of_war ) netall_printf( "%s=%d,%d,%d\n", TOKEN_COUNTRY,pP->id,pP->numjug,pP->ejercitos); else fow_netall_printf( pP->id, "%s=%d,%d,%d\n", TOKEN_COUNTRY,pP->id,pP->numjug,pP->ejercitos); } net_printf(fd,"%s=%d,%d\n", TOKEN_TARJETA, pP->id,pP->tarjeta.usada); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_TARJETA"\n"); return TEG_STATUS_ERROR; }
/** * sends a string to socket */ void net_printf(socket_t s, const char *fmt, ...) { char buf[1025]; va_list argp; va_start(argp, fmt); vsnprintf(buf, sizeof(buf), fmt, argp); va_end(argp); net_print(s, buf); }
/* To exchange cards for armies */ STATIC TEG_STATUS token_canje( int fd, char *str ) { PARSER p; DELIM igualador={ ':', ':', ':' }; DELIM separador={ ',', ',', ',' }; PSPLAYER pJ; int t1,t2,t3; int canj_ejer; PLAY_DEBUG("token_canje()\n"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; if( pJ->estado != PLAYER_STATUS_FICHASC ) goto error; p.igualador = &igualador; p.separador = &separador; p.data = str; if( parser_call( &p ) && p.hay_otro ) { t1 = atoi( p.token ); } else goto error; if( parser_call( &p ) && p.hay_otro ) { t2 = atoi( p.token ); } else goto error; if( parser_call( &p ) && !p.hay_otro ) { t3 = atoi( p.token ); } else goto error; /* se puede hacer el canje ? */ if( !tarjeta_puedocanje( pJ->numjug, t1, t2, t3 ) ) goto error; pJ->estado = PLAYER_STATUS_CANJE; pJ->hizo_canje = TRUE; pJ->tot_exchanges++; pJ->tot_cards -= 3; canj_ejer = cuantos_x_canje( pJ->tot_exchanges ); /* quitarle las tarjetas al player */ tarjeta_poner( &g_countries[t1].tarjeta ); tarjeta_poner( &g_countries[t2].tarjeta ); tarjeta_poner( &g_countries[t3].tarjeta ); netall_printf("%s=%d,%d,%d,%d,%d\n", TOKEN_CANJE, pJ->numjug,canj_ejer,t1,t2,t3); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_CANJE"\n"); return TEG_STATUS_PARSEERROR; }
/* To start the game */ TEG_STATUS token_start( int fd ) { char strout[PROT_MAX_LEN + PLAYERNAME_MAX_LEN * TEG_MAX_PLAYERS + 200]; PSPLAYER pJ; PLAY_DEBUG("token_start()\n"); if( JUEGO_EMPEZADO || g_game.players < 2 ) goto error; if( g_server.with_console && fd != CONSOLE_FD) { if( !SPLAYER_HABILITADO_P(fd,&pJ) || !pJ->is_player ) goto error; } JUEGO_EN_EMPEZAR; g_game.playing = g_game.players; con_text_out(M_INF,_("Starting game number: %d with seed: %u\n"),g_game.gamenumber,g_game.seed); player_all_set_status ( PLAYER_STATUS_START ); countries_repartir(); if(turno_init() != TEG_STATUS_SUCCESS ) { con_text_out(M_ERR,_("Error, can't initialize a new turn\n")); goto error; } JUEGO_EN_FICHAS; g_game.turno->estado = PLAYER_STATUS_FICHAS; aux_token_stasta(strout, sizeof(strout) -1 ); netall_printf( "%s=%s;%s=%d,%d;%s=%d,%d,%d,%d;%s=%d,%d\n" ,TOKEN_START ,strout /* available players */ ,TOKEN_NEW_ROUND ,g_game.turno->numjug /* who starts the new turn */ ,g_game.round_number /* the round number */ ,TOKEN_MODALIDAD ,g_game.mission /* play with missions ? */ ,g_game.cmission /* play with common mission */ ,g_game.fog_of_war /* play with fog of war */ ,g_game.reglas /* which rules ? */ ,TOKEN_FICHAS ,g_game.turno->numjug, /* who starts ? */ g_game.fichas ); /* how many armies to place */ return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_START"\n"); return TEG_STATUS_PARSEERROR; }
/* Sets a server option */ STATIC TEG_STATUS token_set( int fd, char *str ) { if( strlen(str)==0 ) goto error; if( option_parse(fd, str) == TEG_STATUS_SUCCESS ) return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_SET"\n"); return TEG_STATUS_PARSEERROR; }
/* Place 2 armies in the card's country. The player must own the country */ STATIC TEG_STATUS token_ejer2( int fd, char *str ) { PARSER p; DELIM igualador={ ':', ':', ':' }; DELIM separador={ ',', ',', ',' }; int country; PSPLAYER j; PLAY_DEBUG("token_ejer2()\n"); if( strlen(str)==0) goto error; if( player_whoisfd( fd, &j ) != TEG_STATUS_SUCCESS ) goto error; if( j->estado != PLAYER_STATUS_TARJETA ) goto error; p.igualador = &igualador; p.separador = &separador; p.data = str; if( parser_call( &p ) && !p.hay_otro ) { country = atoi( p.token ); } else goto error; if( country >= COUNTRIES_CANT || country < 0 ) { goto error; } if( tarjeta_es_usada( &g_countries[ country ].tarjeta )) goto error; if( g_countries[ country ].numjug == j->numjug && g_countries[ country ].tarjeta.numjug == j->numjug ) { g_countries[ country ].ejercitos += 2; j->tot_armies += 2; tarjeta_usar( &g_countries[ country ].tarjeta ); if( ! g_game.fog_of_war ) netall_printf("%s=%d,%d,%d\n", TOKEN_COUNTRY ,country ,g_countries[ country ].numjug ,g_countries[ country ].ejercitos); else fow_netall_printf( country, "%s=%d,%d,%d\n", TOKEN_COUNTRY ,country ,g_countries[ country ].numjug ,g_countries[ country ].ejercitos); } return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_EJER2"\n"); return TEG_STATUS_PARSEERROR; }
/* Sends the player the message he requested. Usefull as callback */ STATIC TEG_STATUS token_echo( int fd, char *msg ) { PSPLAYER pJ; if( player_whoisfd(fd, &pJ )!=TEG_STATUS_SUCCESS || strlen(msg)==0 ) goto error; net_printf(fd,"%s\n",msg); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_ECHO"\n"); return TEG_STATUS_PARSEERROR; }
void Runtime::debugStart(TextEditInput *editWidget, const char *file) { char buf[OS_PATHNAME_SIZE + 1]; bool open; int size; if (g_debugee != -1) { net_print(g_debugee, "l\n"); open = net_input(g_debugee, buf, sizeof(buf), "\n") > 0; } else { open = false; } if (!open) { launchDebug(file); pause(PAUSE_DEBUG_LAUNCH); SDL_RaiseWindow(_window); g_debugee = net_connect("localhost", g_debugPort); if (g_debugee != -1) { net_print(g_debugee, "l\n"); size = net_input(g_debugee, buf, sizeof(buf), "\n"); if (size > 0) { int *marker = editWidget->getMarkers(); for (int i = 0; i < MAX_MARKERS; i++) { if (marker[i] != -1) { net_printf(g_debugee, "b %d\n", marker[i]); } } editWidget->gotoLine(buf); appLog("Debug session ready"); } } else { appLog("Failed to attach to debug window"); } } else { debugStop(); } }
void test_net() { printf("test net\n"); Array* net = new_net(5); ActivateLayer* layer0 = new_activate_layer(Relu_T); ActivateLayer* layer1 = new_conv_layer(2,2,1,1,0,0,1,1); ActivateLayer* layer2 = new_activate_layer(Relu_T); ActivateLayer* layer3 = new_conv_layer(2,2,1,1,0,0,1,1); ActivateLayer* layer4 = new_activate_layer(Relu_T); net_add(net, layer0, del_activate_layer); net_add(net, layer1, del_activate_layer); net_add(net, layer2, del_activate_layer); net_add(net, layer3, del_activate_layer); net_add(net, layer4, del_activate_layer); net_print(net); del_net(net); }
/* Parses the armies the player must place for the 2nd time */ STATIC TEG_STATUS token_fichas2( int fd, char *str ) { PLAY_DEBUG("token_fichas2()\n"); if( !SPLAYER_FICHAS2(fd) ) goto error; if( aux_token_fichas( fd, str, g_game.fichas2, 0) == TEG_STATUS_SUCCESS ) { g_game.turno->estado = PLAYER_STATUS_POSTFICHAS2; fichas2_next(); return TEG_STATUS_SUCCESS; } error: net_print(fd,TOKEN_ERROR"="TOKEN_FICHAS2"\n"); return TEG_STATUS_PARSEERROR; }
/* Protocol version */ STATIC TEG_STATUS token_pversion( int fd, char *str ) { PSPLAYER pJ; PARSER p; DELIM igualador={ ':', ':', ':' }; DELIM separador={ ',', ',', ',' }; int hi,lo; PLAY_DEBUG("token_pversion()\n"); if( strlen(str)==0 ) goto error; p.igualador = &igualador; p.separador = &separador; p.data = str; if( parser_call( &p ) && p.hay_otro ) { hi = atoi( p.token ); } else goto error; if( parser_call( &p ) && !p.hay_otro ) { lo = atoi( p.token ); } else goto error; net_printf(fd,"%s=%i,%i\n", TOKEN_PVERSION, PROTOCOL_HIVER,PROTOCOL_LOVER); if( hi != PROTOCOL_HIVER ) { con_text_out(M_ERR,_("Client with incompatible protocol version (server:%d , client:%d)\n"),PROTOCOL_HIVER,hi); if( player_whoisfd( fd, &pJ ) == TEG_STATUS_SUCCESS ) player_del_hard( pJ ); else fd_remove(fd); return TEG_STATUS_CONNCLOSED; } return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_PVERSION"\n"); return TEG_STATUS_PARSEERROR; }
/* Sends all the player a message. An observer may use this function */ STATIC TEG_STATUS token_message( int fd, char *msg ) { PSPLAYER j; PLAY_DEBUG("token_message()\n"); if( player_whoisfd(fd, &j )!=TEG_STATUS_SUCCESS || strlen(msg)==0 ) goto error; strip_invalid_msg(msg); if( j->is_player ) netall_printf("%s=%s,%d,\"%s\"\n", TOKEN_MESSAGE, j->name,j->numjug,msg); else netall_printf("%s=observer-%s,%d,\"%s\"\n", TOKEN_MESSAGE, j->name,j->numjug,msg); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_MESSAGE"\n"); return TEG_STATUS_PARSEERROR; }
/* send the hi-scores to the player */ STATIC TEG_STATUS token_scores( int fd, char *unused ) { char strout[PROT_MAX_LEN + PLAYERNAME_MAX_LEN * TEG_MAX_PLAYERS + 200]; PLAY_DEBUG("token_scores()\n"); if( !SPLAYER_CONNECTED( fd )) goto error; if( scores_dump(strout,sizeof(strout) -1 ) != TEG_STATUS_SUCCESS ) goto error; net_printf(fd,"%s=%s\n", TOKEN_SCORES, strout); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_SCORES"\n"); return TEG_STATUS_PARSEERROR; }
/* The player is asking a mission */ STATIC TEG_STATUS token_mission( int fd, char *unused ) { PSPLAYER pJ; PLAY_DEBUG("token_mission()"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) { goto error; } if( pJ->estado < PLAYER_STATUS_START ) { goto error; } if( mission_asignar( pJ ) != TEG_STATUS_SUCCESS ) goto error; net_printf( fd, "%s=%d\n", TOKEN_MISSION, pJ->mission); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_MISSION"\n"); return TEG_STATUS_PARSEERROR; }
/* request who started the round, and which round number */ STATIC TEG_STATUS token_new_round( int fd, char *str ) { PSPLAYER pJ; PLAY_DEBUG("token_new_round()\n"); if( !JUEGO_EMPEZADO ) goto error; if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; net_printf(fd,"%s=%d,%d\n", TOKEN_NEW_ROUND ,g_game.turno->numjug /* who starts the new turn */ ,g_game.round_number /* the round number */ ); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_NEW_ROUND"\n"); return TEG_STATUS_ERROR; }
/* request which cards do the players has */ STATIC TEG_STATUS token_enum_cards( int fd, char *str ) { PSPLAYER pJ; int i,first; char buffer[1024]; PLAY_DEBUG("token_enum_cards()\n"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) goto error; memset( buffer, 0, sizeof(buffer) ); first=1; for(i=0;i<COUNTRIES_CANT;i++) { if( g_countries[i].tarjeta.numjug == pJ->numjug ) { char buffy[512]; memset(buffy,0,sizeof(buffy)); if( first ) { snprintf( buffy, sizeof(buffy)-1, "%d:%d", i, g_countries[i].tarjeta.usada ); strncat( buffer, buffy, sizeof(buffer)-1 ); first = 0; } else { snprintf( buffy, sizeof(buffy)-1, ",%d:%d", i, g_countries[i].tarjeta.usada ); strncat( buffer, buffy, sizeof(buffer)-1 ); } } } net_printf(fd,"%s=%s\n", TOKEN_ENUM_CARDS, buffer); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_ENUM_CARDS"\n"); return TEG_STATUS_ERROR; }
/* Assigns the player a color */ STATIC TEG_STATUS token_color( int fd, char *str ) { int color; int a; PSPLAYER pJ; PLAY_DEBUG("token_color()"); if( player_whoisfd( fd, &pJ ) != TEG_STATUS_SUCCESS ) { goto error; } if(strlen(str)==0) goto error; if( pJ->estado != PLAYER_STATUS_CONNECTED ) goto error; if( pJ->is_player == FALSE ) goto error; a = atoi( str ); if (a < 0 || a >= TEG_MAX_PLAYERS ) goto error; color = a; if ( color_libre( &color ) == FALSE ) goto error; pJ->estado = PLAYER_STATUS_HABILITADO; pJ->color = color; con_text_out(M_INF,_("Player %s(%d) has color %s\n"),pJ->name,pJ->numjug,_(g_colores[color])); netall_printf( "%s=%s,%d,%d\n", TOKEN_NEWPLAYER, pJ->name, pJ->numjug, pJ->color ); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_COLOR"\n"); return TEG_STATUS_PARSEERROR; }
/* sends a player the type of game it is being played */ TEG_STATUS token_typeofgame( int fd, char *str ) { PSPLAYER pJ; PLAY_DEBUG("token_typeofgame()\n"); if( JUEGO_EMPEZADO ) goto error; if( !SPLAYER_HABILITADO_P(fd,&pJ) || !pJ->is_player ) goto error; netall_printf( "%s=%d,%d,%d,%d;\n" ,TOKEN_MODALIDAD ,g_game.mission /* play with missions ? */ ,g_game.cmission /* play with common mission */ ,g_game.fog_of_war /* play with fog of war */ ,g_game.reglas /* which rules ? */ ); return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_MODALIDAD"\n"); return TEG_STATUS_PARSEERROR; }
// open a web server connection int http_open(dev_file_t *f) { char host[250]; char txbuf[1024]; f->port = 0; // check for http:// if (0 != strncasecmp(f->name, "http://", 7)) { rt_raise("HTTP: INVALID URL"); return 0; } // check for end of host delimeter char *colon = strchr(f->name + 7, ':'); char *slash = strchr(f->name + 7, '/'); char *lastSlash; // saves the length of the path component in f->drv_dw[1] if (colon) { // http://host:port/resource or http://host:port if (slash) { *slash = 0; f->port = xstrtol(colon + 1); *slash = '/'; lastSlash = strrchr(slash, '/'); f->drv_dw[1] = lastSlash ? lastSlash - f->name : slash - f->name; } else { f->port = xstrtol(colon + 1); f->drv_dw[1] = strlen(f->name); } *colon = 0; strcpy(host, f->name + 7); *colon = ':'; } else if (slash) { // http://host/resource or http://host/ *slash = 0; strcpy(host, f->name + 7); *slash = '/'; lastSlash = strrchr(slash, '/'); f->drv_dw[1] = lastSlash ? lastSlash - f->name : slash - f->name; } else { // http://host strcpy(host, f->name + 7); f->drv_dw[1] = strlen(f->name); } f->drv_dw[0] = 1; if (f->port == 0) { f->port = 80; } socket_t s = net_connect(host, f->port); f->handle = (socket_t) s; if (f->handle <= 0) { f->handle = -1; f->drv_dw[0] = 0; f->port = 0; return 0; } sprintf(txbuf, "GET %s HTTP/1.0\r\n" "Host: %s\r\n" "Accept: */*\r\n" "Accept-Language: en-au\r\n" "User-Agent: SmallBASIC\r\n", slash ? slash : "/", host); if (f->drv_dw[2]) { // If-Modified-Since: Sun, 03 Apr 2005 04:45:47 GMT strcat(txbuf, "If-Modified-Since: "); strftime(txbuf + strlen(txbuf), 60, "%a, %d %b %Y %H:%M:%S %Z\r\n", localtime((time_t *) &f->drv_dw[2])); } strcat(txbuf, "\r\n"); net_print(s, txbuf); return 1; }
/* * write to a socket */ int sockcl_write(dev_file_t *f, byte *data, dword size) { net_print((socket_t) (long) f->handle, (char *)data); return size; }
int main (int argc, char **argv) { network_t *net; int no_of_pairs; int no_of_inputs; int no_of_outputs; float input[MAX_SIZE]; float target[MAX_SIZE]; float output[MAX_SIZE]; float error, total_error; int t; int i; srand (time (0)); parse_options (argc, argv); read_specification (spec_filename, &no_of_inputs, &no_of_outputs, &no_of_pairs, input, target); if (strlen (input_filename) == 0) { if (hidden_nodes == 0) hidden_nodes = no_of_inputs; net = net_allocate (3, no_of_inputs, hidden_nodes, no_of_outputs); } else { net = net_load (input_filename); } if (!use_bias) { net_use_bias(net, 0); } /* See spec.c for the way the input/target pairs are stored * in the input[] and target[] arrays. */ #define inputs(i) (input + i * no_of_inputs) #define targets(i) (target + i* no_of_outputs) t = 0; total_error = 0; while ((t < max_trainings) && ((total_error >= max_error) || (t <= 10))) { /* choose one of the input/target pairs: inputs(i), targets(i) */ i = rand () % no_of_pairs; /* compute the outputs for inputs(i) */ net_compute (net, inputs (i), output); /* find the error with respect to targets(i) */ error = net_compute_output_error (net, targets (i)); /* train the network one step */ net_train (net); /* keep track of (moving) average of the error */ if (t == 0) { total_error = error; } else { total_error = 0.9 * total_error + 0.1 * error; } /* next */ t++; } if (strlen (output_filename) == 0) { net_print (net); } else { net_save (output_filename, net); } printf ("Number of training performed: %i (max %i)\n", t, max_trainings); printf ("Average output error: %f (max %f)\n", total_error, max_error); return 0; }
/* Place armies in the conquered country */ STATIC TEG_STATUS token_tropas( int fd, char *str ) { PARSER p; DELIM igualador={ ':', ':', ':' }; DELIM separador={ ',', ',', ',' }; int src,dst,cant; PSPLAYER pJ; PLAY_DEBUG("token_tropas()\n"); if( !SPLAYER_TROPAS_P(fd,&pJ) || strlen(str)==0 ) goto error; p.igualador = &igualador; p.separador = &separador; p.data = str; if( parser_call( &p ) && p.hay_otro ) { src = atoi( p.token ); } else goto error; if( parser_call( &p ) && p.hay_otro ) { dst = atoi( p.token ); } else goto error; if( parser_call( &p ) && !p.hay_otro ) { cant = atoi( p.token ); } else goto error; if( src >= COUNTRIES_CANT || src < 0 || dst >= COUNTRIES_CANT || dst < 0) { goto error; } if( src != pJ->country_src || dst != pJ->country_dst ) goto error; if( cant > g_countries[src].ejercitos-1 || cant > 3) goto error; if( cant > 0 ) { g_countries[src].ejercitos -= cant; g_countries[dst].ejercitos += cant; pJ->estado=PLAYER_STATUS_ATAQUE; pJ->country_src = pJ->country_dst = -1; if( ! g_game.fog_of_war ) netall_printf( "%s=%d,%d,%d;%s=%d,%d,%d\n", TOKEN_COUNTRY, src,g_countries[src].numjug,g_countries[src].ejercitos, TOKEN_COUNTRY, dst,g_countries[dst].numjug,g_countries[dst].ejercitos ); else { fow_netall_printf( src, "%s=%d,%d,%d\n", TOKEN_COUNTRY, src,g_countries[src].numjug,g_countries[src].ejercitos ); fow_netall_printf( dst, "%s=%d,%d,%d\n", TOKEN_COUNTRY, dst,g_countries[dst].numjug,g_countries[dst].ejercitos ); } } return TEG_STATUS_SUCCESS; error: net_print(fd,TOKEN_ERROR"="TOKEN_TROPAS"\n"); return TEG_STATUS_PARSEERROR; }