35868d35 |
1 | /* FCE Ultra - NES/Famicom Emulator |
2 | * |
3 | * Copyright notice for this file: |
4 | * Copyright (C) 2002 Ben Parnell |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | */ |
20 | |
21 | #include <sys/types.h> |
22 | #include <sys/socket.h> |
23 | #include <netinet/in.h> |
24 | #include <arpa/inet.h> |
25 | #include <stdio.h> |
26 | #include <unistd.h> |
27 | #include <netdb.h> |
28 | #include <errno.h> |
29 | |
30 | #ifndef socklen_t |
31 | #define socklen_t int |
32 | #endif |
33 | #ifdef NETWORK |
34 | static int Socket=-1; |
35 | #endif |
36 | #include "main.h" |
37 | #include "unix-netplay.h" |
38 | |
39 | char *netplayhost=0; |
40 | int Port=0xFCE; |
41 | int netplay=0; |
42 | |
43 | int FCEUD_NetworkConnect(void) |
44 | { |
45 | #ifdef NETWORK |
46 | struct sockaddr_in sockn; |
47 | int TSocket; |
48 | |
49 | memset(&sockn,0,sizeof(sockn)); |
50 | sockn.sin_family=AF_INET; |
51 | sockn.sin_port=htons(Port); |
52 | |
53 | if((TSocket=socket(AF_INET, SOCK_STREAM, 0))<0) |
54 | { |
55 | puts("Error creating socket."); |
56 | return(0); |
57 | } |
58 | |
59 | if(netplay==1) /* Be a server. */ |
60 | { |
61 | sockn.sin_addr.s_addr=INADDR_ANY; |
62 | if(bind(TSocket, (struct sockaddr *)&sockn, sizeof(sockn))<0) |
63 | { |
64 | close(TSocket); |
65 | puts("Error binding to socket."); |
66 | return(0); |
67 | } |
68 | if(listen(TSocket, 1)<0) |
69 | { |
70 | puts("Error listening on socket."); |
71 | close(TSocket); |
72 | return(0); |
73 | } |
74 | { |
75 | socklen_t len=sizeof(sockn); |
76 | |
77 | printf("Accepting connection on port %d...\n",Port); |
78 | if((Socket=accept(TSocket,(struct sockaddr *)&sockn,&len))<0 ) |
79 | { |
80 | puts("Error accepting a connection."); |
81 | close(TSocket); |
82 | return(0); |
83 | } |
84 | close(TSocket); |
85 | } |
86 | |
87 | } |
88 | else /* Connect as a client if not a server. */ |
89 | { |
90 | struct hostent *Host; |
91 | |
92 | if((sockn.sin_addr.s_addr=inet_addr(netplayhost))==INADDR_NONE) |
93 | { |
94 | if(!(Host=gethostbyname(netplayhost))) |
95 | { |
96 | puts("Error getting network host entry."); |
97 | return(0); |
98 | } |
99 | memcpy(&sockn.sin_addr,Host->h_addr,Host->h_length); |
100 | } |
101 | printf("Attempting to connect to %s...\n",netplayhost); |
102 | if( connect(TSocket, (struct sockaddr *)&sockn, sizeof(sockn)) <0 ) |
103 | { |
104 | puts("Error connecting to remote host."); |
105 | close(TSocket); |
106 | return(0); |
107 | } |
108 | Socket=TSocket; |
109 | } |
110 | #endif |
111 | return(1); |
112 | } |
113 | |
114 | /* 0 on failure, 1 on success, -1 if it would block and blocking is not |
115 | specified. |
116 | */ |
117 | |
118 | int FCEUD_NetworkRecvData(uint8 *data, uint32 len, int block) |
119 | { |
120 | #ifdef NETWORK |
121 | if(block) |
122 | { |
123 | int t; |
124 | uint8 temp[32]; |
125 | t=recv(Socket,temp,32,MSG_PEEK|MSG_DONTWAIT); |
126 | if(t==-1) |
127 | { |
128 | if(errno!=EAGAIN) return(0); |
129 | } |
130 | else if(t==32) |
131 | NoWaiting|=2; |
132 | else |
133 | NoWaiting&=~2; |
134 | return(recv(Socket,data,len,0)==len); |
135 | } |
136 | else |
137 | { |
138 | int t=recv(Socket,data,len,MSG_DONTWAIT); |
139 | if(t==-1) |
140 | { |
141 | if(errno==EAGAIN) // Would block |
142 | return(-1); |
143 | return(0); |
144 | } |
145 | return(1); |
146 | } |
147 | #else |
148 | return 1; |
149 | #endif |
150 | } |
151 | |
152 | /* 0 on failure, 1 on success. This function should always block. */ |
153 | |
154 | int FCEUD_NetworkSendData(uint8 *Value, uint32 len) |
155 | { |
156 | #ifdef NETWORK |
157 | return(send(Socket,Value,len,0)==len); |
158 | #else |
159 | return 0; |
160 | #endif |
161 | } |
162 | |
163 | void FCEUD_NetworkClose(void) |
164 | { |
165 | #ifdef NETWORK |
166 | if(Socket>0) |
167 | close(Socket); |
168 | Socket=-1; |
169 | #endif |
170 | } |
171 | |