1 /* Pcsx - Pc Psx Emulator
2 * Copyright (C) 1999-2003 Pcsx Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses>.
20 int StartServer() { return 0;}
24 int HasClient() { return 0;}
25 int ReadSocket(char * buffer, int len) { return 0;}
26 int RawReadSocket(char * buffer, int len) { return 0;}
27 void WriteSocket(char * buffer, int len) {}
30 void SetsNonblock() {}
38 #include "psxcommon.h"
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <arpa/inet.h>
45 #include <netinet/in.h>
50 static int server_socket = 0;
51 static int client_socket = 0;
53 static char tbuf[513];
56 #define PORT_NUMBER 12345
59 struct in_addr localhostaddr;
60 struct sockaddr_in localsocketaddr;
65 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
69 server_socket = socket(AF_INET, SOCK_STREAM, 0);
72 if (server_socket == INVALID_SOCKET)
75 if (server_socket == -1)
81 memset((void *)&localhostaddr, 0, sizeof(localhostaddr));
82 memset(&localsocketaddr, 0, sizeof(struct sockaddr_in));
85 localhostaddr.S_un.S_addr = htonl(INADDR_ANY);
87 localhostaddr.s_addr = htonl(INADDR_ANY);
89 localsocketaddr.sin_family = AF_INET;
90 localsocketaddr.sin_addr = localhostaddr;
91 localsocketaddr.sin_port = htons(PORT_NUMBER);
93 if (bind(server_socket, (struct sockaddr *) &localsocketaddr, sizeof(localsocketaddr)) < 0)
96 if (listen(server_socket, 1) != 0)
104 shutdown(server_socket, SD_BOTH);
105 closesocket(server_socket);
108 shutdown(server_socket, SHUT_RDWR);
109 close(server_socket);
117 new_socket = accept(server_socket, 0, 0);
120 if (new_socket == INVALID_SOCKET)
123 if (new_socket == -1)
128 client_socket = new_socket;
133 flags = fcntl(client_socket, F_GETFL, 0);
134 fcntl(client_socket, F_SETFL, flags | O_NONBLOCK);
138 sprintf(hello, "000 PCSX Version %s - Debug console\r\n", PCSX_VERSION);
139 WriteSocket(hello, strlen(hello));
146 shutdown(client_socket, SD_BOTH);
147 closesocket(client_socket);
149 shutdown(client_socket, SHUT_RDWR);
150 close(client_socket);
157 return client_socket ? 1 : 0;
160 int ReadSocket(char * buffer, int len) {
167 r = recv(client_socket, tbuf + ptr, 512 - ptr, 0);
175 if (r == SOCKET_ERROR)
187 endl = strstr(tbuf, "\r\n");
191 strncpy(buffer, tbuf, r);
194 memmove(tbuf, tbuf + r, 512 - r);
196 memset(tbuf + r, 0, 512 - r);
208 int RawReadSocket(char * buffer, int len) {
210 int mlen = len < ptr ? len : ptr;
216 memcpy(buffer, tbuf, mlen);
218 memmove(tbuf, tbuf + mlen, 512 - mlen);
222 r = recv(client_socket, buffer + mlen, len - mlen, 0);
230 if (r == SOCKET_ERROR)
245 void WriteSocket(char * buffer, int len) {
249 send(client_socket, buffer, len, 0);
255 ioctlsocket(server_socket, FIONBIO, &b);
257 int flags = fcntl(server_socket, F_GETFL, 0);
258 fcntl(server_socket, F_SETFL, flags & ~O_NONBLOCK);
262 void SetsNonblock() {
265 ioctlsocket(server_socket, FIONBIO, &b);
267 int flags = fcntl(server_socket, F_GETFL, 0);
268 fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);