Jessica James
8 years ago
80 changed files with 570 additions and 76 deletions
@ -1 +1 @@ |
|||
Subproject commit 0963bf950533632a26f266f2f8575dc4344f22d8 |
|||
Subproject commit ae3294c72b799178bc79b312fe43f34c28deca86 |
@ -0,0 +1,86 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<ItemGroup Label="ProjectConfigurations"> |
|||
<ProjectConfiguration Include="Debug|Win32"> |
|||
<Configuration>Debug</Configuration> |
|||
<Platform>Win32</Platform> |
|||
</ProjectConfiguration> |
|||
<ProjectConfiguration Include="Release|Win32"> |
|||
<Configuration>Release</Configuration> |
|||
<Platform>Win32</Platform> |
|||
</ProjectConfiguration> |
|||
</ItemGroup> |
|||
<PropertyGroup Label="Globals"> |
|||
<ProjectGuid>{29C995BB-6A23-4704-BF8D-EA177D5FC7A5}</ProjectGuid> |
|||
<RootNamespace>RenX.Net</RootNamespace> |
|||
</PropertyGroup> |
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> |
|||
<ConfigurationType>Application</ConfigurationType> |
|||
<UseDebugLibraries>true</UseDebugLibraries> |
|||
<PlatformToolset>v140</PlatformToolset> |
|||
<CharacterSet>MultiByte</CharacterSet> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> |
|||
<ConfigurationType>DynamicLibrary</ConfigurationType> |
|||
<UseDebugLibraries>false</UseDebugLibraries> |
|||
<PlatformToolset>v140</PlatformToolset> |
|||
<WholeProgramOptimization>true</WholeProgramOptimization> |
|||
<CharacterSet>Unicode</CharacterSet> |
|||
</PropertyGroup> |
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> |
|||
<ImportGroup Label="ExtensionSettings"> |
|||
</ImportGroup> |
|||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
|||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
|||
</ImportGroup> |
|||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
|||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
|||
</ImportGroup> |
|||
<PropertyGroup Label="UserMacros" /> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
|||
<OutDir>$(SolutionDir)$(Configuration)\Plugins\</OutDir> |
|||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> |
|||
</PropertyGroup> |
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
|||
<ClCompile> |
|||
<WarningLevel>Level3</WarningLevel> |
|||
<Optimization>Disabled</Optimization> |
|||
<SDLCheck>true</SDLCheck> |
|||
</ClCompile> |
|||
<Link> |
|||
<GenerateDebugInformation>true</GenerateDebugInformation> |
|||
</Link> |
|||
</ItemDefinitionGroup> |
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
|||
<ClCompile> |
|||
<WarningLevel>Level3</WarningLevel> |
|||
<Optimization>MaxSpeed</Optimization> |
|||
<FunctionLevelLinking>true</FunctionLevelLinking> |
|||
<IntrinsicFunctions>true</IntrinsicFunctions> |
|||
<SDLCheck>true</SDLCheck> |
|||
<AdditionalIncludeDirectories>../Bot;../Jupiter;../RenX.Core;../HTTPServer</AdditionalIncludeDirectories> |
|||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
|||
</ClCompile> |
|||
<Link> |
|||
<GenerateDebugInformation>true</GenerateDebugInformation> |
|||
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
|||
<OptimizeReferences>true</OptimizeReferences> |
|||
</Link> |
|||
</ItemDefinitionGroup> |
|||
<ItemGroup> |
|||
<ClInclude Include="RenX_Net.h" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ClCompile Include="RenX_Net.cpp" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Library Include="..\Release\Jupiter.lib" /> |
|||
<Library Include="..\Release\Bot.lib" /> |
|||
<Library Include="..\Release\Plugins\HTTPServer.lib" /> |
|||
<Library Include="..\Release\Plugins\RenX.Core.lib" /> |
|||
</ItemGroup> |
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> |
|||
<ImportGroup Label="ExtensionTargets"> |
|||
</ImportGroup> |
|||
</Project> |
@ -0,0 +1,41 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<ItemGroup> |
|||
<Filter Include="Source Files"> |
|||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> |
|||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> |
|||
</Filter> |
|||
<Filter Include="Header Files"> |
|||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> |
|||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> |
|||
</Filter> |
|||
<Filter Include="Resource Files"> |
|||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> |
|||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> |
|||
</Filter> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ClInclude Include="RenX_Net.h"> |
|||
<Filter>Header Files</Filter> |
|||
</ClInclude> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ClCompile Include="RenX_Net.cpp"> |
|||
<Filter>Source Files</Filter> |
|||
</ClCompile> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Library Include="..\Release\Plugins\RenX.Core.lib"> |
|||
<Filter>Resource Files</Filter> |
|||
</Library> |
|||
<Library Include="..\Release\Bot.lib"> |
|||
<Filter>Resource Files</Filter> |
|||
</Library> |
|||
<Library Include="..\Release\Jupiter.lib"> |
|||
<Filter>Resource Files</Filter> |
|||
</Library> |
|||
<Library Include="..\Release\Plugins\HTTPServer.lib"> |
|||
<Filter>Resource Files</Filter> |
|||
</Library> |
|||
</ItemGroup> |
|||
</Project> |
@ -0,0 +1,274 @@ |
|||
/**
|
|||
* Copyright (C) 2016 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#include "RenX_Net.h" |
|||
|
|||
using namespace Jupiter::literals; |
|||
|
|||
constexpr ProductID RENEGADE_X = 0x01; |
|||
|
|||
constexpr RequestID REQUEST_SERVER_LIST = 0x01; // version_number
|
|||
constexpr RequestID REQUEST_SERVER_LIST_UPDATE = 0x02; // version_number, revision_id
|
|||
constexpr RequestID REQUEST_SERVER_DATA = 0x03; // server_id
|
|||
|
|||
constexpr int REQUEST_SIZE_TABLE[] = |
|||
{ |
|||
-1, // Invalid request
|
|||
sizeof(int32_t), // REQUEST_SERVER_LIST
|
|||
sizeof(int32_t) + sizeof(int32_t) // REQUEST_SERVER_LIST_UPDATE
|
|||
// sizeof (int32_t) // REQUEST_SERVER_DATA
|
|||
}; |
|||
|
|||
constexpr size_t minimum_request_size = REQUEST_SIZE_TABLE[REQUEST_SERVER_LIST]; // Size of smallest request
|
|||
constexpr size_t maximum_request_size = REQUEST_SIZE_TABLE[REQUEST_SERVER_LIST_UPDATE]; // Size of largest request
|
|||
|
|||
// Size of packet header
|
|||
constexpr size_t packet_header_size = sizeof(ProductID) + sizeof(RequestID); |
|||
|
|||
constexpr int getRequestSize(RequestID in_request_id) |
|||
{ |
|||
return in_request_id >= sizeof(REQUEST_SIZE_TABLE) / sizeof(int) |
|||
? -1 // Invalid request
|
|||
: REQUEST_SIZE_TABLE[in_request_id]; |
|||
} |
|||
|
|||
/** Plugin initialization and destruction */ |
|||
|
|||
bool RenX_NetPlugin::initialize() //(const Jupiter::CStringType &bind_address, uint16_t bind_port, bool &out_success, std::chrono::milliseconds in_timeout_period)
|
|||
{ |
|||
m_timeout_period = std::chrono::milliseconds(this->config.getLongLong(Jupiter::ReferenceString::empty, "TimeoutPeriod"_jrs, 10000)); |
|||
|
|||
if (m_server_socket.bind(Jupiter::CStringS(this->config.get(Jupiter::ReferenceString::empty, "Address"_jrs)).c_str(), this->config.getInt(Jupiter::ReferenceString::empty, "Port"_jrs, 21338), true) == false) |
|||
return false; |
|||
|
|||
m_server_socket.setBlocking(false); |
|||
return true; |
|||
} |
|||
|
|||
RenX_NetPlugin::~RenX_NetPlugin() |
|||
{ |
|||
m_server_socket.close(); |
|||
} |
|||
|
|||
int RenX_NetPlugin::OnRehash() |
|||
{ |
|||
Jupiter::Plugin::OnRehash(); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
/** Server List functions */ |
|||
|
|||
void RenX_NetPlugin::assignServerIDs() |
|||
{ |
|||
} |
|||
|
|||
void RenX_NetPlugin::addServerToServerList(RenX::Server *server) |
|||
{ |
|||
} |
|||
|
|||
void RenX_NetPlugin::updateServerList() |
|||
{ |
|||
} |
|||
|
|||
/** Client processing */ |
|||
|
|||
void RenX_NetPlugin::process_request(RenX_NetPlugin::Client &client) |
|||
{ |
|||
size_t index; |
|||
const uint32_t *request_params; |
|||
ServerList *list; |
|||
|
|||
request_params = reinterpret_cast<const uint32_t *>(client.m_request_buffer.ptr()); |
|||
|
|||
switch (client.m_request_id) |
|||
{ |
|||
case REQUEST_SERVER_LIST: |
|||
index = 0; |
|||
if (m_server_list.size() != 0) |
|||
{ |
|||
while (index != m_server_list.size()) |
|||
{ |
|||
list = m_server_list.get(index); |
|||
if (list->version_number == *request_params) |
|||
{ |
|||
client.sock.send(list->data); |
|||
break; |
|||
} |
|||
|
|||
if (++index == m_server_list.size()) |
|||
{ |
|||
// Version not found
|
|||
break; |
|||
} |
|||
} |
|||
} |
|||
// else // m_server_list not initialized correctly (misconfiguration)
|
|||
break; |
|||
|
|||
default: // Invalid request. Reset timeout to destroy on next loop.
|
|||
client.timeout = std::chrono::steady_clock::now(); |
|||
break; |
|||
} |
|||
|
|||
client.sock.shutdown(); |
|||
} |
|||
|
|||
int RenX_NetPlugin::think() |
|||
{ |
|||
RenX_NetPlugin::Client *client; |
|||
Jupiter::Socket *sock; |
|||
int request_packet_size; |
|||
Jupiter::DLList<RenX_NetPlugin::Client>::Node *node; |
|||
Jupiter::DLList<RenX_NetPlugin::Client>::Node *tmp; |
|||
|
|||
// handle new clients
|
|||
while ((sock = m_server_socket.accept()) != nullptr) |
|||
{ |
|||
sock->setBlocking(false); |
|||
request_packet_size = sock->peek(); |
|||
|
|||
if (request_packet_size > maximum_request_packet_size // This is larger than expected (invalid) request. Toss the socket.
|
|||
|| (request_packet_size <= 0 && sock->getLastError() != 10035)) // Error!
|
|||
delete sock; |
|||
else |
|||
{ |
|||
client = new RenX_NetPlugin::Client(); |
|||
client->sock = std::move(*sock); |
|||
client->timeout = std::chrono::steady_clock::now() + m_timeout_period; |
|||
|
|||
if (request_packet_size == expected_request_packet_size) // we have a full request
|
|||
{ |
|||
process_request(*client); |
|||
|
|||
m_clients_pending_close.add(client); |
|||
} |
|||
else // request is not complete; toss it to further processing
|
|||
m_clients_processing.add(client); |
|||
} |
|||
} |
|||
|
|||
// check processing clients for remainder of data; timeout as necessary
|
|||
if (m_clients_processing.size() != 0) |
|||
{ |
|||
node = m_clients_processing.getNode(0); |
|||
while (node != nullptr) |
|||
{ |
|||
client = node->data; |
|||
request_packet_size = client->sock.peek(); |
|||
|
|||
if (request_packet_size == expected_request_packet_size) // Full request
|
|||
{ |
|||
process_request(*client); |
|||
|
|||
tmp = node; |
|||
node = node->next; |
|||
|
|||
m_clients_processing.remove(tmp); |
|||
|
|||
client->timeout = std::chrono::steady_clock::now() + m_timeout_period; |
|||
m_clients_pending_close.add(client); |
|||
} |
|||
else if (request_packet_size > expected_request_packet_size // Invalid request
|
|||
|| client->timeout <= std::chrono::steady_clock::now() // Incomplete request & timed out
|
|||
|| (request_packet_size <= 0 && client->sock.getLastError() != 10035)) // Error!
|
|||
{ |
|||
client->sock.close(); |
|||
|
|||
tmp = node; |
|||
node = node->next; |
|||
|
|||
delete m_clients_processing.remove(tmp); |
|||
} |
|||
else // Incomplete request
|
|||
node = node->next; |
|||
} |
|||
} |
|||
|
|||
// check old clients for clean close; timeout as necessary
|
|||
if (m_clients_pending_close.size() != 0) |
|||
{ |
|||
node = m_clients_pending_close.getNode(0); |
|||
while (node != nullptr) |
|||
{ |
|||
client = node->data; |
|||
request_packet_size = client->sock.recv(); |
|||
|
|||
if (request_packet_size == 0 // Shutdown successfully
|
|||
|| (request_packet_size < 0 && client->sock.getLastError() != 10035) // Error!
|
|||
|| client->timeout <= std::chrono::steady_clock::now()) // Timeout
|
|||
{ |
|||
client->sock.close(); |
|||
|
|||
tmp = node; |
|||
node = node->next; |
|||
|
|||
delete m_clients_pending_close.remove(tmp); |
|||
} |
|||
else // Still pending; go to next
|
|||
node = node->next; |
|||
} |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
/** Events */ |
|||
|
|||
void RenX_NetPlugin::RenX_OnServerFullyConnected(RenX::Server *server) |
|||
{ |
|||
if (++m_server_id == 0) // Overflow; this will actually invalidate the results of any previous server list
|
|||
this->assignServerIDs(); |
|||
else |
|||
server->varData.set(this->getName(), "i"_jrs, Jupiter::ReferenceString(reinterpret_cast<const char *>(&m_server_id), sizeof(m_server_id))); |
|||
|
|||
// Server added; append
|
|||
this->addServerToServerList(server); |
|||
} |
|||
|
|||
void RenX_NetPlugin::RenX_OnServerDisconnect(RenX::Server *server, RenX::DisconnectReason reason) |
|||
{ |
|||
// Server removed; update
|
|||
this->updateServerList(); |
|||
} |
|||
|
|||
void RenX_NetPlugin::RenX_OnJoin(RenX::Server *server, const RenX::PlayerInfo *player) |
|||
{ |
|||
// User count changed; update
|
|||
this->updateServerList(); |
|||
} |
|||
|
|||
void RenX_NetPlugin::RenX_OnPart(RenX::Server *server, const RenX::PlayerInfo *player) |
|||
{ |
|||
// User count changed; update
|
|||
this->updateServerList(); |
|||
} |
|||
|
|||
void RenX_NetPlugin::RenX_OnMapLoad(RenX::Server *server, const Jupiter::ReadableString &map) |
|||
{ |
|||
// Level name changed; update
|
|||
this->updateServerList(); |
|||
} |
|||
|
|||
// Plugin instantiation and entry point.
|
|||
RenX_NetPlugin pluginInstance; |
|||
|
|||
extern "C" __declspec(dllexport) Jupiter::Plugin *getPlugin() |
|||
{ |
|||
return &pluginInstance; |
|||
} |
@ -0,0 +1,93 @@ |
|||
/**
|
|||
* Copyright (C) 2016 Jessica James. |
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
|||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
* |
|||
* Written by Jessica James <jessica.aj@outlook.com> |
|||
*/ |
|||
|
|||
#if !defined _RENX_NET_GAME_H_HEADER |
|||
#define _RENX_NET_GAME_H_HEADER |
|||
|
|||
#include <chrono> |
|||
#include "Jupiter/Thinker.h" |
|||
#include "Jupiter/CString.h" |
|||
#include "Jupiter/TCPSocket.h" |
|||
#include "Jupiter/DLList.h" |
|||
#include "Jupiter/ArrayList.h" |
|||
#include "RenX_Plugin.h" |
|||
#include "RenX_Server.h" |
|||
|
|||
typedef uint8_t ProductID; |
|||
typedef uint8_t RequestID; |
|||
typedef uint32_t ServerID; |
|||
|
|||
class RenX_NetPlugin : public RenX::Plugin |
|||
{ |
|||
public: |
|||
|
|||
struct Client |
|||
{ |
|||
Jupiter::Socket sock; |
|||
std::chrono::steady_clock::time_point timeout; |
|||
|
|||
ProductID m_product_id; |
|||
RequestID m_request_id; |
|||
Jupiter::String m_request_buffer; |
|||
}; |
|||
|
|||
struct ServerList |
|||
{ |
|||
uint32_t version_number = 0; |
|||
uint32_t revision_id = 0; |
|||
|
|||
Jupiter::String data; |
|||
Jupiter::ArrayList<RenX::Server> servers; |
|||
}; |
|||
|
|||
void assignServerIDs(); |
|||
void addServerToServerList(RenX::Server *server); |
|||
void updateServerList(); |
|||
~RenX_NetPlugin(); |
|||
|
|||
public: // Jupiter::Plugin
|
|||
virtual bool initialize() override; |
|||
int OnRehash() override; |
|||
int think() override; |
|||
|
|||
public: // RenX::Plugin
|
|||
void RenX_OnServerFullyConnected(RenX::Server *server) override; |
|||
void RenX_OnServerDisconnect(RenX::Server *server, RenX::DisconnectReason reason) override; |
|||
void RenX_OnJoin(RenX::Server *server, const RenX::PlayerInfo *player) override; |
|||
void RenX_OnPart(RenX::Server *server, const RenX::PlayerInfo *player) override; |
|||
void RenX_OnMapLoad(RenX::Server *server, const Jupiter::ReadableString &map) override; |
|||
|
|||
private: |
|||
void process_request(Client &client); |
|||
|
|||
std::chrono::milliseconds m_ping_frequency; |
|||
std::chrono::milliseconds m_ping_timeout; |
|||
|
|||
Jupiter::TCPSocket m_server_socket; |
|||
Jupiter::DLList<Client> m_clients; |
|||
Jupiter::DLList<Client> m_clients_pending_close; |
|||
|
|||
uint32_t m_server_id; |
|||
|
|||
uint32_t m_min_server_version; |
|||
uint32_t m_max_server_version; |
|||
|
|||
Jupiter::ArrayList<ServerList> m_server_list; |
|||
}; |
|||
|
|||
#endif // _RENX_SERVERLIST_GAME_H_HEADER
|
Loading…
Reference in new issue