int FCEUD_RecvData(void *data, uint32 len) { NoWaiting &= ~2; for (;; ) { fd_set funfun; struct timeval popeye; popeye.tv_sec = 0; popeye.tv_usec = 100000; FD_ZERO(&funfun); FD_SET(Socket, &funfun); switch (select(0, &funfun, 0, 0, &popeye)) { case 0: continue; case SOCKET_ERROR: return(0); } if (FD_ISSET(Socket, &funfun)) { if (recv_tcpwrap(data, len) > 0) { unsigned long beefie; if (!ioctlsocket(Socket, FIONREAD, &beefie)) if (beefie) NoWaiting |= 2; return(1); } else return(0); } else return(0); } return 0; }
int FCEUD_NetworkConnect(void) { WSADATA WSAData; SOCKADDR_IN sockin; /* I want to play with fighting robots. */ /* clack clack clack razzzzzzzzzz */ SOCKET TSocket; int netdivisor; if(WSAStartup(MAKEWORD(1,1),&WSAData)) { NetStatAdd("*** Error initializing WIndows Sockets."); return(0); } wsainit=1; if( (TSocket=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) { WSE("Error creating stream socket."); FCEUD_NetworkClose(); return(0); } memset(&sockin,0,sizeof(sockin)); sockin.sin_family=AF_INET; { struct hostent *phostentb; unsigned long hadr; int sockin_len; sockin.sin_port=0; sockin.sin_addr.s_addr=INADDR_ANY; sockin_len=sizeof(sockin); hadr=inet_addr(netplayhost); if(hadr!=INADDR_NONE) sockin.sin_addr.s_addr=hadr; else { NetStatAdd("*** Looking up host name..."); if(!(phostentb=gethostbyname((const char *)netplayhost))) { WSE("Error getting host network information."); closesocket(TSocket); FCEUD_NetworkClose(); return(0); } memcpy((char *)&sockin.sin_addr,((PHOSTENT)phostentb)->h_addr,((PHOSTENT)phostentb)->h_length); } sockin.sin_port=htons(remotetport); NetStatAdd("*** Connecting to remote host..."); if(connect(TSocket,(PSOCKADDR)&sockin,sizeof(sockin))==SOCKET_ERROR) { WSE("Error connecting to remote host."); closesocket(TSocket); FCEUD_NetworkClose(); return(0); } Socket=TSocket; NetStatAdd("*** Sending initialization data to server..."); { uint8 *sendbuf; uint8 buf[1]; uint32 sblen; sblen = 4 + 16 + 16 + 64 + 1 + (netplaynick?strlen(netplaynick):0); sendbuf = (uint8*)malloc(sblen); //mbg merge 7/17/06 added cast memset(sendbuf, 0, sblen); en32(sendbuf, sblen - 4); if(netgamekey) { struct md5_context md5; uint8 md5out[16]; md5_starts(&md5); md5_update(&md5, (uint8*)&GameInfo->MD5.data, 16); md5_update(&md5, (uint8*)netgamekey, strlen(netgamekey)); //mbg merge 7/17/06 added cast md5_finish(&md5, md5out); memcpy(sendbuf + 4, md5out, 16); } else memcpy(sendbuf + 4, &GameInfo->MD5, 16); if(netpassword) { struct md5_context md5; uint8 md5out[16]; md5_starts(&md5); md5_update(&md5, (uint8*)netpassword, strlen(netpassword)); //mbg merge 7/17/06 added cast md5_finish(&md5, md5out); memcpy(sendbuf + 4 + 16, md5out, 16); } memset(sendbuf + 4 + 16 + 16, 0, 64); sendbuf[4 + 16 + 16 + 64] = netlocalplayers; if(netplaynick) memcpy(sendbuf + 4 + 16 + 16 + 64 + 1,netplaynick,strlen(netplaynick)); send(Socket, (char*)sendbuf, sblen, 0); //mbg merge 7/17/06 added cast free(sendbuf); recv_tcpwrap(buf, 1); netdivisor = buf[0]; } } FCEUI_NetplayStart(netlocalplayers,netdivisor); NetStatAdd("*** Connection established."); FCEUDnetplay = 1; char tcpopt = 1; //mbg merge 7/17/06 changed to char if(setsockopt(TSocket, IPPROTO_TCP, TCP_NODELAY, &tcpopt, sizeof(int))) puts("Nodelay fail"); return(1); }