diff options
| author | SND\edgbla_cp <SND\edgbla_cp@e17a0e51-4ae3-4d35-97c3-1a29b211df97> | 2012-11-18 18:15:59 +0000 |
|---|---|---|
| committer | SND\edgbla_cp <SND\edgbla_cp@e17a0e51-4ae3-4d35-97c3-1a29b211df97> | 2012-11-18 18:15:59 +0000 |
| commit | 065ccce6e919353d1e746057521e8e317b9acf12 (patch) | |
| tree | d08f773fe1d980faf8ca88c445da5b1b1c919874 /libpcsxcore/socket.c | |
| parent | adc29a23861e66c104fb411e67e04019c8888ec9 (diff) | |
| download | pcsxr-065ccce6e919353d1e746057521e8e317b9acf12.tar.gz | |
Unix style EOL (thanks to darktjm);
git-svn-id: https://pcsxr.svn.codeplex.com/svn/pcsxr@81299 e17a0e51-4ae3-4d35-97c3-1a29b211df97
Diffstat (limited to 'libpcsxcore/socket.c')
| -rwxr-xr-x[-rw-r--r--] | libpcsxcore/socket.c | 508 |
1 files changed, 254 insertions, 254 deletions
diff --git a/libpcsxcore/socket.c b/libpcsxcore/socket.c index 450aefd9..f0b19853 100644..100755 --- a/libpcsxcore/socket.c +++ b/libpcsxcore/socket.c @@ -1,254 +1,254 @@ -/* Pcsx - Pc Psx Emulator
- * Copyright (C) 1999-2003 Pcsx Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses>.
- */
-
-#ifdef _WIN32
-#include <winsock2.h>
-#endif
-
-#include "psxcommon.h"
-#include "socket.h"
-
-#ifndef _WIN32
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <unistd.h>
-#include <fcntl.h>
-#endif
-
-static int server_socket = 0;
-static int client_socket = 0;
-
-static char tbuf[513];
-static int ptr = 0;
-
-#define PORT_NUMBER 12345
-
-int StartServer() {
- struct in_addr localhostaddr;
- struct sockaddr_in localsocketaddr;
-
-#ifdef _WIN32
- WSADATA wsaData;
-
- if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
- return -1;
-#endif
-
- server_socket = socket(AF_INET, SOCK_STREAM, 0);
-
-#ifdef _WIN32
- if (server_socket == INVALID_SOCKET)
- return -1;
-#else
- if (server_socket == -1)
- return -1;
-#endif
-
- SetsNonblock();
-
- memset((void *)&localhostaddr, 0, sizeof(localhostaddr));
- memset(&localsocketaddr, 0, sizeof(struct sockaddr_in));
-
-#ifdef _WIN32
- localhostaddr.S_un.S_addr = htonl(INADDR_ANY);
-#else
- localhostaddr.s_addr = htonl(INADDR_ANY);
-#endif
- localsocketaddr.sin_family = AF_INET;
- localsocketaddr.sin_addr = localhostaddr;
- localsocketaddr.sin_port = htons(PORT_NUMBER);
-
- if (bind(server_socket, (struct sockaddr *) &localsocketaddr, sizeof(localsocketaddr)) < 0)
- return -1;
-
- if (listen(server_socket, 1) != 0)
- return -1;
-
- return 0;
-}
-
-void StopServer() {
-#ifdef _WIN32
- shutdown(server_socket, SD_BOTH);
- closesocket(server_socket);
- WSACleanup();
-#else
- shutdown(server_socket, SHUT_RDWR);
- close(server_socket);
-#endif
-}
-
-void GetClient() {
- int new_socket;
- char hello[256];
-
- new_socket = accept(server_socket, 0, 0);
-
-#ifdef _WIN32
- if (new_socket == INVALID_SOCKET)
- return;
-#else
- if (new_socket == -1)
- return;
-#endif
- if (client_socket)
- CloseClient();
- client_socket = new_socket;
-
-#ifndef _WIN32
- {
- int flags;
- flags = fcntl(client_socket, F_GETFL, 0);
- fcntl(client_socket, F_SETFL, flags | O_NONBLOCK);
- }
-#endif
-
- sprintf(hello, "000 PCSXR Version %s - Debug console\r\n", PACKAGE_VERSION);
- WriteSocket(hello, strlen(hello));
- ptr = 0;
-}
-
-void CloseClient() {
- if (client_socket) {
-#ifdef _WIN32
- shutdown(client_socket, SD_BOTH);
- closesocket(client_socket);
-#else
- shutdown(client_socket, SHUT_RDWR);
- close(client_socket);
-#endif
- client_socket = 0;
- }
-}
-
-int HasClient() {
- return client_socket ? 1 : 0;
-}
-
-int ReadSocket(char * buffer, int len) {
- int r;
- char * endl;
-
- if (!client_socket)
- return -1;
-
- r = recv(client_socket, tbuf + ptr, 512 - ptr, 0);
-
- if (r == 0) {
- client_socket = 0;
- if (!ptr)
- return 0;
- }
-#ifdef _WIN32
- if (r == SOCKET_ERROR)
-#else
- if (r == -1)
-#endif
- {
- if (ptr == 0)
- return -1;
- r = 0;
- }
- ptr += r;
- tbuf[ptr] = 0;
-
- endl = strstr(tbuf, "\r\n");
-
- if (endl) {
- r = endl - tbuf;
- strncpy(buffer, tbuf, r);
-
- r += 2;
- memmove(tbuf, tbuf + r, 512 - r);
- ptr -= r;
- memset(tbuf + r, 0, 512 - r);
- r -= 2;
-
- } else {
- r = 0;
- }
-
- buffer[r] = 0;
-
- return r;
-}
-
-int RawReadSocket(char * buffer, int len) {
- int r = 0;
- int mlen = len < ptr ? len : ptr;
-
- if (!client_socket)
- return -1;
-
- if (ptr) {
- memcpy(buffer, tbuf, mlen);
- ptr -= mlen;
- memmove(tbuf, tbuf + mlen, 512 - mlen);
- }
-
- if (len - mlen)
- r = recv(client_socket, buffer + mlen, len - mlen, 0);
-
- if (r == 0) {
- client_socket = 0;
- if (!ptr)
- return 0;
- }
-#ifdef _WIN32
- if (r == SOCKET_ERROR)
-#else
- if (r == -1)
-#endif
- {
- if (ptr == 0)
- return -1;
- r = 0;
- }
-
- r += mlen;
-
- return r;
-}
-
-void WriteSocket(char * buffer, int len) {
- if (!client_socket)
- return;
-
- send(client_socket, buffer, len, 0);
-}
-
-void SetsBlock() {
-#ifdef _WIN32
- u_long b = 0;
- ioctlsocket(server_socket, FIONBIO, &b);
-#else
- int flags = fcntl(server_socket, F_GETFL, 0);
- fcntl(server_socket, F_SETFL, flags & ~O_NONBLOCK);
-#endif
-}
-
-void SetsNonblock() {
-#ifdef _WIN32
- u_long b = 1;
- ioctlsocket(server_socket, FIONBIO, &b);
-#else
- int flags = fcntl(server_socket, F_GETFL, 0);
- fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);
-#endif
-}
+/* Pcsx - Pc Psx Emulator + * Copyright (C) 1999-2003 Pcsx Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses>. + */ + +#ifdef _WIN32 +#include <winsock2.h> +#endif + +#include "psxcommon.h" +#include "socket.h" + +#ifndef _WIN32 +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <unistd.h> +#include <fcntl.h> +#endif + +static int server_socket = 0; +static int client_socket = 0; + +static char tbuf[513]; +static int ptr = 0; + +#define PORT_NUMBER 12345 + +int StartServer() { + struct in_addr localhostaddr; + struct sockaddr_in localsocketaddr; + +#ifdef _WIN32 + WSADATA wsaData; + + if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) + return -1; +#endif + + server_socket = socket(AF_INET, SOCK_STREAM, 0); + +#ifdef _WIN32 + if (server_socket == INVALID_SOCKET) + return -1; +#else + if (server_socket == -1) + return -1; +#endif + + SetsNonblock(); + + memset((void *)&localhostaddr, 0, sizeof(localhostaddr)); + memset(&localsocketaddr, 0, sizeof(struct sockaddr_in)); + +#ifdef _WIN32 + localhostaddr.S_un.S_addr = htonl(INADDR_ANY); +#else + localhostaddr.s_addr = htonl(INADDR_ANY); +#endif + localsocketaddr.sin_family = AF_INET; + localsocketaddr.sin_addr = localhostaddr; + localsocketaddr.sin_port = htons(PORT_NUMBER); + + if (bind(server_socket, (struct sockaddr *) &localsocketaddr, sizeof(localsocketaddr)) < 0) + return -1; + + if (listen(server_socket, 1) != 0) + return -1; + + return 0; +} + +void StopServer() { +#ifdef _WIN32 + shutdown(server_socket, SD_BOTH); + closesocket(server_socket); + WSACleanup(); +#else + shutdown(server_socket, SHUT_RDWR); + close(server_socket); +#endif +} + +void GetClient() { + int new_socket; + char hello[256]; + + new_socket = accept(server_socket, 0, 0); + +#ifdef _WIN32 + if (new_socket == INVALID_SOCKET) + return; +#else + if (new_socket == -1) + return; +#endif + if (client_socket) + CloseClient(); + client_socket = new_socket; + +#ifndef _WIN32 + { + int flags; + flags = fcntl(client_socket, F_GETFL, 0); + fcntl(client_socket, F_SETFL, flags | O_NONBLOCK); + } +#endif + + sprintf(hello, "000 PCSXR Version %s - Debug console\r\n", PACKAGE_VERSION); + WriteSocket(hello, strlen(hello)); + ptr = 0; +} + +void CloseClient() { + if (client_socket) { +#ifdef _WIN32 + shutdown(client_socket, SD_BOTH); + closesocket(client_socket); +#else + shutdown(client_socket, SHUT_RDWR); + close(client_socket); +#endif + client_socket = 0; + } +} + +int HasClient() { + return client_socket ? 1 : 0; +} + +int ReadSocket(char * buffer, int len) { + int r; + char * endl; + + if (!client_socket) + return -1; + + r = recv(client_socket, tbuf + ptr, 512 - ptr, 0); + + if (r == 0) { + client_socket = 0; + if (!ptr) + return 0; + } +#ifdef _WIN32 + if (r == SOCKET_ERROR) +#else + if (r == -1) +#endif + { + if (ptr == 0) + return -1; + r = 0; + } + ptr += r; + tbuf[ptr] = 0; + + endl = strstr(tbuf, "\r\n"); + + if (endl) { + r = endl - tbuf; + strncpy(buffer, tbuf, r); + + r += 2; + memmove(tbuf, tbuf + r, 512 - r); + ptr -= r; + memset(tbuf + r, 0, 512 - r); + r -= 2; + + } else { + r = 0; + } + + buffer[r] = 0; + + return r; +} + +int RawReadSocket(char * buffer, int len) { + int r = 0; + int mlen = len < ptr ? len : ptr; + + if (!client_socket) + return -1; + + if (ptr) { + memcpy(buffer, tbuf, mlen); + ptr -= mlen; + memmove(tbuf, tbuf + mlen, 512 - mlen); + } + + if (len - mlen) + r = recv(client_socket, buffer + mlen, len - mlen, 0); + + if (r == 0) { + client_socket = 0; + if (!ptr) + return 0; + } +#ifdef _WIN32 + if (r == SOCKET_ERROR) +#else + if (r == -1) +#endif + { + if (ptr == 0) + return -1; + r = 0; + } + + r += mlen; + + return r; +} + +void WriteSocket(char * buffer, int len) { + if (!client_socket) + return; + + send(client_socket, buffer, len, 0); +} + +void SetsBlock() { +#ifdef _WIN32 + u_long b = 0; + ioctlsocket(server_socket, FIONBIO, &b); +#else + int flags = fcntl(server_socket, F_GETFL, 0); + fcntl(server_socket, F_SETFL, flags & ~O_NONBLOCK); +#endif +} + +void SetsNonblock() { +#ifdef _WIN32 + u_long b = 1; + ioctlsocket(server_socket, FIONBIO, &b); +#else + int flags = fcntl(server_socket, F_GETFL, 0); + fcntl(server_socket, F_SETFL, flags | O_NONBLOCK); +#endif +} |
