void System6502::CheckPoll() { if (intervalCycles >= cyclesPerInterval) { intervalCycles -= cyclesPerInterval; Throttle(); Polling.fire(EventArgs()); } }
static void AsciiByte(Widget W, char byte) { #ifdef DEBUG printf("AsciiByte(%s)\n", XtName(W)); #endif switch (byte) { case 0x11: /*printf("Xon\n");*/ Throttle(W, False); break; case 0x13: /*printf("Xoff\n");*/ Throttle(W, True); break; case 0x0d: case 0x0a: if (Host_InputSize(W) > 0) { _XltHostCallbackStruct tmp; tmp.reason = XltCR_ASCIIINPUT; tmp.data = XtNewString(Host_InputData(W)); tmp.len = strlen(tmp.data); Host_InputData(W)[0] = '\0'; Host_InputSize(W) = 0; XtCallCallbackList(W, Host_AsciiInputCallback(W), &tmp); XtFree(tmp.data); } break; case '#': if (Host_BinaryInputCallback(W)) { Host_Mode(W) = MODE_BINARY; Host_x(W) = 0; Host_y(W) = 0; Host_InputNeed(W) = Host_y(W) + Host_InputSize(W); } default: Host_InputData(W)[Host_InputSize(W)] = byte; Host_InputSize(W)++; Host_InputData(W)[Host_InputSize(W)] = '\0'; break; } }
void XltHostSendData(Widget w, char *data, int len) { #ifdef DEBUG printf("XltHostSendData(%s,%s)\n", XtName(w), data); #endif if (Host_OutputSize(w) + len + 1 > Host_OutputMaxSize(w)) { Host_OutputMaxSize(w) = Host_OutputSize(w) + len + 1; Host_OutputData(w) = XtRealloc(Host_OutputData(w), Host_OutputMaxSize(w)); } memcpy(&(Host_OutputData(w)[Host_OutputSize(w)]), data, len); Host_OutputSize(w) += len; Host_OutputData(w)[Host_OutputSize(w)] = '\0'; Throttle(w, Host_Throttle(w)); }
static void SendByte(Widget w) { int i; while ((XtAppPending(XtWidgetToApplicationContext(w)) & XtIMAlternateInput) == XtIMAlternateInput) { XtAppProcessEvent(XtWidgetToApplicationContext(w), XtIMAlternateInput); } Host_OutputId(w) = (XtIntervalId)NULL; if (!Host_Throttle(w)) { if (Host_OutputSize(w) > 0) { switch (Host_Type(w)) { case XltHOST_CLIENT: write(Host_Fd(w), &(Host_OutputData(w)[0]), 1); break; case XltHOST_SERVER: for (i = 0; i < Host_NumClients(w); i++) { write(Host_ClientList(w)[i].fd, &(Host_OutputData(w)[0]), 1); } break; default: break; } memmove(&(Host_OutputData(w)[0]), &(Host_OutputData(w)[1]), Host_OutputSize(w)); Host_OutputSize(w)--; if (Host_OutputCallback(w)) { _XltHostCallbackStruct tmp; tmp.reason = XltCR_OUTPUT; tmp.data = NULL; tmp.len = Host_OutputSize(w); tmp.input_size = 0; tmp.input_need = 0; XtCallCallbackList(w, Host_OutputCallback(w), &tmp); } } } Throttle(w, Host_Throttle(w)); }
int main(int argc, char **argv) { char buffer[NET_BUF_LEN]; // for command-line input int servers_found = 0; int server_number = -1; Uint32 timer = 0; //Scan local network to find running server: servers_found = LAN_DetectServers(); if(servers_found < 1) { fprintf(stderr, "No server could be found - exiting.\n"); exit(EXIT_FAILURE); } else if(servers_found == 1) //One server - connect without player intervention { fprintf(stderr, "Single server found - connecting automatically..."); if(!LAN_AutoSetup(0)) //i.e.first (and only) entry in list { fprintf(stderr, "setup_client() failed - exiting.\n"); exit(EXIT_FAILURE); } fprintf(stderr, "connected\n"); } else // More than one server - will have to get player selection: { while(server_number < 0 || server_number >= servers_found) { fprintf(stderr, "The following TuxMath servers were detected:\n"); print_server_list(); fprintf(stderr, "Enter the SERVER NUMBER you would like to connect to:\n"); scanf("%d", &server_number); if(server_number < 0 || server_number >= servers_found) fprintf(stderr, "Illegal value - try again.\n"); } if(!LAN_AutoSetup(server_number)) //i.e.first (and only) entry in list { fprintf(stderr, "setup_client() failed - exiting.\n"); exit(EXIT_FAILURE); } fprintf(stderr, "connected\n"); } /* Now we are connected - get nickname from player: */ { char name[NAME_SIZE]; char* p; fprintf(stderr, "Please enter your name:\n>\n"); fgets(buffer, NAME_SIZE, stdin); p = strchr(buffer, '\n'); //get rid of newline character if(p) *p = '\0'; strncpy(name, buffer, NAME_SIZE); /* If no nickname received, use default: */ if(strlen(name) == 1) strcpy(name, "Anonymous Coward"); snprintf(buffer, NET_BUF_LEN, "%s", name); LAN_SetName(name); } fprintf(stderr, "Welcome to the Tux Math Test Client!\n"); fprintf(stderr, "Type:\n" "'game' to start math game;\n" "'exit' to end client leaving server running;\n" "'quit' to end both client and server\n>\n"); /* Set stdin to be non-blocking: */ fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK); quit = 0; while(!quit) { // See if we have any messages from server: game_check_msgs(); //Get user input from command line and send it to server: /*now display the options*/ if(read_stdin_nonblock(buffer, NET_BUF_LEN)) { //Figure out if we are trying to quit: if( (strncmp(buffer, "exit", 4) == 0) ||(strncmp(buffer, "quit", 4) == 0)) { quit = 1; } else if (strncmp(buffer, "game", 4) == 0) { // Begin the actual math game playgame(); fprintf(stderr, "Math game finished.\n\n"); fprintf(stderr, "Type:\n" "'game' to start math game;\n" "'exit' to end client leaving server running;\n" "'quit' to end both client and server\n>\n"); } else { fprintf(stderr, "Command not recognized. Type:\n" "'game' to start math game;\n" "'exit' to end client leaving server running;\n" "'quit' to end both client and server\n\n>\n"); } } //Limit loop to once per 10 msec so we don't eat all CPU Throttle(10, &timer); } LAN_Cleanup(); return EXIT_SUCCESS; }
int playgame(void) { int ans = 0; MC_FlashCard* fc = NULL; char buf[NET_BUF_LEN]; Uint32 timer = 0; fprintf(stderr, "\nStarting Tux, of the Math Command Line ;-)\n"); fprintf(stderr, "Waiting for other players to be ready...\n\n"); //Tell server we're ready to start: LAN_StartGame(); game_status = GAME_IN_PROGRESS; /* Start out with our "comets" empty: */ { int i; for(i = 0; i < QUEST_QUEUE_SIZE; i ++) erase_flashcard(&comets[i]); } //Begin game loop: while (game_status == GAME_IN_PROGRESS) { //Check our network messages, bailing out for fatal errors: if (game_check_msgs() == -1) return -1; //Now we check for any user responses //This function returns 1 and updates buf with input from //stdin if input is present. //If no input, it returns 0 without blocking or waiting if(read_stdin_nonblock(buf, NET_BUF_LEN)) { //While in game, these just quit the current math game: if ((strncmp(buf, "quit", 4) == 0) ||(strncmp(buf, "exit", 4) == 0) ||(strncmp(buf, "q", 1) == 0)) { game_status = GAME_OVER_ESCAPE; // end = 1; //Exit our loop in playgame() //Tell server we are quitting current game: LAN_LeaveGame(); } else { /*NOTE atoi() will return zero for any string that is not a valid int, not just '0' - should not be a big deal for our test program - DSB */ ans = atoi(buf); fc = check_answer(ans); if((fc != NULL)) { fprintf(stderr, "%s is correct!\nAwait next question...\n>\n", buf); //Tell server we answered it right: //NOTE the '-1' means we aren't tracking times for testclient LAN_AnsweredCorrectly(fc->question_id, -1); erase_flashcard(fc); print_current_quests(); } else //we got input, but not the correct answer: { int i = rand()%QUEST_QUEUE_SIZE; fprintf(stderr, "Sorry, %s is incorrect. Try again!\n", buf); // Can't tell which question was the 'wrong' one, so we // a non-empty one at random. Note that this is just for // purposes of testing LAN_NotAnsweredCorrectly() while(-1 == comets[i].question_id) i = rand()%QUEST_QUEUE_SIZE; LAN_NotAnsweredCorrectly(comets[i].question_id); print_current_quests(); } } //input wasn't any of our keywords } // Input was received Throttle(10, &timer); //so don't eat all CPU } //End of game loop switch(game_status) { case GAME_OVER_ESCAPE: fprintf(stderr, "You quit :(\n"); break; case GAME_OVER_WON: fprintf(stderr, "You won! :-)\n"); } DEBUGMSG(debug_lan, "Leaving playgame()\n"); return 1; }