Browse Source

Add godot socket implementation

Fabio Alessandrelli 8 years ago
parent
commit
4cc1b04586
2 changed files with 238 additions and 0 deletions
  1. 42 0
      thirdparty/enet/enet/godot.h
  2. 196 0
      thirdparty/enet/godot.cpp

+ 42 - 0
thirdparty/enet/enet/godot.h

@@ -0,0 +1,42 @@
+/**
+ @file  godot.h
+ @brief ENet Godot header
+*/
+#ifndef __ENET_GODOT_H__
+#define __ENET_GODOT_H__
+
+#ifdef WINDOWS_ENABLED
+#include <stdint.h>
+#include <winsock2.h>
+#endif
+#ifdef UNIX_ENABLED
+#include <arpa/inet.h>
+#endif
+
+#ifdef MSG_MAXIOVLEN
+#define ENET_BUFFER_MAXIMUM MSG_MAXIOVLEN
+#endif
+
+typedef void *ENetSocket;
+
+#define ENET_SOCKET_NULL NULL
+
+#define ENET_HOST_TO_NET_16(value) (htons(value)) /**< macro that converts host to net byte-order of a 16-bit value */
+#define ENET_HOST_TO_NET_32(value) (htonl(value)) /**< macro that converts host to net byte-order of a 32-bit value */
+
+#define ENET_NET_TO_HOST_16(value) (ntohs(value)) /**< macro that converts net to host byte-order of a 16-bit value */
+#define ENET_NET_TO_HOST_32(value) (ntohl(value)) /**< macro that converts net to host byte-order of a 32-bit value */
+
+typedef struct
+{
+	void *data;
+	size_t dataLength;
+} ENetBuffer;
+
+#define ENET_CALLBACK
+
+#define ENET_API extern
+
+typedef void ENetSocketSet;
+
+#endif /* __ENET_GODOT_H__ */

+ 196 - 0
thirdparty/enet/godot.cpp

@@ -0,0 +1,196 @@
+/**
+ @file  godot.c
+ @brief ENet Godot specific functions
+*/
+
+#include "core/io/ip.h"
+#include "core/io/packet_peer_udp.h"
+#include "core/os/os.h"
+
+// This must be last for windows to compile (tested with MinGW)
+#include "enet/enet.h"
+
+static enet_uint32 timeBase = 0;
+
+int enet_initialize(void) {
+
+	return 0;
+}
+
+void enet_deinitialize(void) {
+}
+
+enet_uint32 enet_host_random_seed(void) {
+
+	return (enet_uint32)OS::get_singleton()->get_unix_time();
+}
+
+enet_uint32 enet_time_get(void) {
+
+	return OS::get_singleton()->get_ticks_msec() - timeBase;
+}
+
+void enet_time_set(enet_uint32 newTimeBase) {
+
+	timeBase = OS::get_singleton()->get_ticks_msec() - newTimeBase;
+}
+
+int enet_address_set_host(ENetAddress *address, const char *name) {
+
+	IP_Address ip = IP::get_singleton()->resolve_hostname(name);
+	ERR_FAIL_COND_V(!ip.is_valid(), -1);
+
+	enet_address_set_ip(address, ip.get_ipv6(), 16);
+	return 0;
+}
+
+void enet_address_set_ip(ENetAddress *address, const uint8_t *ip, size_t size) {
+
+	int len = size > 16 ? 16 : size;
+	memset(address->host, 0, 16);
+	memcpy(address->host, ip, len);
+}
+
+int enet_address_get_host_ip(const ENetAddress *address, char *name, size_t nameLength) {
+
+	return -1;
+}
+
+int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLength) {
+
+	return -1;
+}
+
+int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
+
+	IP_Address ip;
+	if (address->wildcard) {
+		ip = IP_Address("*");
+	} else {
+		ip.set_ipv6(address->host);
+	}
+
+	PacketPeerUDP *sock = (PacketPeerUDP *)socket;
+	if (sock->listen(address->port, ip) != OK) {
+		return -1;
+	}
+	return 0;
+}
+
+ENetSocket enet_socket_create(ENetSocketType type) {
+
+	return PacketPeerUDP::create();
+}
+
+void enet_socket_destroy(ENetSocket socket) {
+	PacketPeerUDP *sock = (PacketPeerUDP *)socket;
+	sock->close();
+	memdelete(sock);
+}
+
+int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
+
+	ERR_FAIL_COND_V(address == NULL, -1);
+
+	PacketPeerUDP *sock = (PacketPeerUDP *)socket;
+	IP_Address dest;
+	Error err;
+	size_t i = 0;
+
+	dest.set_ipv6(address->host);
+	sock->set_dest_address(dest, address->port);
+
+	// Create a single packet.
+	PoolVector<uint8_t> out;
+	PoolVector<uint8_t>::Write w;
+	int size = 0;
+	int pos = 0;
+	for (i = 0; i < bufferCount; i++) {
+		size += buffers[i].dataLength;
+	}
+
+	out.resize(size);
+	w = out.write();
+	for (i = 0; i < bufferCount; i++) {
+		memcpy(&w[pos], buffers[i].data, buffers[i].dataLength);
+		pos += buffers[i].dataLength;
+	}
+
+	err = sock->put_packet((const uint8_t *)&w[0], size);
+	if (err != OK) {
+		WARN_PRINT("Sending failed!");
+		return -1;
+	}
+
+	return size;
+}
+
+int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
+
+	ERR_FAIL_COND_V(bufferCount != 1, -1);
+
+	PacketPeerUDP *sock = (PacketPeerUDP *)socket;
+
+	if (sock->get_available_packet_count() == 0) {
+		return 0;
+	}
+
+	const uint8_t *buffer;
+	int buffer_size;
+	Error err = sock->get_packet(&buffer, buffer_size);
+	if (err)
+		return -1;
+
+	copymem(buffers[0].data, buffer, buffer_size);
+
+	enet_address_set_ip(address, sock->get_packet_address().get_ipv6(), 16);
+	address->port = sock->get_packet_port();
+
+	return buffer_size;
+}
+
+// Not implemented
+int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
+
+	return 0; // do we need this function?
+}
+
+int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
+
+	return -1; // do we need this function?
+}
+
+int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
+
+	return -1;
+}
+
+int enet_socket_listen(ENetSocket socket, int backlog) {
+
+	return -1;
+}
+
+int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
+
+	return -1;
+}
+
+int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
+
+	return -1;
+}
+
+int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
+
+	return -1;
+}
+
+ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
+
+	return NULL;
+}
+
+int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
+
+	return -1;
+}