/// \brief Print welcome message and input the size of the board /// \return void void welcome(){ clear(); char mesg[]=":2o48_Network"; getmaxyx(stdscr,row,col); mvprintw(row/2,(col-strlen(mesg))/2,mesg); int ver=c_version(); char verstr[100]; char infostr[100]="Press N to connect to server,S to be a server"; sprintf(verstr,"version %X",ver); mvprintw(row/2+1,(col-strlen(verstr))/2,verstr); mvprintw(row/2+2,(col-strlen(infostr))/2,infostr); mvprintw(row-3,0,"Welcome to :2048 by Librazy\n"); printw("Enter a number<=9 that you want the board be:"); int inpN=4+'0'; while(1){ inpN=getch(); if((inpN==3)||(inpN==KEY_F(1)))c_forceQuit(); if(inpN>'0'&&inpN<='9')break; if(inpN=='n'||inpN=='N'){ mvprintw(row-3,0,"Networking mode now,enter ip you want to connect\n");clrtoeol();move(row-2,0);echo(); char ipstr[130]={0}; while(scanw("%127s",ipstr)!=1){mvprintw(row-3,0,"Networking mode now,enter ip you want to connect!\n");clrtoeol();move(row-2,0);} noecho(); SClient = dyad_newStream(); printw(ipstr); printw(",Enter a number<=9 that you want the board be:"); while(1){ inpN=getch(); if(inpN>'0'&&inpN<='9'){ N=(char)(inpN-'0');printw("%d",N);refresh();break; } } pthread_create (&TInfo, &AThread, t_Info, NULL); pthread_mutex_lock(&MNetC); connecting=true; isnetworking=true; dyad_addListener(SClient, DYAD_EVENT_DATA, n_Data, NULL); dyad_addListener(SClient, DYAD_EVENT_ERROR, g_Error, NULL); dyad_connect(SClient,ipstr, 2048); return; } if(inpN=='s'||inpN=='S'){ move(row-3,0);clrtoeol();move(row-2,0);clrtoeol(); SServ = dyad_newStream(); pthread_create (&TInfo, &AThread, t_Info, NULL); pthread_mutex_lock(&MNetC); connecting=true; isnetworking=true; dyad_addListener(SServ, DYAD_EVENT_ERROR, g_Error, NULL); dyad_addListener(SServ, DYAD_EVENT_ACCEPT, s_Accept, NULL); dyad_listenEx(SServ, "0.0.0.0",2048,1);//Force IPv4 mvprintw(row-3,0,"Serving mode now,listening port 2048\n"); refresh(); return; } } N=(char)(inpN-'0'); printw("%d\nThe board will be %d.Press any key and rock on!",N,N); getch(); pthread_create (&TInfo, &AThread, t_Info, NULL); }
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 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); }
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; }
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); } }
static void s_Accept(dyad_Event *e) { connecting=false; dyad_writef(e->remote,"ACC\n"); dyad_removeAllListeners(SServ, DYAD_EVENT_ACCEPT); dyad_addListener(e->remote, DYAD_EVENT_DATA, s_Init, NULL); }
static void onAccept(dyad_Event *e) { dyad_addListener(e->remote, DYAD_EVENT_DATA, onData, NULL); dyad_writef(e->remote, "echo server\r\n"); }
void addListener(int event, dyadpp_Callback callback, void *udata = NULL) { dyad_addListener(m_wrap, event, (dyad_Callback)callback, udata); }