/// \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); }
static void dyad_acceptPendingConnections(dyad_Stream *stream) { for (;;) { dyad_Stream *remote; dyad_Event e; int err = 0; int sockfd = accept(stream->sockfd, NULL, NULL); if (sockfd == -1) { err = errno; if (err == EWOULDBLOCK) { /* No more waiting sockets */ return; } } /* Create client stream */ remote = dyad_newStream(); remote->state = DYAD_STATE_CONNECTED; /* Set stream's socket */ dyad_setSocket(remote, sockfd); /* Emit accept event */ e = dyad_createEvent(DYAD_EVENT_ACCEPT); e.msg = "accepted connection"; e.remote = remote; dyad_emitEvent(stream, &e); /* Handle invalid socket -- the stream is still made and the ACCEPT event * is still emitted, but its shut immediately with an error */ if (remote->sockfd == -1) { dyad_streamError(remote, "failed to create socket on accept", err); return; } } }
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; }
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; }
dyad_stream() : m_wrap(dyad_newStream()) { m_wrap = dyad_newStream(); }