From 7e23d2d01382f8595b01e4aa57aab2954c8e7079 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Thu, 12 Feb 2015 02:01:02 +0000 Subject: [PATCH] add basic server connection functions --- projects/openrct2.vcxproj | 2 + projects/openrct2.vcxproj.filters | 6 ++ src/network/network.c | 111 ++++++++++++++++++++++++++++++ src/network/network.h | 42 +++++++++++ 4 files changed, 161 insertions(+) create mode 100644 src/network/network.c create mode 100644 src/network/network.h diff --git a/projects/openrct2.vcxproj b/projects/openrct2.vcxproj index 156eefd8f4..baa73155fe 100644 --- a/projects/openrct2.vcxproj +++ b/projects/openrct2.vcxproj @@ -75,6 +75,7 @@ + @@ -230,6 +231,7 @@ + diff --git a/projects/openrct2.vcxproj.filters b/projects/openrct2.vcxproj.filters index cfc5b0b556..b851bb9a90 100644 --- a/projects/openrct2.vcxproj.filters +++ b/projects/openrct2.vcxproj.filters @@ -498,6 +498,9 @@ Source\Localisation + + Source\Network + Source\Drawing @@ -740,5 +743,8 @@ Source\Drawing + + Source\Network + \ No newline at end of file diff --git a/src/network/network.c b/src/network/network.c new file mode 100644 index 0000000000..8ecbc3310b --- /dev/null +++ b/src/network/network.c @@ -0,0 +1,111 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 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 3 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 . + *****************************************************************************/ + +#ifndef DISABLE_NETWORK + +#include "network.h" + +#pragma comment(lib, "Ws2_32.lib") + +static int _wsaInitialised = 0; +static WSADATA _wsaData; +static SOCKET _clientSocket; + +int network_init() +{ + if (!_wsaInitialised) { + log_verbose("Initialising WSA"); + if (WSAStartup(MAKEWORD(2, 2), &_wsaData) != 0) { + log_error("Unable to initialise winsock."); + return 0; + } + _wsaInitialised = 1; + } + + return 1; +} + +void network_close() +{ + log_verbose("Closing WSA"); + WSACleanup(); + _wsaInitialised = 0; +} + +int network_begin_server(int port) +{ + SOCKET listeningSocket; + SOCKADDR_IN localAddress; + + if (!network_init()) + return 0; + + log_verbose("Begin listening for clients"); + listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listeningSocket == INVALID_SOCKET) { + log_error("Unable to create socket."); + return 0; + } + + localAddress.sin_family = AF_INET; + localAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); + localAddress.sin_port = htons(port); + + if (bind(listeningSocket, (SOCKADDR*)&localAddress, sizeof(SOCKADDR_IN)) != 0) { + closesocket(listeningSocket); + log_error("Unable to bind to socket."); + return 0; + } + + if (listen(listeningSocket, SOMAXCONN) != 0) { + closesocket(listeningSocket); + log_error("Unable to listen on socket."); + return 0; + } + + printf("Waiting for client...\n"); + _clientSocket = accept(listeningSocket, NULL, NULL); + if (_clientSocket == INVALID_SOCKET) { + closesocket(listeningSocket); + log_error("Failed to accept client."); + return 0; + } + + closesocket(listeningSocket); + printf("Connected to client!"); + return 1; +} + +void network_end_server() +{ + closesocket(_clientSocket); +} + +int network_send_data(uint8 *data, int length) +{ + return send(_clientSocket, data, length, 0); +} + +int network_receive_data(uint8 *data, int maxLength) +{ + return recv(_clientSocket, data, maxLength, 0); +} + +#endif /* DISABLE_NETWORK */ \ No newline at end of file diff --git a/src/network/network.h b/src/network/network.h new file mode 100644 index 0000000000..e0d6d25583 --- /dev/null +++ b/src/network/network.h @@ -0,0 +1,42 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 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 3 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 . + *****************************************************************************/ + +#ifndef _NETWORK_H_ +#define _NETWORK_H_ + +#ifndef DISABLE_NETWORK + +#include + +#include "../common.h" + +#define NETWORK_DEFAULT_PORT 11753 + +int network_init(); +void network_close(); +int network_begin_server(int port); +void network_end_server(); + +int network_send_data(uint8 *data, int length); +int network_receive_data(uint8 *data, int maxLength); + +#endif /* DISABLE_NETWORK */ + +#endif \ No newline at end of file