add ode
This commit is contained in:
parent
a310325a61
commit
e0c6f631ab
15 changed files with 69 additions and 2546 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
|
@ -1,3 +1,6 @@
|
|||
[submodule "external/milsko"]
|
||||
path = external/milsko
|
||||
url = https://gitea.nishi.boats/pyrite-dev/milsko
|
||||
[submodule "external/ode"]
|
||||
path = external/ode
|
||||
url = https://github.com/thomasmarsh/ODE
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ include(GNUInstallDirs)
|
|||
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
|
||||
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
|
||||
|
||||
add_subdirectory(ode)
|
||||
|
||||
macro(install_target name)
|
||||
install(
|
||||
TARGETS ${name}
|
||||
|
|
@ -14,15 +16,14 @@ macro(install_target name)
|
|||
)
|
||||
endmacro()
|
||||
|
||||
add_subdirectory(net)
|
||||
|
||||
file(GLOB GearSrc_SRC src/*.c lz4/*.c)
|
||||
|
||||
add_library(GearSrc SHARED ${GearSrc_SRC})
|
||||
target_include_directories(GearSrc PUBLIC include)
|
||||
target_include_directories(GearSrc PRIVATE lz4)
|
||||
target_compile_definitions(GearSrc PRIVATE _GEARSRC)
|
||||
target_link_libraries(GearSrc PRIVATE gbnet)
|
||||
target_link_libraries(GearSrc PRIVATE ode)
|
||||
target_link_options(GearSrc PRIVATE -static-libgcc -static-libstdc++)
|
||||
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
|
||||
target_link_libraries(GearSrc PRIVATE m)
|
||||
endif()
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
---
|
||||
Language: Cpp
|
||||
UseTab: Always
|
||||
TabWidth: 8
|
||||
AlignConsecutiveAssignments:
|
||||
Enabled: true
|
||||
AlignConsecutiveDeclarations:
|
||||
Enabled: true
|
||||
IndentWidth: 8
|
||||
PointerAlignment: Left
|
||||
ColumnLimit: 0
|
||||
AllowShortIfStatementsOnASingleLine: Always
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
SpaceBeforeParens: Never
|
||||
AlignEscapedNewlines: DontAlign
|
||||
SortIncludes: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
#IndentPPDirectives: AfterHash
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
cmake_minimum_required(VERSION 3.11)
|
||||
project(gbnet)
|
||||
|
||||
add_library(gbnet SHARED src/client.c src/server.c src/socket.c src/stb_ds.c)
|
||||
target_include_directories(gbnet PUBLIC include)
|
||||
|
||||
find_package(PkgConfig)
|
||||
pkg_check_modules(ZLIB REQUIRED zlib)
|
||||
target_compile_options(gbnet PRIVATE ${ZLIB_CFLAGS_OTHER})
|
||||
target_include_directories(gbnet PRIVATE ${ZLIB_INCLUDE_DIRS})
|
||||
target_link_options(gbnet PRIVATE ${ZLIB_LDFLAGS_OTHER})
|
||||
target_link_directories(gbnet PRIVATE ${ZLIB_LIBRARY_DIRS})
|
||||
target_link_libraries(gbnet PRIVATE ${ZLIB_LIBRARIES})
|
||||
|
||||
install_target(gbnet)
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
#ifndef __GBNET_H__
|
||||
#define __GBNET_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#endif
|
||||
|
||||
/* TCP */
|
||||
#define NET_PORT 3219
|
||||
|
||||
#define NET_PING 3
|
||||
|
||||
enum gbnet_packet_type {
|
||||
NET_PACKET_PING = 0,
|
||||
NET_PACKET_PONG,
|
||||
NET_PACKET_USER
|
||||
};
|
||||
|
||||
typedef struct gbnet_client gbnet_client_t;
|
||||
typedef struct gbnet_server gbnet_server_t;
|
||||
typedef struct gbnet_packet_header gbnet_packet_header_t;
|
||||
typedef struct gbnet_packet gbnet_packet_t;
|
||||
|
||||
struct gbnet_client {
|
||||
int fd;
|
||||
time_t last_ping;
|
||||
void (*on_packet)(gbnet_client_t* client, gbnet_packet_t* packet);
|
||||
void (*on_disconnect)(gbnet_client_t* client);
|
||||
void* user;
|
||||
struct {
|
||||
int sent_ping;
|
||||
int cleanup;
|
||||
} server;
|
||||
};
|
||||
|
||||
struct gbnet_server {
|
||||
int fd;
|
||||
struct sockaddr_in address;
|
||||
gbnet_client_t* clients;
|
||||
void* user;
|
||||
void (*on_client)(gbnet_server_t* server, gbnet_client_t* client);
|
||||
void (*on_tick)(gbnet_server_t* server);
|
||||
};
|
||||
|
||||
#pragma pack(1)
|
||||
struct gbnet_packet_header {
|
||||
unsigned char type;
|
||||
unsigned short length; /* ok, seriously, who sends 64k at ONCE */
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
/* !!! REMEMBER TO FREE DATA AFTER RECV UNLESS IT'S NULL !!! */
|
||||
struct gbnet_packet {
|
||||
gbnet_packet_header_t header;
|
||||
void* data;
|
||||
};
|
||||
|
||||
gbnet_client_t* gbnet_client(const char* host, int port);
|
||||
void gbnet_client_on_packet(gbnet_client_t* client, void (*on_packet)(gbnet_client_t* client, gbnet_packet_t* packet));
|
||||
void gbnet_client_on_disconnect(gbnet_client_t* client, void (*on_disconnect)(gbnet_client_t* client));
|
||||
int gbnet_client_poll(gbnet_client_t* client);
|
||||
void gbnet_client_destroy(gbnet_client_t* client);
|
||||
|
||||
gbnet_server_t* gbnet_server(int port);
|
||||
int gbnet_server_clients(gbnet_server_t* server);
|
||||
void gbnet_server_loop(gbnet_server_t* server);
|
||||
void gbnet_server_on_client(gbnet_server_t* server, void (*on_client)(gbnet_server_t* server, gbnet_client_t* client));
|
||||
void gbnet_server_on_tick(gbnet_server_t* server, void (*on_tick)(gbnet_server_t* server));
|
||||
void gbnet_server_broadcast(gbnet_server_t* server, gbnet_packet_t* packet);
|
||||
void gbnet_server_destroy(gbnet_server_t* server);
|
||||
|
||||
int gbnet_fd_send(int fd, const void* buf, int length);
|
||||
int gbnet_fd_recv(int fd, void* buf, int length, int to);
|
||||
int gbnet_packet_send(int fd, gbnet_packet_t* packet);
|
||||
int gbnet_packet_recv(int fd, gbnet_packet_t* packet);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
#include <gbnet.h>
|
||||
|
||||
gbnet_client_t* gbnet_client(const char* host, int port) {
|
||||
gbnet_client_t* client = malloc(sizeof(*client));
|
||||
struct addrinfo hints;
|
||||
char p[32];
|
||||
struct addrinfo* result;
|
||||
struct addrinfo* rp;
|
||||
|
||||
if(port == 0) port = NET_PORT;
|
||||
sprintf(p, "%d", port);
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
if(getaddrinfo(host, p, &hints, &result) != 0) {
|
||||
free(client);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(rp = result; rp != NULL; rp = rp->ai_next) {
|
||||
int val = 1;
|
||||
|
||||
if((client->fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
val = 1;
|
||||
setsockopt(client->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(val));
|
||||
|
||||
if(connect(client->fd, rp->ai_addr, rp->ai_addrlen) >= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
close(client->fd);
|
||||
}
|
||||
|
||||
if(rp == NULL) {
|
||||
free(client);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
client->on_packet = NULL;
|
||||
client->on_disconnect = NULL;
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
void gbnet_client_on_packet(gbnet_client_t* client, void (*on_packet)(gbnet_client_t* client, gbnet_packet_t* packet)) {
|
||||
client->on_packet = on_packet;
|
||||
}
|
||||
|
||||
void gbnet_client_on_disconnect(gbnet_client_t* client, void (*on_disconnect)(gbnet_client_t* client)) {
|
||||
client->on_disconnect = on_disconnect;
|
||||
}
|
||||
|
||||
int gbnet_client_poll(gbnet_client_t* client) {
|
||||
struct pollfd pfd;
|
||||
gbnet_packet_t pkt;
|
||||
int ret;
|
||||
|
||||
pfd.fd = client->fd;
|
||||
pfd.events = POLLIN | POLLPRI;
|
||||
ret = poll(&pfd, 1, 0);
|
||||
if(ret <= 0) return ret;
|
||||
|
||||
ret = gbnet_packet_recv(client->fd, &pkt);
|
||||
if(ret == 0) return -1; /* connection broke, probably */
|
||||
|
||||
if(pkt.header.type == NET_PACKET_PING) {
|
||||
gbnet_packet_t ret;
|
||||
client->last_ping = time(NULL);
|
||||
|
||||
ret.header.type = NET_PACKET_PONG;
|
||||
ret.header.length = 0;
|
||||
ret.data = NULL;
|
||||
if(gbnet_packet_send(client->fd, &ret) == 0) {
|
||||
if(pkt.data != NULL) free(pkt.data);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if(client->on_packet != NULL) client->on_packet(client, &pkt);
|
||||
|
||||
if(pkt.data != NULL) free(pkt.data);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void gbnet_client_destroy(gbnet_client_t* client) {
|
||||
if(client->on_disconnect != NULL) client->on_disconnect(client);
|
||||
close(client->fd);
|
||||
free(client);
|
||||
}
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
#include <gbnet.h>
|
||||
|
||||
#include "stb_ds.h"
|
||||
|
||||
gbnet_server_t* gbnet_server(int port) {
|
||||
gbnet_server_t* server = malloc(sizeof(*server));
|
||||
int val;
|
||||
|
||||
if(port == 0) port = NET_PORT;
|
||||
|
||||
if((server->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
|
||||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
val = 1;
|
||||
setsockopt(server->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(val));
|
||||
|
||||
server->address.sin_family = AF_INET;
|
||||
server->address.sin_addr.s_addr = INADDR_ANY;
|
||||
server->address.sin_port = htons(port);
|
||||
|
||||
if(bind(server->fd, (struct sockaddr*)&server->address, sizeof(server->address)) < 0) {
|
||||
close(server->fd);
|
||||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(listen(server->fd, 128) < 0) {
|
||||
close(server->fd);
|
||||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
server->clients = NULL;
|
||||
server->on_client = NULL;
|
||||
server->on_tick = NULL;
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
int gbnet_server_clients(gbnet_server_t* server) {
|
||||
return arrlen(server->clients);
|
||||
}
|
||||
|
||||
void gbnet_server_loop(gbnet_server_t* server) {
|
||||
struct pollfd* pfd;
|
||||
|
||||
while(1) {
|
||||
int i;
|
||||
int ret;
|
||||
pfd = malloc(sizeof(*pfd) * (1 + arrlen(server->clients)));
|
||||
|
||||
pfd[0].fd = server->fd;
|
||||
pfd[0].events = POLLIN | POLLPRI;
|
||||
|
||||
for(i = 0; i < arrlen(server->clients); i++) {
|
||||
pfd[1 + i].fd = server->clients[i].fd;
|
||||
pfd[1 + i].events = POLLIN | POLLPRI;
|
||||
}
|
||||
|
||||
ret = poll(pfd, 1 + arrlen(server->clients), 50);
|
||||
if(ret < 0) {
|
||||
free(pfd);
|
||||
break;
|
||||
}
|
||||
|
||||
if(server->on_tick != NULL) server->on_tick(server);
|
||||
|
||||
if(pfd[0].revents & POLLIN) {
|
||||
gbnet_client_t client;
|
||||
struct sockaddr_in address;
|
||||
int len = sizeof(address);
|
||||
|
||||
client.fd = accept(server->fd, (struct sockaddr*)&address, &len);
|
||||
client.last_ping = time(NULL);
|
||||
client.server.cleanup = 0;
|
||||
client.server.sent_ping = 0;
|
||||
client.on_packet = NULL;
|
||||
|
||||
arrput(server->clients, client);
|
||||
|
||||
if(server->on_client != NULL) server->on_client(server, &server->clients[arrlen(server->clients) - 1]);
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Client %d accepted\n", server->clients[i].fd);
|
||||
#endif
|
||||
}
|
||||
|
||||
for(i = 0; i < arrlen(server->clients); i++) {
|
||||
time_t diff;
|
||||
if(server->clients[i].server.cleanup) continue;
|
||||
if(pfd[1 + i].revents & POLLIN) {
|
||||
gbnet_packet_t pkt;
|
||||
|
||||
if(gbnet_packet_recv(server->clients[i].fd, &pkt) == 0) {
|
||||
server->clients[i].server.cleanup = 1;
|
||||
} else {
|
||||
if(pkt.header.type == NET_PACKET_PONG && server->clients[i].server.sent_ping) {
|
||||
server->clients[i].last_ping = time(NULL);
|
||||
server->clients[i].server.sent_ping = 0;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Pong from client %d\n", server->clients[i].fd);
|
||||
#endif
|
||||
} else if(pkt.header.type == NET_PACKET_PING) {
|
||||
gbnet_packet_t ret;
|
||||
|
||||
ret.header.type = NET_PACKET_PONG;
|
||||
ret.header.length = 0;
|
||||
ret.data = NULL;
|
||||
|
||||
if(gbnet_packet_send(server->clients[i].fd, &ret) == 0) {
|
||||
server->clients[i].server.cleanup = 1;
|
||||
} else {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Ping from client %d, sending back pong\n", server->clients[i].fd);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if(!server->clients[i].server.cleanup && server->clients[i].on_packet != NULL) server->clients[i].on_packet(&server->clients[i], &pkt);
|
||||
|
||||
if(pkt.data != NULL) free(pkt.data);
|
||||
}
|
||||
}
|
||||
if(server->clients[i].server.cleanup) continue;
|
||||
|
||||
diff = time(NULL) - server->clients[i].last_ping;
|
||||
|
||||
if(diff == NET_PING && !server->clients[i].server.sent_ping) {
|
||||
gbnet_packet_t pkt;
|
||||
|
||||
pkt.header.type = NET_PACKET_PING;
|
||||
pkt.header.length = 0;
|
||||
pkt.data = NULL;
|
||||
|
||||
if(gbnet_packet_send(server->clients[i].fd, &pkt) == 0) {
|
||||
server->clients[i].server.cleanup = 1;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Failed to ping client %d\n", server->clients[i].fd);
|
||||
#endif
|
||||
} else {
|
||||
server->clients[i].server.sent_ping = 1;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Pinging client %d\n", server->clients[i].fd);
|
||||
#endif
|
||||
}
|
||||
} else if(diff == NET_PING * 2 && server->clients[i].server.sent_ping) {
|
||||
server->clients[i].server.cleanup = 1;
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Ping timeout (client %d)\n", server->clients[i].fd);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < arrlen(server->clients); i++) {
|
||||
if(server->clients[i].server.cleanup) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Client %d cleanup\n", server->clients[i].fd);
|
||||
#endif
|
||||
if(server->clients[i].on_disconnect != NULL) server->clients[i].on_disconnect(&server->clients[i]);
|
||||
close(server->clients[i].fd);
|
||||
arrdel(server->clients, i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
free(pfd);
|
||||
}
|
||||
}
|
||||
|
||||
void gbnet_server_on_client(gbnet_server_t* server, void (*on_client)(gbnet_server_t* server, gbnet_client_t* client)) {
|
||||
server->on_client = on_client;
|
||||
}
|
||||
|
||||
void gbnet_server_on_tick(gbnet_server_t* server, void (*on_tick)(gbnet_server_t* server)) {
|
||||
server->on_tick = on_tick;
|
||||
}
|
||||
|
||||
void gbnet_server_destroy(gbnet_server_t* server) {
|
||||
int i;
|
||||
for(i = 0; i < arrlen(server->clients); i++) {
|
||||
if(server->clients[i].on_disconnect != NULL) server->clients[i].on_disconnect(&server->clients[i]);
|
||||
close(server->clients[i].fd);
|
||||
}
|
||||
arrfree(server->clients);
|
||||
close(server->fd);
|
||||
free(server);
|
||||
}
|
||||
|
||||
void gbnet_server_broadcast(gbnet_server_t* server, gbnet_packet_t* packet) {
|
||||
int i;
|
||||
for(i = 0; i < arrlen(server->clients); i++) {
|
||||
if(gbnet_packet_send(server->clients[i].fd, packet) == 0) {
|
||||
server->clients[i].server.cleanup = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,200 +0,0 @@
|
|||
#include <gbnet.h>
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#define CHUNK 512
|
||||
|
||||
int gbnet_packet_send(int fd, gbnet_packet_t* packet) {
|
||||
int r;
|
||||
gbnet_packet_header_t h;
|
||||
int len = 0;
|
||||
unsigned char* data = NULL;
|
||||
|
||||
if(packet->header.length > 0) {
|
||||
z_stream strm;
|
||||
int in = packet->header.length;
|
||||
int seek = 0;
|
||||
int flush = 0;
|
||||
unsigned char buf[CHUNK];
|
||||
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
if(deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) return 0;
|
||||
|
||||
do {
|
||||
strm.avail_in = in <= CHUNK ? in : CHUNK;
|
||||
strm.next_in = ((unsigned char*)packet->data) + seek;
|
||||
|
||||
flush = in <= CHUNK ? Z_FINISH : Z_NO_FLUSH;
|
||||
|
||||
do {
|
||||
int have;
|
||||
|
||||
strm.avail_out = CHUNK;
|
||||
strm.next_out = buf;
|
||||
|
||||
deflate(&strm, flush);
|
||||
|
||||
have = CHUNK - strm.avail_out;
|
||||
|
||||
if(data == NULL) {
|
||||
data = malloc(have);
|
||||
memcpy(data, buf, have);
|
||||
} else {
|
||||
unsigned char* old = data;
|
||||
|
||||
data = malloc(len + have);
|
||||
memcpy(data, old, len);
|
||||
memcpy(data + len, buf, have);
|
||||
|
||||
free(data);
|
||||
}
|
||||
|
||||
len += have;
|
||||
} while(strm.avail_out == 0);
|
||||
|
||||
seek += CHUNK;
|
||||
} while(flush != Z_FINISH);
|
||||
|
||||
deflateEnd(&strm);
|
||||
}
|
||||
|
||||
memcpy(&h, &packet->header, sizeof(h));
|
||||
h.length = htons(len);
|
||||
|
||||
r = gbnet_fd_send(fd, &h, sizeof(h));
|
||||
if(r != sizeof(h)) return 0;
|
||||
|
||||
if(data != NULL) {
|
||||
r = gbnet_fd_send(fd, data, len);
|
||||
if(r != sizeof(h)) {
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
|
||||
return sizeof(h) + len;
|
||||
}
|
||||
|
||||
int gbnet_packet_recv(int fd, gbnet_packet_t* packet) {
|
||||
gbnet_packet_header_t h;
|
||||
|
||||
if(gbnet_fd_recv(fd, &h, sizeof(h), 500) != sizeof(h)) {
|
||||
packet->data = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(&packet->header, &h, sizeof(h));
|
||||
packet->header.length = ntohs(packet->header.length);
|
||||
packet->data = NULL;
|
||||
if(packet->header.length > 0) {
|
||||
unsigned char* data = malloc(packet->header.length);
|
||||
z_stream strm;
|
||||
int in = packet->header.length;
|
||||
int ret;
|
||||
int seek = 0;
|
||||
int len = 0;
|
||||
unsigned char buf[CHUNK];
|
||||
if(gbnet_fd_recv(fd, data, packet->header.length, 50) != packet->header.length) {
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
strm.avail_in = 0;
|
||||
strm.next_in = Z_NULL;
|
||||
|
||||
if(inflateInit(&strm) != Z_OK) {
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
do {
|
||||
strm.avail_in = in <= CHUNK ? in : CHUNK;
|
||||
strm.next_in = data + seek;
|
||||
|
||||
do {
|
||||
int have;
|
||||
|
||||
strm.avail_out = CHUNK;
|
||||
strm.next_out = buf;
|
||||
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
|
||||
if(ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
|
||||
inflateEnd(&strm);
|
||||
if(packet->data != NULL) {
|
||||
free(packet->data);
|
||||
packet->data = NULL;
|
||||
}
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
have = CHUNK - strm.avail_out;
|
||||
|
||||
if(packet->data == NULL) {
|
||||
packet->data = malloc(have);
|
||||
memcpy(packet->data, buf, have);
|
||||
} else {
|
||||
unsigned char* old = packet->data;
|
||||
|
||||
packet->data = malloc(len + have);
|
||||
memcpy(packet->data, old, len);
|
||||
memcpy(packet->data + len, buf, have);
|
||||
|
||||
free(packet->data);
|
||||
}
|
||||
|
||||
len += have;
|
||||
} while(strm.avail_out == 0);
|
||||
} while(ret != Z_STREAM_END);
|
||||
|
||||
inflateEnd(&strm);
|
||||
|
||||
free(data);
|
||||
|
||||
packet->header.length = len;
|
||||
}
|
||||
|
||||
return sizeof(h) + packet->header.length;
|
||||
}
|
||||
|
||||
int gbnet_fd_send(int fd, const void* buf, int length) {
|
||||
struct pollfd pfd;
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLOUT | POLLPRI;
|
||||
|
||||
poll(&pfd, 1, 0);
|
||||
if(!(pfd.revents & POLLOUT)) return 0;
|
||||
|
||||
return send(fd, buf, length, 0);
|
||||
}
|
||||
|
||||
int gbnet_fd_recv(int fd, void* buf, int length, int to) {
|
||||
struct pollfd pfd;
|
||||
int ret;
|
||||
unsigned char* b = buf;
|
||||
int i = 0;
|
||||
time_t t = time(NULL);
|
||||
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN | POLLPRI;
|
||||
|
||||
while(i < length) {
|
||||
/* i think 500 is reasonable timeout. no? */
|
||||
ret = poll(&pfd, 1, to);
|
||||
|
||||
if((time(NULL) - t) >= 5) return i; /* we waited enough! kill the connection */
|
||||
if(ret <= 0) return i;
|
||||
|
||||
ret = recv(fd, &b[i], 1, 0);
|
||||
if(ret < 1) return i;
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
#define STB_DS_IMPLEMENTATION
|
||||
#include "stb_ds.h"
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,11 +0,0 @@
|
|||
#include <gbnet.h>
|
||||
|
||||
void on(gbnet_client_t* client, gbnet_packet_t* pkt) {
|
||||
if(pkt->header.type == NET_PACKET_USER) printf("Received: %s\n", pkt->data);
|
||||
}
|
||||
|
||||
int main() {
|
||||
gbnet_client_t* client = gbnet_client("127.0.0.1", 0);
|
||||
gbnet_client_on_packet(client, on);
|
||||
while(gbnet_client_poll(client) >= 0);
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
#include <gbnet.h>
|
||||
|
||||
void on(gbnet_server_t* server, gbnet_client_t* client) {
|
||||
gbnet_packet_t pkt;
|
||||
|
||||
pkt.data = "Hello world!";
|
||||
pkt.header.length = strlen(pkt.data) + 1;
|
||||
pkt.header.flag = 0;
|
||||
pkt.header.type = NET_PACKET_USER;
|
||||
|
||||
gbnet_packet_send(client->fd, &pkt);
|
||||
}
|
||||
|
||||
int main() {
|
||||
gbnet_server_t* server = gbnet_server(0);
|
||||
gbnet_server_on_client(server, on);
|
||||
gbnet_server_loop(server);
|
||||
}
|
||||
50
engine/ode/CMakeLists.txt
Normal file
50
engine/ode/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
project(ode)
|
||||
|
||||
file(GLOB ODE_SRC
|
||||
../../external/ode/ode/src/*.c
|
||||
../../external/ode/ode/src/*.cpp
|
||||
../../external/ode/ode/src/joints/*.cpp
|
||||
../../external/ode/ou/src/ou/*.cpp
|
||||
../../external/ode/GIMPACT/src/*.cpp
|
||||
../../external/ode/libccd/src/*.c
|
||||
)
|
||||
add_library(ode ${ODE_SRC})
|
||||
target_include_directories(ode PRIVATE
|
||||
../../external/ode/include
|
||||
../../external/ode/ode/src
|
||||
../../external/ode/ode/src/joints
|
||||
../../external/ode/OPCODE
|
||||
../../external/ode/ou/include
|
||||
../../external/ode/GIMPACT/include
|
||||
../../external/ode/libccd/src/custom
|
||||
../../external/ode/libccd/src
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
list(REMOVE_ITEM ODE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../../external/ode/ode/src/collision_trimesh_trimesh_old.cpp)
|
||||
list(REMOVE_ITEM ODE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../../external/ode/ode/src/collision_trimesh_opcode.cpp)
|
||||
list(REMOVE_ITEM ODE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../../external/ode/ode/src/collision_trimesh_disabled.cpp)
|
||||
|
||||
target_compile_definitions(ode PRIVATE
|
||||
_OU_NAMESPACE=odeou
|
||||
_OU_FEATURE_SET=_OU_FEATURE_SET_TLS
|
||||
dLIBCCD_ENABLED
|
||||
dLIBCCD_INTERNAL
|
||||
dLIBCCD_BOX_CYL
|
||||
dLIBCCD_CYL_CYL
|
||||
dLIBCCD_CAP_CYL
|
||||
dLIBCCD_CONVEX_BOX
|
||||
dLIBCCD_CONVEX_CAP
|
||||
dLIBCCD_CONVEX_CYL
|
||||
dLIBCCD_CONVEX_SPHERE
|
||||
dLIBCCD_CONVEX_CONVEX
|
||||
)
|
||||
|
||||
set(ODE_PRECISION "dDOUBLE")
|
||||
set(CCD_PRECISION "CCD_DOUBLE")
|
||||
set(ODE_VERSION "Custom-ODE")
|
||||
|
||||
configure_file(../../external/ode/include/ode/version.h.in ode/version.h)
|
||||
configure_file(../../external/ode/include/ode/precision.h.in ode/precision.h)
|
||||
configure_file(../../external/ode/libccd/src/ccd/precision.h.in ccd/precision.h)
|
||||
configure_file(config.h.in config.h)
|
||||
11
engine/ode/config.h.in
Normal file
11
engine/ode/config.h.in
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef _ODE_CONFIG_H_
|
||||
#define _ODE_CONFIG_H_
|
||||
#define dTRIMESH_ENABLED 1
|
||||
#define dTRIMESH_GIMPACT 1
|
||||
#define dOU_ENABLED 1
|
||||
#define dATOMICS_ENABLED 0
|
||||
#define dTLS_ENABLED 0
|
||||
#define dBUILTIN_THREADING_IMPL_ENABLED 0
|
||||
#define dTHREADING_INTF_DISABLED 1
|
||||
#include "typedefs.h"
|
||||
#endif
|
||||
1
external/ode
vendored
Submodule
1
external/ode
vendored
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit bcfb66cd5e18e32e27cfaae3651ead930bbcda13
|
||||
Loading…
Add table
Add a link
Reference in a new issue