1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-05 09:48:42 +00:00

(libretro-common) Create net_socket

This commit is contained in:
twinaphex 2016-05-01 21:18:45 +02:00
parent ad71c56ea4
commit a7b856b65f
6 changed files with 110 additions and 60 deletions

View File

@ -952,6 +952,7 @@ ifeq ($(HAVE_NETWORKING), 1)
DEFINES += -DHAVE_NETWORKING
OBJ += libretro-common/net/net_compat.o \
libretro-common/net/net_http.o \
libretro-common/net/net_socket.o \
net_http_special.o \
tasks/task_http.o

View File

@ -26,6 +26,7 @@
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
#include <string/stdstring.h>
#include "msg_hash.h"
@ -122,41 +123,25 @@ static const struct cmd_map map[] = {
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port)
{
struct addrinfo hints = {0};
char port_buf[16] = {0};
int *file_desc = (int*)&handle->net_fd;
struct addrinfo *res = NULL;
int yes = 1;
if (!network_init())
return false;
RARCH_LOG("Bringing up command interface on port %hu.\n",
(unsigned short)port);
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo_retro(NULL, port_buf, &hints, &res) < 0)
if (!socket_init(res, file_desc, port, NULL))
goto error;
handle->net_fd = socket(res->ai_family,
res->ai_socktype, res->ai_protocol);
if (handle->net_fd < 0)
if (*file_desc < 0)
goto error;
if (!socket_nonblock(handle->net_fd))
if (!socket_nonblock(*file_desc))
goto error;
setsockopt(handle->net_fd, SOL_SOCKET,
setsockopt(*file_desc, SOL_SOCKET,
SO_REUSEADDR, (const char*)&yes, sizeof(int));
if (bind(handle->net_fd, res->ai_addr, res->ai_addrlen) < 0)
if (bind(*file_desc, res->ai_addr, res->ai_addrlen) < 0)
{
RARCH_ERR("Failed to bind socket.\n");
goto error;

View File

@ -789,6 +789,7 @@ NETPLAY
#include "../netplay/netplay_common.c"
#include "../netplay/netplay.c"
#include "../libretro-common/net/net_compat.c"
#include "../libretro-common/net/net_socket.c"
#include "../libretro-common/net/net_http.c"
#ifndef HAVE_SOCKET_LEGACY
#include "../libretro-common/net/net_ifinfo.c"

View File

@ -0,0 +1,38 @@
/* Copyright (C) 2010-2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (net_socket.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _LIBRETRO_SDK_NET_SOCKET_H
#define _LIBRETRO_SDK_NET_SOCKET_H
#include <stdint.h>
#include <boolean.h>
#include <string.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
bool socket_init(void *address, int *fd, uint16_t port, const char *server);
RETRO_END_DECLS
#endif

View File

@ -0,0 +1,62 @@
/* Copyright (C) 2010-2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (net_socket.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
bool socket_init(void *address, int *fd, uint16_t port, const char *server)
{
char port_buf[16] = {0};
struct addrinfo hints = {0};
struct addrinfo *addr = (struct addrinfo*)address;
if (!fd)
goto error;
if (!network_init())
goto error;
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo_retro(server, port_buf, &hints, &addr) < 0)
goto error;
if (!addr)
goto error;
*fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
return true;
error:
return false;
}

View File

@ -23,6 +23,7 @@
#include <string.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
#include <retro_endianness.h>
#include "netplay_private.h"
@ -758,44 +759,6 @@ static bool init_tcp_socket(netplay_t *netplay, const char *server,
return ret;
}
static int socket_init(void *address, int *fd, uint16_t port, const char *server)
{
char port_buf[16] = {0};
struct addrinfo hints = {0};
struct addrinfo *addr = (struct addrinfo*)address;
if (!fd)
goto error;
if (!network_init())
goto error;
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo_retro(server, port_buf, &hints, &addr) < 0)
goto error;
if (!addr)
goto error;
*fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
return true;
error:
RARCH_ERR("Failed to initialize socket.\n");
return false;
}
static bool init_udp_socket(netplay_t *netplay, const char *server,
uint16_t port)
{