//************************************************************************** //* MODE 3 Lobby Screen * //************************************************************************** static void checkMouseMode3(SDL_Event *event, SDL_Point *currentMouseLocation, SDL_Rect buttonPlacement[], int *select, int *mode, int modeMaxButtons[], bool *match, int *keyboardMode){ for(int i=0; i < modeMaxButtons[3]; i++){ if(mouseOnButton(currentMouseLocation, buttonPlacement, &i)){ // Is the mouse on button 'i' ? if(event->type == SDL_MOUSEBUTTONDOWN){ *keyboardMode = ENTERING_TEXT; if(i == 2){ // Ready //playerReady = !playerReady; SDLNet_TCP_Send(client.TCPSock, "#", strlen("#")); } else if(i == 1){ // Leave isConnected = false; SDLNet_TCP_Send(client.TCPSock, "-", strlen("-")); SDLNet_TCP_Close(client.TCPSock); SDLNet_UDP_Close(client.UDPRecvSock); SDLNet_UDP_Close(client.UDPSendSock); for(int i=0; i < MAX_PLAYERS; i++) playerReady[i] = 0; clearTextStrings(6); printf("LEFT; '-' sent to server, socket closed, ready statuses cleared, textstrings cleared, mode changed\n"); //***************************************** *mode = 5; } // (ELSE: Enter Chat Message Window Pressed) } } } }
void closeServer() { SDLNet_Quit(); SDLNet_TCP_Close(TCPsock); SDLNet_UDP_Close(udpSendSock); SDLNet_UDP_Close(udpRecvSock); SDL_Quit(); IMG_Quit(); }
void UDP_NetListener::threadFunc(UDP_NetListener * sh) { // Create UDP socket. UDPsocket socket = SDLNet_UDP_Open(LISTENER_PORT_NUMBER); if (socket == NULL) { NETMSG("Failed to open UDP socket: %s", SDLNet_GetError()); return; } // Create packet. UDPpacket * const packet = SDLNet_AllocPacket(MAX_PACKET_SIZE); if (packet == NULL) { NETMSG("Failed to create UDP packet: %s", SDLNet_GetError()); SDLNet_UDP_Close(socket); return; } sh->cond.lock(); while (!sh->cond.shouldExit()) { int result = SDLNet_UDP_Recv(socket, packet); if (result == 1) { //unlock to give signalExit() a chance in case we're receiving a large number of packets sh->cond.unlock(); /*if (packet->len > broadcastMsgPrefix.size() && memcmp(packet->data, broadcastMsgPrefix.c_str(), broadcastMsgPrefix.size()) == 0) { IPaddress addr; addr.host = packet->address.host; SDLNet_Write16(HOST_PORT_NUMBER, &addr.port); packet->data[packet->len - 1] = 0; //prevent buffer overflow on invalid data if (strlen(reinterpret_cast<char *>(packet->data)) > broadcastMsgPrefix.size()) { SDL_LockMutex(sp->sessionsMutex); sp->reportSession(addr, reinterpret_cast<char *>(packet->data) + broadcastMsgPrefix.size()); SDL_UnlockMutex(sp->sessionsMutex); } }*/ sh->cond.lock(); } else { if (result != 0) { NETMSG("UDP receive error"); } sh->cond.waitMs(SESSION_LISTENER_PERIOD); } } sh->cond.unlock(); SDLNet_FreePacket(packet); SDLNet_UDP_Close(socket); }
void Client::CleanUp() { delete pScene; pScene = NULL; #ifdef _WIN32 DestroyIcon (icon); icon = NULL; #endif SDL_GL_DeleteContext (mainGLContext); SDL_DestroyWindow (mainWindow); SDL_Quit (); mainGLContext = NULL; mainWindow = NULL; if (udpPackets) { SDLNet_FreePacketV(udpPackets); udpPackets = NULL; fromServer = toServer = NULL; } if (udp_socket) { SDLNet_UDP_Close(udp_socket); udp_socket = NULL; } SDLNet_Quit(); Mix_CloseAudio(); }
void CUDPServer::sendAll(CUDPPacket poPacket) { UDPsocket oSocket; int i, iResult; IPaddress *poIPAddress; oSocket=SDLNet_UDP_Open(0); if(!oSocket) { printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError()); } for ( i = 0 ; i < goClientManager.getNumberClients() ; i++ ) { poIPAddress = goClientManager.getIPAddress(i); iResult = SDLNet_UDP_Bind( oSocket, -1, poIPAddress ); if( iResult == -1 ) { printf("SDLNet_UDP_Bind: %s\n", SDLNet_GetError()); // do something because we failed to bind } poPacket.send(oSocket, MSG_TEXTMESSAGE); SDLNet_UDP_Unbind( oSocket, iResult ); } SDLNet_UDP_Close( oSocket ); }
OSErr NetDDPCloseSocket(short portNumber) { //fdprintf("NetDDPCloseSocket\n"); // ZZZ: shut down receiving thread if(sReceivingThread) { sKeepListening = false; SDL_WaitThread(sReceivingThread, NULL); sReceivingThread = NULL; } if(sSocketSet) { SDLNet_FreeSocketSet(sSocketSet); sSocketSet = NULL; } // (CB's code follows) if (sUDPPacketBuffer) { SDLNet_FreePacket(sUDPPacketBuffer); sUDPPacketBuffer = NULL; SDLNet_UDP_Close(sSocket); sSocket = NULL; } return 0; }
void CUDPServer::listen(Uint32 uiTimeout) { Uint32 uiStop = SDL_GetTicks() + uiTimeout; CUDPPacket oPacket; UDPsocket oSocket; char strUsername[32], strMessage[32]; oPacket.setLastMessageRecievedType(MSG_NOMESSAGE); oSocket = SDLNet_UDP_Open(9998); if ( !oSocket ) { printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError()); } while (SDL_GetTicks() < uiStop) { oPacket.recieve(oSocket); if ( oPacket.getLastMessageRecievedType() != MSG_NOMESSAGE ) { printf("Last message recieved was %d\n", oPacket.getLastMessageRecievedType()); oPacket.getMessageData(strUsername, strMessage); printf("Message was %s, %s\n", strUsername, strMessage); //send message to other clients go_UDPServer.sendAll(oPacket); } } SDLNet_UDP_Close( oSocket ); }
void UDPNetwork::Close() { SDLNet_UDP_Close(m_socket); SDLNet_FreePacket(m_packet); m_socket = nullptr; m_packet = nullptr; }
void UdpSocket::Close() { if(mOpen) { SDLNet_UDP_Close(mSocket); mOpen = false; } }
void NET::Disconnect() { if (remote_socket != NULL) { SDLNet_UDP_Close(remote_socket); remote_socket = NULL; connected = false; } }
//Free the used memory and close the used socket Server::~Server() { SDLNet_FreePacket(input); SDLNet_FreePacket(output); SDLNet_FreePacket(ret); SDLNet_UDP_Close(sock); SDLNet_Quit(); cout << "SERVER CLOSED"; }
bool NET::Connect(string host, int port) { server = false; bool err = false; if (host == "Server") server = true; if (remote_socket != NULL) { SDLNet_UDP_Close(remote_socket); remote_socket = NULL; } if (server) remote_socket = SDLNet_UDP_Open(port); else remote_socket = SDLNet_UDP_Open(0); if (!server) { SDLNet_ResolveHost(&remote_ip, host.c_str(), port); SDLNet_UDP_Unbind(remote_socket, 0); if (SDLNet_UDP_Bind(remote_socket, 0, &remote_ip)==-1) { err = true; if (NET_DEBUG) { cout << "net: couldn't bind UDP socket to host " << host << " port " << port << endl; printf("SDLNet_UDP_Bind: %s\n",SDLNet_GetError()); } } } //try a handshake if (pout == NULL) pout = SDLNet_AllocPacket(MAX_PACKET_SIZE); if (pin == NULL) pin = SDLNet_AllocPacket(MAX_PACKET_SIZE); if (server) { } else { err = ClientHandshake(); } return !err; }
/* Open a UDP network socket If 'port' is non-zero, the UDP socket is bound to a fixed local port. */ extern UDPsocket SDLNet_UDP_Open(u16 port) { UDPsocket sock; /* Allocate a UDP socket structure */ sock = (UDPsocket)malloc(sizeof(*sock)); if ( sock == NULL ) { iprintf("SDL_NET: Out of memory\n"); goto error_return; } memset(sock, 0, sizeof(*sock)); /* Open the socket */ sock->channel = socket(AF_INET, SOCK_DGRAM, 0); if ( sock->channel == INVALID_SOCKET ) { iprintf("SDL_NET: Couldn't create socket\n"); goto error_return; } /* Bind locally, if appropriate */ if ( port ) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = INADDR_ANY; sock_addr.sin_port = SDL_SwapBE16(port); // sock_addr.sin_port = port; /* Bind the socket for listening */ if ( bind(sock->channel, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR ) { iprintf("SDL_NET: Couldn't bind to local port\n"); goto error_return; } /* Fill in the channel host address */ sock->address.host = sock_addr.sin_addr.s_addr; sock->address.port = sock_addr.sin_port; } /* The socket is ready */ return(sock); error_return: SDLNet_UDP_Close(sock); return(NULL); }
Peer::~Peer() { //SDLNet_FreeSocketSet(watch); for(auto i : sockets) { SDLNet_UDP_DelSocket(watch,i); for (int c=0;c<SDLNET_MAX_UDPCHANNELS-1;c++) { SDLNet_UDP_Unbind(i, c); } SDLNet_UDP_Close(i); } //SDLNet_FreeSocketSet(watch); still causes segfault here too, damn }
void UDPNetworkSocket::close() { if (isOpen()) { #ifdef UTIL_HAVE_LIB_SDL2_NET SDLNet_UDP_Close(data->udpsock); data->udpsock = nullptr; #elif defined(__linux__) || defined(__unix__) || defined(ANDROID) if (::close(data->udpSocket) != 0) { int error = errno; WARN(std::string(strerror(error))); } data->udpSocket = 0; #endif } }
void I_QuitNetwork(void) { if (packet) { SDLNet_FreePacket(packet); packet = NULL; } if (udpsocket) { SDLNet_UDP_Close(udpsocket); udpsocket = NULL; } SDLNet_Quit(); }
int main (int argc, char *argv[]) { SDL_Thread *thread = NULL; //setup signal handler if ( signal(SIGINT, sig_handler) == SIG_ERR) { fprintf(stderr, "can't catch SIGINT\n"); return 1; } running = 1; cmd_index = 0; cmd_value = 0; cmd_mode = MODE_CMD; score_entry = 0; if (sdl_init ()) return 1; /* thread = SDL_CreateThread (recv_thread, "RecvThread", (void *) NULL); if (thread == NULL) { fprintf (stderr, "SDL_CreateThread failed: %s\n", SDL_GetError()); return 1; } */ while (running) { SDL_SetRenderDrawColor (ren, 0, 0, 0, 0); SDL_RenderClear(ren); draw_cmd (); SDL_RenderPresent (ren); read_input (); } SDLNet_UDP_Close (socket); SDL_WaitThread (thread, NULL); SDLNet_Quit (); TTF_Quit (); SDL_Quit (); fprintf (stdout, "Exiting\n"); return 0; }
static UDPsocket NET_Socket(void) { UDPsocket temp = NULL; Uint16 portnum = 0; IPaddress tempip = {INADDR_BROADCAST,0}; //Hurdler: I'd like to put a server and a client on the same computer //Logan: Me too //BP: in fact for client we can use any free port we want i have read // in some doc that connect in udp can do it for us... //Alam: where? if (M_CheckParm("-clientport")) { if (!M_IsNextParm()) I_Error("syntax: -clientport <portnum>"); portnum = atoi(M_GetNextParm()); } else portnum = sock_port; temp = SDLNet_UDP_Open(portnum); if (!temp) { CONS_Printf("SDL_Net: %s",SDLNet_GetError()); return NULL; } if (SDLNet_UDP_Bind(temp,BROADCASTADDR-1,&tempip) == -1) { CONS_Printf("SDL_Net: %s",SDLNet_GetError()); SDLNet_UDP_Close(temp); return NULL; } clientaddress[BROADCASTADDR].port = sock_port; clientaddress[BROADCASTADDR].host = INADDR_BROADCAST; doomcom->extratics = 1; // internet is very high ping return temp; }
/* Open a UDP network socket If 'port' is non-zero, the UDP socket is bound to a fixed local port. */ UDPsocket SDLNet_UDP_Open(Uint16 port) { UDPsocket sock; struct sockaddr_in sock_addr; socklen_t sock_len; /* Allocate a UDP socket structure */ sock = (UDPsocket)malloc(sizeof(*sock)); if ( sock == NULL ) { SDLNet_SetError("Out of memory"); goto error_return; } memset(sock, 0, sizeof(*sock)); memset(&sock_addr, 0, sizeof(sock_addr)); /* Open the socket */ sock->channel = socket(AF_INET, SOCK_DGRAM, 0); if ( sock->channel == INVALID_SOCKET ) { SDLNet_SetError("Couldn't create socket"); goto error_return; } /* Bind locally, if appropriate */ sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = INADDR_ANY; sock_addr.sin_port = SDLNet_Read16(&port); /* Bind the socket for listening */ if ( bind(sock->channel, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR ) { SDLNet_SetError("Couldn't bind to local port"); goto error_return; } /* Get the bound address and port */ sock_len = sizeof(sock_addr); if ( getsockname(sock->channel, (struct sockaddr *)&sock_addr, &sock_len) < 0 ) { SDLNet_SetError("Couldn't get socket address"); goto error_return; } /* Fill in the channel host address */ sock->address.host = sock_addr.sin_addr.s_addr; sock->address.port = sock_addr.sin_port; #ifdef SO_BROADCAST /* Allow LAN broadcasts with the socket */ { int yes = 1; setsockopt(sock->channel, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes)); } #endif #ifdef IP_ADD_MEMBERSHIP /* Receive LAN multicast packets on 224.0.0.1 This automatically works on Mac OS X, Linux and BSD, but needs this code on Windows. */ /* A good description of multicast can be found here: http://www.docs.hp.com/en/B2355-90136/ch05s05.html */ /* FIXME: Add support for joining arbitrary groups to the API */ { struct ip_mreq g; g.imr_multiaddr.s_addr = inet_addr("224.0.0.1"); g.imr_interface.s_addr = INADDR_ANY; setsockopt(sock->channel, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&g, sizeof(g)); } #endif /* The socket is ready */ return(sock); error_return: SDLNet_UDP_Close(sock); return(NULL); }
/* Open a UDP network socket If 'port' is non-zero, the UDP socket is bound to a fixed local port. */ extern UDPsocket SDLNet_UDP_Open(Uint16 port) { UDPsocket sock; #ifdef MACOS_OPENTRANSPORT EndpointRef dummy = NULL; #endif /* Allocate a UDP socket structure */ sock = (UDPsocket)malloc(sizeof(*sock)); if ( sock == NULL ) { SDLNet_SetError("Out of memory"); goto error_return; } memset(sock, 0, sizeof(*sock)); /* Open the socket */ #ifdef MACOS_OPENTRANSPORT { sock->error = OTAsyncOpenEndpoint( OTCreateConfiguration(kUDPName),0, &(sock->info), (OTNotifyProcPtr)AsyncUDPNotifier, sock ); AsyncUDPPopEvent( sock ); while( !sock->error && !( sock->completion & CompleteMask(T_OPENCOMPLETE))) { AsyncUDPPopEvent( sock ); } if( sock->error ) { SDLNet_SetError("Could not open UDP socket"); goto error_return; } // Should we ?? // (01/05/03 minami<*****@*****.**> OTSetBlocking( sock->channel ); } #else sock->channel = socket(AF_INET, SOCK_DGRAM, 0); #endif /* MACOS_OPENTRANSPORT */ if ( sock->channel == INVALID_SOCKET ) { SDLNet_SetError("Couldn't create socket"); goto error_return; } #ifdef MACOS_OPENTRANSPORT { InetAddress required, assigned; TBind req_addr, assigned_addr; OSStatus status; InetInterfaceInfo info; memset(&assigned_addr, 0, sizeof(assigned_addr)); assigned_addr.addr.maxlen = sizeof(assigned); assigned_addr.addr.len = sizeof(assigned); assigned_addr.addr.buf = (UInt8 *) &assigned; if ( port ) { status = OTInetGetInterfaceInfo( &info, kDefaultInetInterface ); if( status != kOTNoError ) goto error_return; OTInitInetAddress(&required, port, info.fAddress ); req_addr.addr.maxlen = sizeof( required ); req_addr.addr.len = sizeof( required ); req_addr.addr.buf = (UInt8 *) &required; sock->error = OTBind(sock->channel, &req_addr, &assigned_addr); } else { sock->error = OTBind(sock->channel, nil, &assigned_addr ); } AsyncUDPPopEvent(sock); while( !sock->error && !(sock->completion & CompleteMask(T_BINDCOMPLETE))) { AsyncUDPPopEvent(sock); } if (sock->error != noErr) { SDLNet_SetError("Couldn't bind to local port, OTBind() = %d",(int) status); goto error_return; } sock->address.host = assigned.fHost; sock->address.port = assigned.fPort; #ifdef DEBUG_NET printf("UDP open host = %d, port = %d\n", assigned.fHost, assigned.fPort ); #endif } #else /* Bind locally, if appropriate */ if ( port ) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = INADDR_ANY; sock_addr.sin_port = SDL_SwapBE16(port); /* Bind the socket for listening */ if ( bind(sock->channel, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR ) { SDLNet_SetError("Couldn't bind to local port"); goto error_return; } /* Fill in the channel host address */ sock->address.host = sock_addr.sin_addr.s_addr; sock->address.port = sock_addr.sin_port; } /* Allow LAN broadcasts with the socket */ { int yes = 1; setsockopt(sock->channel, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes)); } #endif /* MACOS_OPENTRANSPORT */ /* The socket is ready */ return(sock); error_return: #ifdef MACOS_OPENTRANSPORT if( dummy ) OTCloseProvider( dummy ); #endif SDLNet_UDP_Close(sock); return(NULL); }
void IPX_StopServer() { SDLNet_UDP_Close(ipxServerSocket); }
void NetInputChannelTerminate(NetInputChannel *n) { SDLNet_UDP_Close(n->sock); n->sock = NULL; }
static void NET_CloseSocket(void) { if (mysocket) SDLNet_UDP_Close(mysocket); mysocket = NULL; }
UDP_Socket::~UDP_Socket() { SDLNet_UDP_Close(sock); }
int network_udp_close(UDPsocket udp) { SDLNet_UDP_Close(udp); udp = NULL; return 0; }
int main(int argc, char **argv) { Uint16 port; char *host,*fname,*fbasename; Sint32 flen,pos,p2; int len,blocks,i,err=0; Uint32 ack; IPaddress ip; UDPsocket sock; UDPpacket *in, *out; FILE *f; /* check our commandline */ if(argc<4) { printf("%s host port file\n",argv[0]); exit(0); } /* initialize SDL */ if(SDL_Init(0)==-1) { printf("SDL_Init: %s\n",SDL_GetError()); exit(1); } /* initialize SDL_net */ if(SDLNet_Init()==-1) { printf("SDLNet_Init: %s\n",SDLNet_GetError()); exit(2); } /* get the host from the commandline */ host=argv[1]; /* get the port from the commandline */ port=(Uint16) strtol(argv[2],NULL,0); if(!port) { printf("a server port cannot be 0.\n"); exit(3); } /* get filename to get from server from commandline */ fname=argv[3]; if(SDLNet_ResolveHost(&ip,host,port)==-1) { printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError()); exit(4); } /* open udp client socket */ if(!(sock=SDLNet_UDP_Open(0))) { printf("SDLNet_UDP_Open: %s\n",SDLNet_GetError()); exit(5); } /* allocate max packet */ if(!(out=SDLNet_AllocPacket(65535))) { printf("SDLNet_AllocPacket: %s\n",SDLNet_GetError()); exit(6); } if(!(in=SDLNet_AllocPacket(65535))) { printf("SDLNet_AllocPacket: %s\n",SDLNet_GetError()); exit(6); } /* bind server address to channel 0 */ if(SDLNet_UDP_Bind(sock, 0, &ip)==-1) { printf("SDLNet_UDP_Bind: %s\n",SDLNet_GetError()); exit(7); } /* open output file */ fbasename=strrchr(fname,'/'); if(!fbasename) fbasename=fname; else fbasename++; printf("writting file: %s\n",fbasename); if(!(f=fopen(fbasename,"wb"))) { perror("fopen"); exit(8); } /* request file / expect filesize */ printf("requesting file=%s\n",fname); out->data[0]=1<<4; strcpy((char*)out->data+1,fname); out->len=strlen(fname)+2; if(udpsend(sock,0,out,in,200,1,TIMEOUT)<1) exit(9); flen=SDLNet_Read32(in->data+1); len=SDLNet_Read32(in->data+5); blocks=(flen+len-1)/len; printf("flen=%d blocksize=%d blocks=%d\n",flen,len,blocks); /* send ready / expect file */ printf("starting transfer\n"); out->data[0]=2<<4; out->len=1; if(udpsend(sock,0,out,in,10,2,TIMEOUT)<1) exit(10); if(flen<0) { printf("file not available...\n"); exit(11); } pos=0; /* count per 32 blocks */ while(pos*32<blocks && !err) { /*printf("pos=%d\n",pos); */ ack=0; if((pos+1)*32>=blocks) { for(i=blocks%32;i<32;i++) ack|=1<<i; } printf("\r " "\r%3d%% %08x: ",(pos*3200)/blocks,pos*32*len); while(ack!=0xffffffff && !err) { i=in->data[1]; p2=SDLNet_Read32(in->data+2); /*printf("received %d,%d\n",i,p2); */ if(!(ack&1<<i) && p2>=pos*32*len) { fseek(f,p2,SEEK_SET); fwrite(in->data+6,in->len-6,1,f); ack|=1<<i; printf("."); fflush(stdout); } if(ack!=0xffffffff) err=udprecv(sock,in,10,2,500); if(err<0) continue; /* error... */ if(!err) { /*printf("sending ack 0x%0X\n",ack); */ out->data[0]=3<<4; SDLNet_Write32(pos*32*len,out->data+1); SDLNet_Write32(ack,out->data+5); out->len=9; SDLNet_UDP_Send(sock,0,out); } err=0; } pos++; } printf("\ndone.\n"); fclose(f); /* close the socket */ SDLNet_UDP_Close(sock); /* free packets */ SDLNet_FreePacket(out); SDLNet_FreePacket(in); /* shutdown SDL_net */ SDLNet_Quit(); /* shutdown SDL */ SDL_Quit(); return(0); }
static __inline__ void snUDPClose(UDPsocket s) { lockSDLNet(); SDLNet_UDP_Close(s); unlockSDLNet(); }
void I_CloseSocket(UDP_SOCKET sock) { SDLNet_UDP_Close(sock); }
udp_socket_t::~udp_socket_t(){ if(likely(!socket)){ SDLNet_UDP_Close(socket); } }
void IPX_StopServer() { TIMER_DelTickHandler(&IPX_ServerLoop); SDLNet_UDP_Close(ipxServerSocket); }