static void n_Data(dyad_Event *e) { connecting=false; int ver=c_version(); dyad_writef(e->stream, "VERSION %X %d\n",ver,N);dyad_update(); dyad_removeAllListeners(e->stream, DYAD_EVENT_CONNECT); dyad_addListener(e->stream, DYAD_EVENT_DATA, n_Init, NULL); }
void x_right(){ pthread_mutex_lock(&MNet); attack[0][attacktimes[0]++]=5; dyad_writef(SGaming, "RIGHT\n"); dyad_update(); pthread_mutex_unlock(&MNet); }
void x_down(){ pthread_mutex_lock(&MNet); attack[0][attacktimes[0]++]=3; dyad_writef(SGaming, "DOWN\n"); dyad_update(); pthread_mutex_unlock(&MNet); }
void x_left(){ pthread_mutex_lock(&MNet); attack[0][attacktimes[0]++]=4; dyad_writef(SGaming, "LEFT\n"); dyad_update(); pthread_mutex_unlock(&MNet); }
void x_boom(int arg,int arg2){ pthread_mutex_lock(&MNet); attack[0][attacktimes[0]++]=6; dyad_writef(SGaming, "BOOM %d %d\n",arg,arg2); dyad_update(); pthread_mutex_unlock(&MNet); }
void x_del(int arg,int arg2){ pthread_mutex_lock(&MNet); attack[0][attacktimes[0]++]=1; dyad_writef(SGaming, "DEL %d %d\n",arg,arg2); dyad_update(); pthread_mutex_unlock(&MNet); }
/// \brief The thread to print Board to screen /// \param arg Void /// \return void* NULL void* t_NetworkSend(void* arg){ pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS , NULL); int oldtype; while(1){ pthread_testcancel(); pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); pthread_mutex_lock(&MNet); pthread_cond_wait (&CNet, &MNet); pthread_testcancel(); pthread_mutex_lock(&MBoard); c_writeBoardToDisk(0,false); FILE *fp; char name[20]; int ver=c_version(); sprintf(name,"2048.0.%X.save",ver); if((fp=fopen(name,"r"))) { int c; int count = 500; while (count--) { if ((c = fgetc(fp)) != EOF) { dyad_write(SGaming, &c, 1); } else { dyad_update(); break; } } }else{ dyad_writef(SGaming, "ERR"); } pthread_mutex_unlock(&MBoard); pthread_mutex_unlock(&MNet); pthread_setcanceltype(oldtype, NULL); } return NULL; }
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); } }
int main(void) { dyad_Stream *s; dyad_init(); s = dyad_newStream(); dyad_addListener(s, DYAD_EVENT_ERROR, onError, NULL); dyad_addListener(s, DYAD_EVENT_ACCEPT, onAccept, NULL); dyad_listen(s, 8000); while (dyad_getStreamCount() > 0) { dyad_update(); } return 0; }
static void* tcpThread(void* data) { UNUSED(data); dyad_init(); dyad_setTickInterval(0.2f); dyad_setUpdateTimeout(0.5f); while (workerRunning) { dyad_update(); } dyad_shutdown(); printf("tcpThread end!!\n"); return NULL; }
int main(void) { dyad_Stream *s; dyad_init(); s = dyad_newStream(); dyad_addListener(s, DYAD_EVENT_CONNECT, onConnect, NULL); dyad_addListener(s, DYAD_EVENT_ERROR, onError, NULL); dyad_addListener(s, DYAD_EVENT_DATA, onData, NULL); dyad_connect(s, "time-nw.nist.gov", 13); while (dyad_getStreamCount() > 0) { dyad_update(); } dyad_shutdown(); return 0; }
/// \brief Main executable /// \return 0 int main() { srand((int)time(0)); initscr(); clear(); curs_set(0); noecho(); raw(); if(has_colors())start_color(); settings(); dyad_init(); pthread_attr_init (&AThread); pthread_attr_setdetachstate (&AThread, PTHREAD_CREATE_JOINABLE); bool cho=true; while(cho){ pthread_mutex_init (&MBoard, NULL); pthread_cond_init (&CBoard, NULL); pthread_mutex_init (&MInfo, NULL); pthread_cond_init (&CInfo, NULL); pthread_mutex_init (&MScr, NULL); pthread_mutex_init (&MNet, NULL); pthread_mutex_init (&MNetC, NULL); pthread_cond_init (&CNet, NULL); Clrboard(curs); welcome(); if(isnetworking){ dyad_setUpdateTimeout(0.05); pthread_create (&TNetworkPlay, &AThread, t_NetworkPlay, NULL); while (dyad_getStreamCount() > 0 && isconnected) { pthread_mutex_lock(&MNet); dyad_update(); pthread_mutex_unlock(&MNet); usleep(10000); } if(result){ 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!"); pthread_cancel(TNetworkPlay); pthread_join(TNetworkPlay, NULL); 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); getch(); } cho=0; }else{ cho=play(); } if(result){ 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); } } if(result){c_forceQuit();} return 0; }
static void update() { dyad_update(); }