static void g_Error(dyad_Event *e) { connecting=false; c_warning(e->msg); dyad_end(e->stream); dyad_shutdown(); pthread_cancel(TInfo); pthread_cond_signal (&CInfo); pthread_join(TInfo, NULL); getch(); c_forceQuit(); }
static void n_Init(dyad_Event *e) { if (!memcmp(e->data, "INCOMP", 6)) { c_warning("Incompatiable version!Quit."); dyad_end(e->stream); dyad_shutdown(); pthread_cancel(TInfo); pthread_cond_signal (&CInfo); pthread_join(TInfo, NULL); getch(); c_forceQuit(); }else{ SGaming=e->stream; dyad_removeAllListeners(e->stream, DYAD_EVENT_DATA); dyad_addListener(e->stream, DYAD_EVENT_DATA, g_Data, NULL); dyad_addListener(e->stream, DYAD_EVENT_CLOSE, g_Close, NULL); pthread_mutex_unlock(&MNetC); } }
static void s_Init(dyad_Event *e) { int cliver=0,val=0,cliN; val=sscanf(e->data,"VERSION %X %d",&cliver,&cliN); if(cliver!=c_version()||val!=2){ dyad_writef(e->stream,"INCOMP\n"); c_warning("Incompatiable version!Quit."); dyad_update(); dyad_end(SServ); getch(); c_forceQuit(); }else{ SGaming=e->stream; N=cliN; dyad_writef(e->stream,"OK\n");dyad_update(); dyad_removeAllListeners(e->stream, DYAD_EVENT_DATA); dyad_addListener(e->stream, DYAD_EVENT_DATA, g_Data, NULL); dyad_addListener(e->stream, DYAD_EVENT_CLOSE, g_Close, NULL); pthread_mutex_unlock(&MNetC); } }
/// \brief The thread to print Board to screen /// \param arg Void /// \return void* NULL void* t_NetworkPlay(void* arg){ pthread_mutex_lock(&MNetC); pthread_mutex_lock(&MBoard); curs=0; if(boardseed[curs]==NA){ boardseed[curs]=Rando(RAND_MAX); } srand(boardseed[curs]); Clrboard(curs); clear(); pthread_create (&TNetworkShow, &AThread, t_NetworkShow , NULL); pthread_create (&TNetworkSend, &AThread, t_NetworkSend , NULL); keypad(stdscr, TRUE); int ch=0; int res=NA,lastres=NA;GetRandNums(); pthread_mutex_unlock(&MBoard); usleep(100000); pthread_cond_signal(&CNet); pthread_cond_signal(&CBoard); usleep(100000); pthread_cond_signal(&CNet); pthread_cond_signal (&CBoard); while((ch = getch())&&dyad_getStreamCount()>0){ pthread_testcancel(); move(0,0); clrtoeol(); pthread_mutex_lock(&MInfo); pthread_mutex_lock(&MScr); wclear(MenuWin); pthread_mutex_unlock(&MScr); memset(sinfo,0,sizeof(sinfo)); pthread_mutex_unlock(&MInfo); pthread_cond_signal(&CInfo); lastres=res; pthread_mutex_lock(&MBoard); curs=0; switch(ch) { case KEY_LEFT:case 'H':case 'h': res=Eat(ELEFT);GetRandNums(); break; case KEY_RIGHT:case 'L':case 'l': res=Eat(ERIGHT);GetRandNums(); break; case KEY_UP:case 'K':case 'k': res=Eat(EUP);GetRandNums(); break; case KEY_DOWN:case 'J':case 'j': res=Eat(EDOWN);GetRandNums(); break; case 3:case KEY_F(1): pthread_mutex_unlock(&MBoard); c_tryQuit(); pthread_mutex_lock(&MBoard); break; case ':': pthread_mutex_unlock(&MBoard); x_netCommand(); pthread_mutex_lock(&MBoard); break; } pthread_mutex_unlock(&MBoard); pthread_cond_signal (&CBoard); pthread_cond_signal (&CNet); if(res==lastres&&res==0){ if(score[0]>score[1]) c_info("You Win!"); else if(score[0]<score[1]) c_info("You Lost!"); else c_info("Neck and neck!"); result=0; break; } } getch(); dyad_end(SGaming); dyad_end(SServ); dyad_end(SClient); usleep(100000); pthread_cancel(TNetworkSend); pthread_cancel(TNetworkShow); pthread_cond_signal (&CBoard); pthread_cond_signal (&CNet); pthread_join(TNetworkSend, NULL); pthread_join(TNetworkShow, NULL); pthread_cancel(TInfo); pthread_cond_signal (&CInfo); pthread_join(TInfo, NULL); usleep(100000); wclear(BoardWin); wclear(MenuWin); delwin(BoardWin); delwin(MenuWin); BoardWin=NULL; MenuWin=NULL; pthread_mutex_unlock(&MNetC); pthread_mutex_destroy (&MScr); pthread_mutex_destroy (&MInfo); pthread_cond_destroy (&CInfo); pthread_mutex_destroy (&MBoard); pthread_cond_destroy (&CBoard); pthread_mutex_destroy (&MNet); pthread_mutex_destroy (&MNetC); pthread_cond_destroy (&CNet); c_forceQuit(); return NULL; }
void end() { dyad_end(m_wrap); }