fixed: broken fs0, sram saves
[fceu.git] / drivers / cli / sdl-netplay.c
CommitLineData
63bd5be0 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
4 * Copyright (C) 2001 LULU
5 * Copyright (C) 2002 Ben Parnell
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "sdl.h"
23#include <SDL/SDL_net.h>
24#include "sdl-netplay.h"
25
26char *netplayhost=0;
27
28static int tonowait;
29
30int Port=0xFCE;
31int FDnetplay=0;
32
33
34static SDLNet_SocketSet socketset = NULL;
35static TCPsocket tcpsock = NULL, servsock = NULL;
36
37void cleanup(void)
38{
39 if (tcpsock != NULL) {
40 SDLNet_TCP_DelSocket(socketset, tcpsock);
41 SDLNet_TCP_Close(tcpsock);
42 tcpsock = NULL;
43 }
44 if (servsock != NULL) {
45 SDLNet_TCP_DelSocket(socketset, servsock);
46 SDLNet_TCP_Close(servsock);
47 servsock = NULL;
48 }
49 if (socketset != NULL) {
50 SDLNet_FreeSocketSet(socketset);
51 socketset = NULL;
52 }
53}
54
55int FCEUD_NetworkConnect(void)
56{
57 IPaddress serverIP;
58
59 tonowait=0;
60
61 if (netplay == 2) {
62 /* client */
63 printf("connecting to %s\n", netplayhost);
64
65 SDLNet_ResolveHost(&serverIP, netplayhost, Port);
66 if (serverIP.host == INADDR_NONE) {
67 fprintf(stderr, "Couldn't connected to %s\n", netplayhost);
68 return -1;
69 } else {
70 tcpsock = SDLNet_TCP_Open(&serverIP);
71 if (tcpsock == NULL) {
72 fprintf(stderr, "Couldn't connected to %s\n", netplayhost);
73 return -1;
74 }
75 }
76 printf("connected to %s\n", netplayhost);
77
78 socketset = SDLNet_AllocSocketSet(1);
79 if (socketset == NULL) {
80 fprintf(stderr, "Couldn't create socket set: %s\n",
81 SDLNet_GetError());
82 return -1;
83 }
84 SDLNet_TCP_AddSocket(socketset, tcpsock);
85
86 return 1;
87 } else {
88 /* server */
89
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) {
94 cleanup();
95 fprintf(stderr, "Couldn't create server socket: %s\n",
96 SDLNet_GetError());
97 return -1;
98 }
99
100 socketset = SDLNet_AllocSocketSet(2);
101 if (socketset == NULL) {
102 fprintf(stderr, "Couldn't create socket set: %s\n",
103 SDLNet_GetError());
104 return -1;
105 }
106 SDLNet_TCP_AddSocket(socketset, servsock);
107
108 if (SDLNet_CheckSockets(socketset, ~0)) {
109 tcpsock = SDLNet_TCP_Accept(servsock);
110 if (tcpsock == NULL) {
111 return -1;
112 }
113 SDLNet_TCP_AddSocket(socketset, tcpsock);
114
115 printf("OK, connected\n");
116 return 1;
117 }
118 }
119
120 return -1;
121}
122
123void FCEUD_NetworkClose(void)
124{
125 cleanup();
126}
127
128int FCEUD_NetworkRecvData(uint8 *data, uint32 len, int block)
129{
130 if(block)
131 {
132 if(SDLNet_TCP_Recv(tcpsock, (void *) data, len)!=len)
133 {
134 cleanup();
135 return(0);
136 }
137 switch(SDLNet_CheckSockets(socketset,0))
138 {
139 case -1:return(0);
140 case 0:NoWaiting&=~2;tonowait=0;break;
141 default:if(tonowait>=3)
142 NoWaiting|=2;
143 else tonowait++;
144 break;
145 }
146 return(1);
147 }
148 else
149 {
150 int t=SDLNet_CheckSockets(socketset,0);
151 if(t<0) return(0);
152 if(!t) return(-1);
153 return(SDLNet_TCP_Recv(tcpsock, (void *) data, len)==len);
154 }
155}
156
157/* 0 on failure, 1 on success. This function should always block. */
158int FCEUD_NetworkSendData(uint8 *Value, uint32 len)
159{
160 if (tcpsock)
161 return(SDLNet_TCP_Send(tcpsock, (void *) Value, len)==len);
162 return 0;
163}