1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2001 LULU
5 * Copyright (C) 2002 Ben Parnell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <SDL/SDL_net.h>
24 #include "sdl-netplay.h"
34 static SDLNet_SocketSet socketset = NULL;
35 static TCPsocket tcpsock = NULL, servsock = NULL;
39 if (tcpsock != NULL) {
40 SDLNet_TCP_DelSocket(socketset, tcpsock);
41 SDLNet_TCP_Close(tcpsock);
44 if (servsock != NULL) {
45 SDLNet_TCP_DelSocket(socketset, servsock);
46 SDLNet_TCP_Close(servsock);
49 if (socketset != NULL) {
50 SDLNet_FreeSocketSet(socketset);
55 int FCEUD_NetworkConnect(void)
63 printf("connecting to %s\n", netplayhost);
65 SDLNet_ResolveHost(&serverIP, netplayhost, Port);
66 if (serverIP.host == INADDR_NONE) {
67 fprintf(stderr, "Couldn't connected to %s\n", netplayhost);
70 tcpsock = SDLNet_TCP_Open(&serverIP);
71 if (tcpsock == NULL) {
72 fprintf(stderr, "Couldn't connected to %s\n", netplayhost);
76 printf("connected to %s\n", netplayhost);
78 socketset = SDLNet_AllocSocketSet(1);
79 if (socketset == NULL) {
80 fprintf(stderr, "Couldn't create socket set: %s\n",
84 SDLNet_TCP_AddSocket(socketset, tcpsock);
90 SDLNet_ResolveHost(&serverIP, NULL, Port);
91 printf("Server IP: %x, %d\n", serverIP.host, serverIP.port);
92 servsock = SDLNet_TCP_Open(&serverIP);
93 if (servsock == NULL) {
95 fprintf(stderr, "Couldn't create server socket: %s\n",
100 socketset = SDLNet_AllocSocketSet(2);
101 if (socketset == NULL) {
102 fprintf(stderr, "Couldn't create socket set: %s\n",
106 SDLNet_TCP_AddSocket(socketset, servsock);
108 if (SDLNet_CheckSockets(socketset, ~0)) {
109 tcpsock = SDLNet_TCP_Accept(servsock);
110 if (tcpsock == NULL) {
113 SDLNet_TCP_AddSocket(socketset, tcpsock);
115 printf("OK, connected\n");
123 void FCEUD_NetworkClose(void)
128 int FCEUD_NetworkRecvData(uint8 *data, uint32 len, int block)
132 if(SDLNet_TCP_Recv(tcpsock, (void *) data, len)!=len)
137 switch(SDLNet_CheckSockets(socketset,0))
140 case 0:NoWaiting&=~2;tonowait=0;break;
141 default:if(tonowait>=3)
150 int t=SDLNet_CheckSockets(socketset,0);
153 return(SDLNet_TCP_Recv(tcpsock, (void *) data, len)==len);
157 /* 0 on failure, 1 on success. This function should always block. */
158 int FCEUD_NetworkSendData(uint8 *Value, uint32 len)
161 return(SDLNet_TCP_Send(tcpsock, (void *) Value, len)==len);