Moved server test out of library

This commit is contained in:
Dreaded_X 2020-05-06 22:41:41 +02:00
parent 3b49c00df0
commit c794e4a5d2
17 changed files with 2 additions and 1081 deletions

View File

@ -1,16 +0,0 @@
[flint.py]
version = feature_git
base_url = https://downloads.mtgames.nl/release/flint
[flint.py-plugins]
[git-vulkan-headers]
url = https://github.com/KhronosGroup/Vulkan-Headers
revision = master
commit = 42ad3f90faec009b9435383ee89910846d6a91ba
[git-glfw]
url = https://github.com/glfw/glfw
revision = 3.3-stable
commit = b1309dd42a72c8f7cd58a6f75329c4328679aed2

1
.gitignore vendored
View File

@ -30,7 +30,6 @@ Session.vim
tags
# flint
.flint
.build
# Custom

9
.gitmodules vendored
View File

@ -1,9 +0,0 @@
[submodule "vendor/sol2"]
path = vendor/sol2
url = https://github.com/ThePhD/sol2
[submodule "vendor/lua"]
path = vendor/lua
url = https://github.com/lua/lua
[submodule "vendor/stduuid"]
path = vendor/stduuid
url = https://github.com/mariusbancila/stduuid

View File

@ -1,3 +1,3 @@
let &makeprg="./flint.py --version ../flint/.flint/build/linux/debug/bin"
let &makeprg="../flint2/.build/bin/flint"
map <silent> <F9> :Make<cr>
map <silent> <F10> :Start ./flint.py --version ../flint/.flint/build/linux/debug/bin -rs && read<cr>
map <silent> <F10> :Start ./.build/bin/test && read<cr>

102
flint.lua
View File

@ -1,102 +0,0 @@
lib "lua"
src("*vendor/lua", "-vendor/lua/lua.c")
include "vendor/lua"
include "vendor/headers"
warnings(false)
lib "sol2"
include "vendor/sol2"
dependency "lua"
lib "stduuid"
include "vendor/stduuid/include"
link "uuid"
lib "ecs"
path "ecs"
dependency("sol2", "stduuid")
lib "ecs-lua"
path "ecs-lua"
dependency "ecs"
subfile("../iohelper/flint.lua", "iohelper")
lib "ecs-serial"
path "ecs-serial"
dependency("ecs", "iohelper")
function codegen(path, file)
local handle = io.popen("mkdir " .. config.paths.build .. "/generated")
handle:close()
local command = "python test.py " .. path .. "/" .. file .. " > " .. config.paths.build .. "/generated/ecs_" .. file
handle = io.popen(command)
handle:close()
end
executable "test"
path "test"
dependency "ecs-lua"
dependency "ecs-serial"
hook(step.PRE_BUILD, codegen, "test/include", "components.h")
include(config.paths.build .. "/generated/")
git("https://github.com/glfw/glfw", "3.3-stable", "glfw")
lib "glfw"
include "@glfw@/include"
src "@glfw@/src/{context,init,input,monitor,window}.c"
src "@glfw@/src/egl_context.c"
if config.platform.target == "linux" then
src "@glfw@/src/glx_context.c"
src "@glfw@/src/x11_{init,monitor,window}.c"
src "@glfw@/src/posix_{time,thread}.c"
src "@glfw@/src/linux_joystick.c"
src "@glfw@/src/xkb_unicode.c"
src "@glfw@/src/osmesa_context.c"
define "_GLFW_X11"
-- @todo This is needed for strdup to work
define "_POSIX_C_SOURCE=200809L"
link("dl", "X11", "Xrandr", "Xinerama", "Xcursor", "GL", "GLU")
elseif config.platform.target == "windows" then
end
lang "c11"
warnings(false)
lib "network-shared"
path "network-shared"
dependency "ecs-serial"
hook(step.PRE_BUILD, codegen, "network-shared/include", "network_components.h")
include(config.paths.build .. "/generated/")
executable "server"
path "network-server"
dependency "network-shared"
dependency "iohelper"
threads()
executable "client"
path "network-client"
dependency "network-shared"
dependency "glfw"
threads()
run_dir "test"
run_target "test"

279
flint.py
View File

@ -1,279 +0,0 @@
#!/usr/bin/env python3
import os
import platform
import errno
import configparser
import urllib.request
import shutil
import subprocess
from subprocess import PIPE
import stat
import sys
import argparse
flintlock = ".flintlock"
def install_plugin(url, plugins_folder, config, local = False):
metainfo = configparser.ConfigParser()
if not local:
req = urllib.request.Request(url + "/metainfo")
with urllib.request.urlopen(req) as response:
metainfo.read_string(response.read().decode("ascii"))
else:
with open(os.path.join(url, "metainfo.local"), "r") as f:
metainfo.read_file(f)
plugin_name = metainfo["meta"]["name"] + "@" + metainfo["meta"]["author"]
# Make sure we can't have local and remote version of the same plugin at the same time
# @todo This could be cleaner
if config.has_option("flint.py-plugins", plugin_name if local else plugin_name + ".local"):
config.remove_option("flint.py-plugins", plugin_name if local else plugin_name + ".local")
print("Installing '" + plugin_name + "'")
plugin_folder = os.path.join(plugins_folder, plugin_name)
if os.path.exists(plugin_folder):
shutil.rmtree(plugin_folder)
os.makedirs(plugin_folder)
# Register the plugin so that we can later update
config["flint.py-plugins"][plugin_name if not local else plugin_name + ".local"] = url
with open(flintlock, "w") as f:
config.write(f)
# @todo Use this same system for flint self
# @todo Add multiplatform support
if platform.system() == "Linux" and metainfo.has_section("linux"):
for filename in metainfo["linux"]:
if not local:
req = urllib.request.Request(url + "/" + filename)
with urllib.request.urlopen(req) as response, open(os.path.join(plugin_folder, filename), "wb") as f:
shutil.copyfileobj(response, f)
else:
os.symlink(os.path.abspath(os.path.join(url, metainfo["linux"][filename])), os.path.join(plugin_folder, filename))
elif platform.system() == "Windows" and metainfo.has_section("windows"):
for filename in metainfo["windows"]:
if not local:
req = urllib.request.Request(url + "/" + filename)
with urllib.request.urlopen(req) as response, open(os.path.join(plugin_folder, filename), "wb") as f:
shutil.copyfileobj(response, f)
else:
# @todo This requires admin mode on windows
os.symlink(os.path.abspath(os.path.join(url, metainfo["windows"][filename])), os.path.join(plugin_folder, filename))
else:
print("Plugin not available for current platform")
def remove_plugin(plugin_name, plugins_folder, config):
plugin_folder = os.path.join(plugins_folder, plugin_name)
if config.has_option("flint.py-plugins", plugin_name):
config.remove_option("flint.py-plugins", plugin_name)
elif config.has_option("flint.py-plugins", plugin_name + ".local"):
config.remove_option("flint.py-plugins", plugin_name + ".local")
else:
print("Plugin '" + plugin_name + "' does not exist")
return
if os.path.exists(plugin_folder):
shutil.rmtree(plugin_folder)
def main():
# @todo Add update, gets latest version and stores the specific version
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--config", action='store_const', const=True, default=False)
parser.add_argument("--local", action='store_const', const=True, default=False)
parser.add_argument("--generate")
parser.add_argument("--version")
parser.add_argument("--install", nargs='?', const="__update__")
parser.add_argument("--remove")
parser.add_argument("--debug-flint", action='store_const', const=True, default=False)
args, unknownargs = parser.parse_known_args()
flintdir_base = ".flint"
flintdir = os.path.join(flintdir_base, "versions")
flintplug = os.path.join(flintdir_base, "plugins")
flintbin = "flint"
if os.name == "nt":
flintbin += ".exe"
# Make sure the flintlock file exists
if not os.path.isfile(flintlock):
# Create a new flintlock file with default values
config = configparser.ConfigParser()
with open(flintlock, "w") as f:
config.write(f)
# Open the flintlock file
config = configparser.ConfigParser()
with open(flintlock, "r") as f:
config.read_file(f)
# Make sure that all required keys exist
if not config.has_section("flint.py"):
config["flint.py"] = {}
if not config.has_option("flint.py", "version"):
config["flint.py"]["version"] = "nightly"
if not config.has_option("flint.py", "base_url"):
config["flint.py"]["base_url"] = "https://downloads.mtgames.nl/release/flint"
if not config.has_section("flint.py-plugins"):
config["flint.py-plugins"] = {}
with open(flintlock, "w") as f:
config.write(f)
# Check if the user has enable config mode
if args.config:
# @todo Improve config mode
if args.version:
config["flint.py"]["version"] = args.version
print("Flint will now use version: " + args.version)
else:
print("Did nothing...")
with open(flintlock, "w") as f:
config.write(f)
elif args.generate:
if not os.path.exists(args.generate + "/src"):
os.makedirs(args.generate + "/src")
if not os.path.exists(args.generate + "/include"):
os.makedirs(args.generate + "/include")
# @todo Also generate a simlpe example cpp file
with open("./flint.lua", "w") as f:
f.write("executable \"" + args.generate + "\"\n")
f.write("\tpath \"" + args.generate + "\"\n")
f.write("\n")
f.write("run_target \"" + args.generate + "\"\n")
elif args.install:
# @todo We need to auto update all plugins
# @todo We need to be able to remove plugins
metainfo = configparser.ConfigParser()
if args.install == "__update__":
for plugin in config["flint.py-plugins"]:
# @todo Make the download functions actual functions so we can call them here
install_plugin(config["flint.py-plugins"][plugin], flintplug, config, plugin.endswith(".local"))
else:
install_plugin(args.install, flintplug, config, args.local)
elif args.remove:
remove_plugin(args.remove, flintplug, config)
with open(flintlock, "w") as f:
config.write(f)
else:
# Check desired version
version = config["flint.py"]["version"]
base_url = config["flint.py"]["base_url"]
if args.version:
version = args.version
print("Flint: " + version)
flintdir = os.path.join(flintdir, version)
if os.path.isfile(os.path.join(version, flintbin)):
# The version contains a path to a flint executable
flintdir = version
else:
# Make sure the flint directory exists
if not os.path.exists(flintdir):
try:
os.makedirs(flintdir)
except OSError as exc:
if exc != errno.EEXIST:
raise
# Check if we need to update
needs_update = False
# Download actual checksum
checksums = configparser.ConfigParser()
try:
req = urllib.request.Request(base_url + "/" + version + "/checksums")
with urllib.request.urlopen(req) as response:
checksums.read_string(response.read().decode("ascii"))
checksums_current = configparser.ConfigParser()
if (not os.path.isfile(os.path.join(flintdir, flintbin))) or (not os.path.isfile(os.path.join(flintdir, "checksums"))):
# We need to download instead of update
needs_update = True
checksums_current["flint"] = {}
print("Downloading flint...")
else:
# Load the current checksum
with open(os.path.join(flintdir, "checksums"), "r") as f:
checksums_current.read_file(f)
# Compare the checksum to determine if updates are needed
# @note This does not detect if the executable is corrupted
if not checksums_current["flint"][flintbin] == checksums["flint"][flintbin]:
needs_update = True
print("Updating flint...")
# Store the new checksum
checksums_current["flint"][flintbin] = checksums["flint"][flintbin]
with open(os.path.join(flintdir, "checksums"), "w") as f:
checksums_current.write(f)
except urllib.error.URLError as error:
print("Unable to update flint!")
needs_update = False
# @todo Show progress bar
if needs_update:
try:
req = urllib.request.Request(base_url + "/" + version + "/" + flintbin)
with urllib.request.urlopen(req) as response, open(flintdir + "/" + flintbin, "wb") as f:
shutil.copyfileobj(response, f)
# Make sure we can execute flint
mode = os.stat(os.path.join(flintdir, flintbin)).st_mode
os.chmod(os.path.join(flintdir, flintbin), mode | stat.S_IEXEC)
with open(flintlock, "w") as f:
config.write(f)
except urllib.error.URLError as error:
print("Flint update failed!")
needs_update = False
command = []
env = os.environ
if os.name == "nt":
env["PATH"] = flintdir + ";.flint/plugins;" + env["PATH"]
else:
env["LD_LIBRARY_PATH"] = flintdir + ":.flint/plugins"
if os.name == "nt":
command.extend([r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat", "x64", "&&"])
env["VSCMD_START_DIR"] = os.getcwd()
if args.debug_flint:
if platform.system() == "Linux":
command.extend(["gdb", "--args"])
elif platform.system() == "Windows":
command.extend(["devenv", "/debugexe"])
else:
print("Debugging not available for current platform")
command.append(os.path.join(flintdir, flintbin))
command.extend(unknownargs)
# Python pre 3.5 does not support run
return_code = 0
if sys.version_info[1] < 5:
subprocess.call(command, env=env)
else:
return_code = subprocess.run(command, env=env).returncode
exit(return_code)
if __name__ == "__main__":
main()

View File

@ -1,225 +0,0 @@
#include <iostream>
#include <array>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ecs-serial.h"
#include "ecs_network_components.h"
#include "iohelper/memstream.h"
#include "packets.h"
#include "debug.h"
#include <queue>
#include <mutex>
#include <thread>
#include <GLFW/glfw3.h>
// template <typename T>
// class ThreadQueue {
// public:
// void push(const T& value) {
// std::lock_guard<std::mutex> lock(_mutex);
// _queue.push(value);
// }
//
// void pop() {
// std::lock_guard<std::mutex> lock(_mutex);
// _queue.pop();
// }
//
// void front() {
//
// }
//
// private:
// std::queue<T> _queue;
// std::mutex _mutex;
// };
int main() {
std::queue<std::array<uint8_t, 1024>> thread_queue;
GLFWwindow* window;
if (!glfwInit()) {
return -1;
}
window = glfwCreateWindow(640, 480, "Network Client", nullptr, nullptr);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
ecs::Manager manager;
generated::init();
// IPv4
int address_family = AF_INET;
// UDP
int type = SOCK_DGRAM;
int protocol = 0;
int sock = socket(address_family, type, protocol);
if (sock < 0) {
throw std::runtime_error("Failed to create socket!");
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
throw std::runtime_error("Failed to set timeout");
}
sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9999);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
sockaddr* to = (sockaddr*)&server_address;
int to_size = sizeof(server_address);
std::array<uint8_t, 1024> buff;
// @todo Implement iomemstream
iohelper::omemstream_fixed<buff.size()> buff_stream_out(buff);
iohelper::imemstream_fixed<buff.size()> buff_stream_in(buff);
iohelper::write<uint8_t>(buff_stream_out, PACKET::CONNECT);
// @todo Resend connect request until we have an ack (or reach timeout)
// @todo Same for requesting initial ids and entities
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
throw std::runtime_error("Failed to connect to server");
}
sockaddr_in from;
socklen_t from_size = sizeof(from);
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, &from_size);
if (bytes_received < 0) {
throw std::runtime_error("Failed to receive data!");
}
if (iohelper::read<uint8_t>(buff_stream_in) == PACKET::ACK) {
std::cout << "Connected to server!\n";
} else {
throw std::runtime_error("Unexpected server response");
}
bool connected = true;
uint8_t timeout = TIMEOUT;
std::thread receive([&thread_queue, sock, &from, &from_size, &window, &connected, &timeout] {
while (!glfwWindowShouldClose(window) && connected) {
std::array<uint8_t, 1024> buff;
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, &from_size);
if (bytes_received < 0) {
timeout--;
if (timeout == 0) {
std::cout << "Connection to server lost!\n";
connected = false;
} else if (timeout < TIMEOUT/3) {
std::cout << "Server connection interrupted\n";
}
} else {
timeout = TIMEOUT;
thread_queue.push(std::move(buff));
}
}
});
while (!glfwWindowShouldClose(window) && connected) {
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
buff_stream_out.seekp(0, std::ios::beg);
iohelper::write<uint8_t>(buff_stream_out, PACKET::EVENT);
iohelper::write<std::string>(buff_stream_out, "up");
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
throw std::runtime_error("Failed to connect to server");
}
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
buff_stream_out.seekp(0, std::ios::beg);
iohelper::write<uint8_t>(buff_stream_out, PACKET::EVENT);
iohelper::write<std::string>(buff_stream_out, "down");
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
throw std::runtime_error("Failed to connect to server");
}
}
while (!thread_queue.empty()) {
std::array<uint8_t, 1024> buff = thread_queue.front();
iohelper::imemstream_fixed<buff.size()> buff_stream_in(buff);
buff_stream_in.seekg(0, std::ios::beg);
PACKET type = (PACKET)iohelper::read<uint8_t>(buff_stream_in);
switch (type) {
case PACKET::IDS:
std::cout << "Receiving ids\n";
ecs::serial::deserialize_ids(buff_stream_in);
break;
case PACKET::ENTITIES: {
std::cout << "Receiving entities\n";
size_t entity_count = iohelper::read_length(buff_stream_in);
for (size_t i = 0; i < entity_count; ++i) {
ecs::serial::deserialize(buff_stream_in, manager);
}
manager.view<Position>().for_each([](ecs::Entity* entity, Position* position) {
std::cout << entity->uuid << ' ' << position->x << ' ' << position->y << '\n';
});
break;
}
case PACKET::PING: {
buff_stream_out.seekp(0, std::ios::beg);
iohelper::write<uint8_t>(buff_stream_out, PACKET::PING);
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
throw std::runtime_error("Failed to connect to server");
}
break;
}
default:
std::cout << "Unknown packet type: " << (uint32_t)type << '\n';
}
// dump_buffer<buff.size()>(buff);
std::cout << "PACKET TYPE: " << type << '\n';
thread_queue.pop();
}
}
std::cout << "Disconnecting\n";
buff_stream_out.seekp(0, std::ios::beg);
iohelper::write<uint8_t>(buff_stream_out, PACKET::DISCONNECT);
if (sendto(sock, buff.data(), buff.size(), 0, to, to_size) < 0) {
throw std::runtime_error("Failed to connect to server");
}
std::cout << "Waiting for thread to join\n";
shutdown(sock, SHUT_RDWR);
receive.join();
std::cout << "Shutting down\n";
glfwTerminate();
return 0;
}

View File

@ -1,270 +0,0 @@
#include <iostream>
#include <thread>
#include <functional>
#include <array>
#include <unordered_set>
#include <sstream>
#include <netinet/in.h>
#include "ecs-serial.h"
#include "ecs_network_components.h"
#include "iohelper/memstream.h"
#include "packets.h"
#include "debug.h"
bool operator==(const sockaddr_in& lhs, const sockaddr_in& rhs) {
return lhs.sin_addr.s_addr == rhs.sin_addr.s_addr && lhs.sin_port == rhs.sin_port;
}
namespace std {
template <>
struct hash<sockaddr_in> {
size_t operator()(const sockaddr_in& from) const {
size_t addr_hash = hash<unsigned long>()(from.sin_addr.s_addr);
size_t port_hash = hash<unsigned short>()(from.sin_port);
addr_hash ^= port_hash + 0x9e3779b9 + (addr_hash << 6) + (addr_hash >> 2);
return addr_hash;
}
};
}
struct Client {
uint8_t timeout = TIMEOUT;
ecs::Entity* player = nullptr;
};
ecs::Manager manager;
std::unordered_map<sockaddr_in, Client> clients = {};
std::string get_ip(sockaddr_in from) {
uint32_t ip = from.sin_addr.s_addr;
uint32_t bytes[4];
bytes[0] = ip & 0xff;
bytes[1] = (ip >> 8) & 0xff;
bytes[2] = (ip >> 16) & 0xff;
bytes[3] = (ip >> 24) & 0xff;
std::stringstream ss;
ss << bytes[0] << '.' << bytes[1] << '.' << bytes[2] << '.' << bytes[3];
return ss.str();
}
void send_entities(int sock, sockaddr_in from) {
std::array<uint8_t, 1024> buff;
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
iohelper::write<uint8_t>(buff_stream, PACKET::ENTITIES);
iohelper::write_length(buff_stream, manager.view<>().size());
for (auto [uuid, entity] : manager.view<>()) {
ecs::serial::serialize(buff_stream, entity);
}
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
throw std::runtime_error("Failed to send ids to client");
}
}
void send_ids(int sock, sockaddr_in from) {
std::array<uint8_t, 1024> buff;
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
buff_stream.seekp(0, std::ios::beg);
iohelper::write<uint8_t>(buff_stream, PACKET::IDS);
ecs::serial::serialize_ids(buff_stream);
std::cout << "Sending ids\n";
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
throw std::runtime_error("Failed to send ids to client");
}
}
void on_connect(int sock, sockaddr_in from) {
std::cout << "Client connected: " << std::dec << get_ip(from) << ':' << from.sin_port << '\n';
ecs::Entity* player = manager.create_entity();
player->add_component<Position>(0, 0);
Client client = {TIMEOUT, player};
clients.insert({from, client});
std::array<uint8_t, 1024> buff;
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
iohelper::write<uint8_t>(buff_stream, PACKET::ACK);
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
throw std::runtime_error("Failed to send ack to client");
}
send_ids(sock, from);
send_entities(sock, from);
}
void send_ping(int sock) {
for (auto i = clients.begin(), last = clients.end(); i != last; ) {
auto& from = i->first;
auto& timeout = i->second.timeout;
timeout--;
if (timeout < TIMEOUT/2) {
std::array<uint8_t, 1024> buff;
iohelper::omemstream_fixed<buff.size()> buff_stream(buff);
iohelper::write<uint8_t>(buff_stream, PACKET::PING);
if (sendto(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, sizeof(from)) < 0) {
throw std::runtime_error("Failed to send ping to client");
}
}
if (timeout == 0) {
std::cout << "Client timed-out: " << std::dec << get_ip(from) << ':' << from.sin_port << '\n';
manager.remove_entity(i->second.player);
i = clients.erase(i);
} else {
++i;
}
}
}
// This is the loop that listens for messages from clients
void listener(int sock) {
std::array<uint8_t, 1024> buff;
iohelper::imemstream_fixed<buff.size()> buff_stream(buff);
sockaddr_in from;
socklen_t from_size = sizeof(from);
while (true) {
buff_stream.seekg(0, std::ios::beg);
int bytes_received = recvfrom(sock, buff.data(), buff.size(), 0, (sockaddr*)&from, &from_size);
if (bytes_received < 0) {
throw std::runtime_error("Failed to receive data!");
}
auto it = clients.find(from);
if (it != clients.end()) {
it->second.timeout = TIMEOUT;
} else {
std::cout << "Unknown client\n";
}
switch (iohelper::read<uint8_t>(buff_stream)) {
case PACKET::CONNECT:
on_connect(sock, from);
break;
case PACKET::DISCONNECT: {
auto it = clients.find(from);
if (it == clients.end()) {
std::cout << "Unknown client tried to disconnect\n";
break;
}
std::cout << "Client disconnected: " << std::dec << get_ip(from) << ':' << from.sin_port << '\n';
manager.remove_entity(it->second.player);
it = clients.erase(it);
break;
}
case PACKET::IDS:
send_ids(sock, from);
break;
case PACKET::ENTITIES:
send_entities(sock, from);
break;
case PACKET::EVENT: {
std::string event = iohelper::read<std::string>(buff_stream);
auto player = it->second.player;
if (!player) {
break;
}
if (!event.compare("up")) {
std::cout << "Moving up\n";
player->get_component<Position>()->x += 0.1f;
} else if (!event.compare("down")) {
std::cout << "Moving down\n";
player->get_component<Position>()->x -= 0.1f;
}
for (auto [from, timeout] : clients) {
send_entities(sock, from);
}
break;
}
// Maybe calculate the ping time here?
case PACKET::PING: {
break;
}
default:
std::cout << "Unknown packet\n";
break;
}
}
}
int main() {
/// Setup ECS
generated::init();
// for (int i = 0; i < 10; ++i) {
// // We can create entities
// ecs::Entity* entity = manager.create_entity();
// // Then we can add components to them
// entity->add_component<Position>(0.1f*i, 0.3f*i);
// }
/// Setup socket
int address_family = AF_INET;
int type = SOCK_DGRAM;
int protocol = 0;
int sock = socket(address_family, type, protocol);
if (sock < 0) {
throw std::runtime_error("Failed to create socket!");
}
sockaddr_in local_address;
local_address.sin_family = AF_INET;
local_address.sin_port = htons(9999);
local_address.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (sockaddr*)&local_address, sizeof(local_address)) < 0) {
throw std::runtime_error("Failed to bind to port!");
}
/// Start listener
std::thread listen(std::bind(listener, sock));
// @todo Add ping thread
std::cout << "Server started!\n";
/// Run server logic
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
send_ping(sock);
manager.view<Position>().for_each([](ecs::Entity* entity, Position* position) {
std::cout << entity->uuid << ' ' << position->x << ' ' << position->y << '\n';
});
}
/// Waint for threads to join
listen.join();
}

View File

@ -1,13 +0,0 @@
#pragma once
#include <array>
#include <iostream>
template<int I>
void dump_buffer(std::array<uint8_t, I> &buff) {
for (uint32_t b : buff) {
std::cout << b << ' ';
}
std::cout << '\n';
}

View File

@ -1,10 +0,0 @@
#pragma once
#include "ecs.h"
struct Position : ecs::Component {
Position(float _x, float _y) : x(_x), y(_y) {}
Position() {}
float x = 0;
float y = 0;
};

View File

@ -1,15 +0,0 @@
#pragma once
#include <cstdint>
#define TIMEOUT 20
enum PACKET : uint8_t {
CONNECT,
DISCONNECT,
PING,
ACK,
IDS,
ENTITIES,
EVENT
};

View File

@ -1,60 +0,0 @@
//========================================================================
// GLFW 3.3 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2010-2016 Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
// As glfw_config.h.in, this file is used by CMake to produce the
// glfw_config.h configuration header file. If you are adding a feature
// requiring conditional compilation, this is where to add the macro.
//========================================================================
// As glfw_config.h, this file defines compile-time option macros for a
// specific platform and development environment. If you are using the
// GLFW CMake files, modify glfw_config.h.in instead of this file. If you
// are using your own build system, make this file define the appropriate
// macros in whatever way is suitable.
//========================================================================
// Define this to 1 if building GLFW for X11
// #cmakedefine _GLFW_X11
// Define this to 1 if building GLFW for Win32
// #cmakedefine _GLFW_WIN32
// Define this to 1 if building GLFW for Cocoa
// #cmakedefine _GLFW_COCOA
// Define this to 1 if building GLFW for Wayland
// #cmakedefine _GLFW_WAYLAND
// Define this to 1 if building GLFW for OSMesa
// #cmakedefine _GLFW_OSMESA
// Define this to 1 if building as a shared library / dynamic library / DLL
// #cmakedefine _GLFW_BUILD_DLL
// Define this to 1 to use Vulkan loader linked statically into application
// #cmakedefine _GLFW_VULKAN_STATIC
// Define this to 1 to force use of high-performance GPU on hybrid systems
// #cmakedefine _GLFW_USE_HYBRID_HPG
// Define this to 1 if xkbcommon supports the compose key
// #cmakedefine HAVE_XKBCOMMON_COMPOSE_H
// Define this to 1 if the libc supports memfd_create()
// #cmakedefine HAVE_MEMFD_CREATE

View File

@ -1,73 +0,0 @@
//========================================================================
// GLFW 3.3 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
// As mappings.h.in, this file is used by CMake to produce the mappings.h
// header file. If you are adding a GLFW specific gamepad mapping, this is
// where to put it.
//========================================================================
// As mappings.h, this provides all pre-defined gamepad mappings, including
// all available in SDL_GameControllerDB. Do not edit this file. Any gamepad
// mappings not specific to GLFW should be submitted to SDL_GameControllerDB.
// This file can be re-generated from mappings.h.in and the upstream
// gamecontrollerdb.txt with the GenerateMappings.cmake script.
//========================================================================
// All gamepad mappings not labeled GLFW are copied from the
// SDL_GameControllerDB project under the following license:
//
// Simple DirectMedia Layer
// Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the
// use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
const char* _glfwDefaultMappings[] =
{
@GLFW_GAMEPAD_MAPPINGS@
"78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
"78696e70757402000000000000000000,XInput Wheel (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
"78696e70757403000000000000000000,XInput Arcade Stick (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
"78696e70757404000000000000000000,XInput Flight Stick (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
"78696e70757405000000000000000000,XInput Dance Pad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
"78696e70757406000000000000000000,XInput Guitar (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
"78696e70757408000000000000000000,XInput Drum Kit (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,",
NULL
};

View File

@ -1,3 +0,0 @@
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

1
vendor/lua vendored

@ -1 +0,0 @@
Subproject commit e354c6355e7f48e087678ec49e340ca0696725b1

1
vendor/sol2 vendored

@ -1 +0,0 @@
Subproject commit 8643dec9e5f548c719289adccf66045c9371404e

1
vendor/stduuid vendored

@ -1 +0,0 @@
Subproject commit dee14f660da4d2889e60848b206c1d7b8d1fba99