Skip to content

Commit

Permalink
Client prototyping #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mvccogo committed Jan 19, 2022
1 parent 74c3d8e commit 6a4bb38
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 17 deletions.
28 changes: 16 additions & 12 deletions GNL-Enet/Client/Client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,39 @@
<LinkIncremental>true</LinkIncremental>
<PublicIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(PublicIncludeDirectories)</PublicIncludeDirectories>
<AllProjectIncludesArePublic>true</AllProjectIncludesArePublic>
<IncludePath>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<PublicIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(PublicIncludeDirectories)</PublicIncludeDirectories>
<AllProjectIncludesArePublic>true</AllProjectIncludesArePublic>
<IncludePath>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<PublicIncludeDirectories>$(PublicIncludeDirectories)</PublicIncludeDirectories>
<AllProjectIncludesArePublic>true</AllProjectIncludesArePublic>
<IncludePath>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<PublicIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(PublicIncludeDirectories)</PublicIncludeDirectories>
<AllProjectIncludesArePublic>true</AllProjectIncludesArePublic>
<IncludePath>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(ProjectDir)..\..\enet\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;$(ProjectDir)..\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;enet.lib;ws2_32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -116,14 +117,15 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(ProjectDir)..\..\enet\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;$(ProjectDir)..\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;enet.lib;ws2_32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -132,12 +134,13 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(ProjectDir)..\..\enet\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;$(ProjectDir)..\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;enet.lib;ws2_32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -148,14 +151,15 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\godot-cpp\include;$(ProjectDir)..\..\godot-cpp\include\core;$(ProjectDir)..\..\godot-cpp\include\gen;$(ProjectDir)..\..\godot-cpp\godot-headers;$(ProjectDir)..\..\enet\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\godot-cpp\bin;$(ProjectDir)..\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;enet.lib;ws2_32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
47 changes: 45 additions & 2 deletions GNL-Enet/Client/src/GDNative.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "GDNative.h"
#include "Client.h"

#include <enet/enet.h>

const uint16_t MAX_LEN = 1024 * 5;
using namespace godot;
Expand All @@ -18,13 +18,56 @@ NetNode::NetNode() {

NetNode::~NetNode() {

enet_host_destroy(enet_host);
}

void NetNode::_init() {

enet_host = enet_host_create(NULL, 1, 2, 0, 0);
if (!enet_host) {
godot::Godot::print("An error occurred while trying to create an ENet client host.");
}

}

void NetNode::_process(float delta) {
ENetEvent event;
while (enet_host_service(enet_host, &event, 0) > 0)
{
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
godot::Godot::print("Received packet from host...\n");

emit_signal("packet_received", static_cast<int> (m_pktIn.header.cmdID))


}



}


}

void NetNode::Connect(String host, uint16_t port) {
ENetAddress addr;
ENetEvent event;
ENetPeer* peer;
enet_address_set_host(&addr, host.alloc_c_string());
addr.port = port;

peer = enet_host_connect(enet_host, &addr, 2, 0);

if (!peer) {
godot::Godot::print("No available peers for initiating an ENet connection.\n");
}

if (enet_host_service(enet_host, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) {
godot::Godot::print("Successfully connected over ENet.\n");
}
else {
enet_peer_reset(peer);
godot::Godot::print("Failed to connect over ENet.\n");
}

}
6 changes: 4 additions & 2 deletions GNL-Enet/Client/src/GDNative.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <core/Ref.hpp>
#include "Client.h"
#include <gen/Object.hpp>

#include <enet/enet.h>

namespace godot {
class NetNode : public Node {
Expand All @@ -23,7 +23,9 @@ namespace godot {
void _init();
void _process(float delta);

void Connect(String host, uint16_t port);

private:

ENetHost* enet_host;
};
}
21 changes: 20 additions & 1 deletion GNL-Enet/Client/src/MainTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@

#include <enet/enet.h>
#include <cstdio>

int main() {
if (enet_initialize() != 0)
{

(stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
ENetHost* client;

client = enet_host_create(NULL, 4, 2, 0, 0);
if (client == NULL)
{
fprintf(stderr,
"An error occurred while trying to create an ENet server host.\n");
exit(EXIT_FAILURE);
}

enet_host_destroy(client);
atexit(enet_deinitialize);

return 0;
}
18 changes: 18 additions & 0 deletions GNL-Enet/Client/src/Packet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <stdint.h>

class Packet {
public:
Packet();
~Packet();

uint32_t get_packet_len() const {return static_cast<uint32_t>(body.size()) }


private:

uint16_t cmdID;
uint32_t size;


};

0 comments on commit 6a4bb38

Please sign in to comment.