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>.
22 #include "psxcommon.h"
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
34 static int server_socket = 0;
35 static int client_socket = 0;
37 static char tbuf[513];
40 #define PORT_NUMBER 12345
43 struct in_addr localhostaddr;
44 struct sockaddr_in localsocketaddr;
49 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
53 server_socket = socket(AF_INET, SOCK_STREAM, 0);
56 if (server_socket == INVALID_SOCKET)
59 if (server_socket == -1)
65 memset((void *)&localhostaddr, 0, sizeof(localhostaddr));
66 memset(&localsocketaddr, 0, sizeof(struct sockaddr_in));
69 localhostaddr.S_un.S_addr = htonl(INADDR_ANY);
71 localhostaddr.s_addr = htonl(INADDR_ANY);
73 localsocketaddr.sin_family = AF_INET;
74 localsocketaddr.sin_addr = localhostaddr;
75 localsocketaddr.sin_port = htons(PORT_NUMBER);
77 if (bind(server_socket, (struct sockaddr *) &localsocketaddr, sizeof(localsocketaddr)) < 0)
80 if (listen(server_socket, 1) != 0)
88 shutdown(server_socket, SD_BOTH);
89 closesocket(server_socket);
92 shutdown(server_socket, SHUT_RDWR);
101 new_socket = accept(server_socket, 0, 0);
104 if (new_socket == INVALID_SOCKET)
107 if (new_socket == -1)
112 client_socket = new_socket;
117 flags = fcntl(client_socket, F_GETFL, 0);
118 fcntl(client_socket, F_SETFL, flags | O_NONBLOCK);
122 sprintf(hello, "000 PCSX Version %s - Debug console\r\n", PACKAGE_VERSION);
123 WriteSocket(hello, strlen(hello));
130 shutdown(client_socket, SD_BOTH);
131 closesocket(client_socket);
133 shutdown(client_socket, SHUT_RDWR);
134 close(client_socket);
141 return client_socket ? 1 : 0;
144 int ReadSocket(char * buffer, int len) {
151 r = recv(client_socket, tbuf + ptr, 512 - ptr, 0);
159 if (r == SOCKET_ERROR)
171 endl = strstr(tbuf, "\r\n");
175 strncpy(buffer, tbuf, r);
178 memmove(tbuf, tbuf + r, 512 - r);
180 memset(tbuf + r, 0, 512 - r);
192 int RawReadSocket(char * buffer, int len) {
194 int mlen = len < ptr ? len : ptr;
200 memcpy(buffer, tbuf, mlen);
202 memmove(tbuf, tbuf + mlen, 512 - mlen);
206 r = recv(client_socket, buffer + mlen, len - mlen, 0);
214 if (r == SOCKET_ERROR)
229 void WriteSocket(char * buffer, int len) {
233 send(client_socket, buffer, len, 0);
239 ioctlsocket(server_socket, FIONBIO, &b);
241 int flags = fcntl(server_socket, F_GETFL, 0);
242 fcntl(server_socket, F_SETFL, flags & ~O_NONBLOCK);
246 void SetsNonblock() {
249 ioctlsocket(server_socket, FIONBIO, &b);
251 int flags = fcntl(server_socket, F_GETFL, 0);
252 fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);