mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 11:03:00 +01:00
Merge pull request #3766 from IntelOrca/refactor/network
Refactor network
This commit is contained in:
@@ -7,6 +7,12 @@
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
007A05CD1CFB2C8B00F419C3 /* NetworkAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A05C01CFB2C8B00F419C3 /* NetworkAction.cpp */; };
|
||||
007A05CE1CFB2C8B00F419C3 /* NetworkAddress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A05C21CFB2C8B00F419C3 /* NetworkAddress.cpp */; };
|
||||
007A05CF1CFB2C8B00F419C3 /* NetworkConnection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A05C41CFB2C8B00F419C3 /* NetworkConnection.cpp */; };
|
||||
007A05D01CFB2C8B00F419C3 /* NetworkGroup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A05C61CFB2C8B00F419C3 /* NetworkGroup.cpp */; };
|
||||
007A05D11CFB2C8B00F419C3 /* NetworkPacket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A05C81CFB2C8B00F419C3 /* NetworkPacket.cpp */; };
|
||||
007A05D21CFB2C8B00F419C3 /* NetworkPlayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A05CA1CFB2C8B00F419C3 /* NetworkPlayer.cpp */; };
|
||||
008BF72A1CDAA5C30019A2AD /* track_design_index.c in Sources */ = {isa = PBXBuildFile; fileRef = 008BF7261CDAA5C30019A2AD /* track_design_index.c */; };
|
||||
008BF72B1CDAA5C30019A2AD /* track_design_save.c in Sources */ = {isa = PBXBuildFile; fileRef = 008BF7271CDAA5C30019A2AD /* track_design_save.c */; };
|
||||
008BF72C1CDAA5C30019A2AD /* track_design.c in Sources */ = {isa = PBXBuildFile; fileRef = 008BF7281CDAA5C30019A2AD /* track_design.c */; };
|
||||
@@ -331,6 +337,19 @@
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
007A05C01CFB2C8B00F419C3 /* NetworkAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkAction.cpp; sourceTree = "<group>"; };
|
||||
007A05C11CFB2C8B00F419C3 /* NetworkAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkAction.h; sourceTree = "<group>"; };
|
||||
007A05C21CFB2C8B00F419C3 /* NetworkAddress.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkAddress.cpp; sourceTree = "<group>"; };
|
||||
007A05C31CFB2C8B00F419C3 /* NetworkAddress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkAddress.h; sourceTree = "<group>"; };
|
||||
007A05C41CFB2C8B00F419C3 /* NetworkConnection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkConnection.cpp; sourceTree = "<group>"; };
|
||||
007A05C51CFB2C8B00F419C3 /* NetworkConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkConnection.h; sourceTree = "<group>"; };
|
||||
007A05C61CFB2C8B00F419C3 /* NetworkGroup.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkGroup.cpp; sourceTree = "<group>"; };
|
||||
007A05C71CFB2C8B00F419C3 /* NetworkGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkGroup.h; sourceTree = "<group>"; };
|
||||
007A05C81CFB2C8B00F419C3 /* NetworkPacket.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkPacket.cpp; sourceTree = "<group>"; };
|
||||
007A05C91CFB2C8B00F419C3 /* NetworkPacket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkPacket.h; sourceTree = "<group>"; };
|
||||
007A05CA1CFB2C8B00F419C3 /* NetworkPlayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkPlayer.cpp; sourceTree = "<group>"; };
|
||||
007A05CB1CFB2C8B00F419C3 /* NetworkPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkPlayer.h; sourceTree = "<group>"; };
|
||||
007A05CC1CFB2C8B00F419C3 /* NetworkTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkTypes.h; sourceTree = "<group>"; };
|
||||
008BF7261CDAA5C30019A2AD /* track_design_index.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = track_design_index.c; sourceTree = "<group>"; };
|
||||
008BF7271CDAA5C30019A2AD /* track_design_save.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = track_design_save.c; sourceTree = "<group>"; };
|
||||
008BF7281CDAA5C30019A2AD /* track_design.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = track_design.c; sourceTree = "<group>"; };
|
||||
@@ -1326,6 +1345,19 @@
|
||||
D442714F1CC81B3200D84D28 /* network */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
007A05C01CFB2C8B00F419C3 /* NetworkAction.cpp */,
|
||||
007A05C11CFB2C8B00F419C3 /* NetworkAction.h */,
|
||||
007A05C21CFB2C8B00F419C3 /* NetworkAddress.cpp */,
|
||||
007A05C31CFB2C8B00F419C3 /* NetworkAddress.h */,
|
||||
007A05C41CFB2C8B00F419C3 /* NetworkConnection.cpp */,
|
||||
007A05C51CFB2C8B00F419C3 /* NetworkConnection.h */,
|
||||
007A05C61CFB2C8B00F419C3 /* NetworkGroup.cpp */,
|
||||
007A05C71CFB2C8B00F419C3 /* NetworkGroup.h */,
|
||||
007A05C81CFB2C8B00F419C3 /* NetworkPacket.cpp */,
|
||||
007A05C91CFB2C8B00F419C3 /* NetworkPacket.h */,
|
||||
007A05CA1CFB2C8B00F419C3 /* NetworkPlayer.cpp */,
|
||||
007A05CB1CFB2C8B00F419C3 /* NetworkPlayer.h */,
|
||||
007A05CC1CFB2C8B00F419C3 /* NetworkTypes.h */,
|
||||
D44271501CC81B3200D84D28 /* http.cpp */,
|
||||
D44271511CC81B3200D84D28 /* http.h */,
|
||||
D44271521CC81B3200D84D28 /* network.cpp */,
|
||||
@@ -1960,12 +1992,15 @@
|
||||
C686F91D1CDBC3B7009F9BFC /* multi_dimension_roller_coaster.c in Sources */,
|
||||
C686F8B31CDBC37E009F9BFC /* surface.c in Sources */,
|
||||
D442724E1CC81B3200D84D28 /* scenario_sources.c in Sources */,
|
||||
007A05CE1CFB2C8B00F419C3 /* NetworkAddress.cpp in Sources */,
|
||||
D442729A1CC81B3200D84D28 /* banner.c in Sources */,
|
||||
C650B21C1CCABC4400B4D91C /* ConvertCommand.cpp in Sources */,
|
||||
D44272211CC81B3200D84D28 /* viewport_interaction.c in Sources */,
|
||||
D442721B1CC81B3200D84D28 /* graph.c in Sources */,
|
||||
C686F9581CDBC4C7009F9BFC /* vehicle_paint.c in Sources */,
|
||||
007A05D11CFB2C8B00F419C3 /* NetworkPacket.cpp in Sources */,
|
||||
D44272101CC81B3200D84D28 /* sprite.c in Sources */,
|
||||
007A05CD1CFB2C8B00F419C3 /* NetworkAction.cpp in Sources */,
|
||||
D442721F1CC81B3200D84D28 /* title_sequences.c in Sources */,
|
||||
C686F8AE1CDBC37E009F9BFC /* fence.c in Sources */,
|
||||
C686F8AC1CDBC37E009F9BFC /* banner.c in Sources */,
|
||||
@@ -1982,6 +2017,7 @@
|
||||
D442723F1CC81B3200D84D28 /* shared.c in Sources */,
|
||||
D442727E1CC81B3200D84D28 /* save_prompt.c in Sources */,
|
||||
D442726A1CC81B3200D84D28 /* main.c in Sources */,
|
||||
007A05CF1CFB2C8B00F419C3 /* NetworkConnection.cpp in Sources */,
|
||||
C686F9341CDBC3B7009F9BFC /* merry_go_round.c in Sources */,
|
||||
C686F8B81CDBC37E009F9BFC /* sprite.c in Sources */,
|
||||
C686F9221CDBC3B7009F9BFC /* stand_up_roller_coaster.c in Sources */,
|
||||
@@ -2113,6 +2149,7 @@
|
||||
D442729B1CC81B3200D84D28 /* climate.c in Sources */,
|
||||
C686F9261CDBC3B7009F9BFC /* vertical_drop_roller_coaster.c in Sources */,
|
||||
D442726F1CC81B3200D84D28 /* multiplayer.c in Sources */,
|
||||
007A05D21CFB2C8B00F419C3 /* NetworkPlayer.cpp in Sources */,
|
||||
D442725B1CC81B3200D84D28 /* editor_inventions_list.c in Sources */,
|
||||
D44272311CC81B3200D84D28 /* news_item.c in Sources */,
|
||||
D442720D1CC81B3200D84D28 /* rain.c in Sources */,
|
||||
@@ -2143,6 +2180,7 @@
|
||||
D442720F1CC81B3200D84D28 /* scrolling_text.c in Sources */,
|
||||
D44271F51CC81B3200D84D28 /* addresses.c in Sources */,
|
||||
D44272041CC81B3200D84D28 /* Stopwatch.cpp in Sources */,
|
||||
007A05D01CFB2C8B00F419C3 /* NetworkGroup.cpp in Sources */,
|
||||
C686F9491CDBC3B7009F9BFC /* chairlift.c in Sources */,
|
||||
C686F9501CDBC3B7009F9BFC /* log_flume.c in Sources */,
|
||||
D44272241CC81B3200D84D28 /* intro.c in Sources */,
|
||||
|
||||
@@ -83,7 +83,13 @@
|
||||
<ClCompile Include="src\management\research.c" />
|
||||
<ClCompile Include="src\network\http.cpp" />
|
||||
<ClCompile Include="src\network\network.cpp" />
|
||||
<ClCompile Include="src\network\NetworkAction.cpp" />
|
||||
<ClCompile Include="src\network\NetworkAddress.cpp" />
|
||||
<ClCompile Include="src\network\NetworkConnection.cpp" />
|
||||
<ClCompile Include="src\network\NetworkGroup.cpp" />
|
||||
<ClCompile Include="src\network\NetworkKey.cpp" />
|
||||
<ClCompile Include="src\network\NetworkPacket.cpp" />
|
||||
<ClCompile Include="src\network\NetworkPlayer.cpp" />
|
||||
<ClCompile Include="src\network\NetworkUser.cpp" />
|
||||
<ClCompile Include="src\network\twitch.cpp" />
|
||||
<ClCompile Include="src\object.c" />
|
||||
@@ -358,6 +364,13 @@
|
||||
<ClInclude Include="src\management\news_item.h" />
|
||||
<ClInclude Include="src\management\research.h" />
|
||||
<ClInclude Include="src\network\http.h" />
|
||||
<ClInclude Include="src\network\NetworkAction.h" />
|
||||
<ClInclude Include="src\network\NetworkAddress.h" />
|
||||
<ClInclude Include="src\network\NetworkConnection.h" />
|
||||
<ClInclude Include="src\network\NetworkGroup.h" />
|
||||
<ClInclude Include="src\network\NetworkPacket.h" />
|
||||
<ClInclude Include="src\network\NetworkPlayer.h" />
|
||||
<ClInclude Include="src\network\NetworkTypes.h" />
|
||||
<ClInclude Include="src\network\NetworkUser.h" />
|
||||
<ClInclude Include="src\network\twitch.h" />
|
||||
<ClInclude Include="src\network\network.h" />
|
||||
|
||||
20
src/common.h
20
src/common.h
@@ -35,16 +35,18 @@
|
||||
#endif
|
||||
|
||||
#ifdef PLATFORM_X86
|
||||
#ifdef __GNUC__
|
||||
#define FASTCALL __attribute__((fastcall))
|
||||
#elif _MSC_VER
|
||||
#define FASTCALL __fastcall
|
||||
#else
|
||||
#pragma message "Not using fastcall calling convention, please check your compiler support"
|
||||
#define FASTCALL
|
||||
#endif
|
||||
#ifndef FASTCALL
|
||||
#ifdef __GNUC__
|
||||
#define FASTCALL __attribute__((fastcall))
|
||||
#elif _MSC_VER
|
||||
#define FASTCALL __fastcall
|
||||
#else
|
||||
#pragma message "Not using fastcall calling convention, please check your compiler support"
|
||||
#define FASTCALL
|
||||
#endif
|
||||
#endif // FASTCALL
|
||||
#else // PLATFORM_X86
|
||||
#define FASTCALL
|
||||
#define FASTCALL
|
||||
#endif // PLATFORM_X86
|
||||
|
||||
#endif
|
||||
|
||||
224
src/network/NetworkAction.cpp
Normal file
224
src/network/NetworkAction.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include <algorithm>
|
||||
#include "NetworkAction.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../game.h"
|
||||
#include "../localisation/string_ids.h"
|
||||
}
|
||||
|
||||
int NetworkActions::FindCommand(int command)
|
||||
{
|
||||
auto it = std::find_if(Actions.begin(), Actions.end(), [&command](NetworkAction const &action)
|
||||
{
|
||||
for (auto it = action.Commands.begin(); it != action.Commands.end(); it++)
|
||||
{
|
||||
if ((*it) == command)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (it != Actions.end())
|
||||
{
|
||||
return it - Actions.begin();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int NetworkActions::FindCommandByPermissionName(const std::string &permission_name)
|
||||
{
|
||||
auto it = std::find_if(Actions.begin(), Actions.end(), [&permission_name](NetworkAction const &action)
|
||||
{
|
||||
if (action.PermissionName == permission_name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (it != Actions.end())
|
||||
{
|
||||
return it - Actions.begin();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
const std::vector<NetworkAction> NetworkActions::Actions =
|
||||
{
|
||||
{
|
||||
STR_ACTION_CHAT, "PERMISSION_CHAT",
|
||||
{
|
||||
-1
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_TERRAFORM, "PERMISSION_TERRAFORM",
|
||||
{
|
||||
GAME_COMMAND_SET_LAND_HEIGHT,
|
||||
GAME_COMMAND_RAISE_LAND,
|
||||
GAME_COMMAND_LOWER_LAND,
|
||||
GAME_COMMAND_EDIT_LAND_SMOOTH,
|
||||
GAME_COMMAND_CHANGE_SURFACE_STYLE
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_SET_WATER_LEVEL, "PERMISSION_SET_WATER_LEVEL",
|
||||
{
|
||||
GAME_COMMAND_SET_WATER_HEIGHT,
|
||||
GAME_COMMAND_RAISE_WATER,
|
||||
GAME_COMMAND_LOWER_WATER
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_TOGGLE_PAUSE, "PERMISSION_TOGGLE_PAUSE",
|
||||
{
|
||||
GAME_COMMAND_TOGGLE_PAUSE
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_CREATE_RIDE, "PERMISSION_CREATE_RIDE",
|
||||
{
|
||||
GAME_COMMAND_CREATE_RIDE
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_REMOVE_RIDE, "PERMISSION_REMOVE_RIDE",
|
||||
{
|
||||
GAME_COMMAND_DEMOLISH_RIDE
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_BUILD_RIDE, "PERMISSION_BUILD_RIDE",
|
||||
{
|
||||
GAME_COMMAND_PLACE_TRACK,
|
||||
GAME_COMMAND_REMOVE_TRACK,
|
||||
GAME_COMMAND_SET_MAZE_TRACK,
|
||||
GAME_COMMAND_PLACE_TRACK_DESIGN,
|
||||
GAME_COMMAND_PLACE_MAZE_DESIGN,
|
||||
GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT,
|
||||
GAME_COMMAND_REMOVE_RIDE_ENTRANCE_OR_EXIT
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_RIDE_PROPERTIES, "PERMISSION_RIDE_PROPERTIES",
|
||||
{
|
||||
GAME_COMMAND_SET_RIDE_NAME,
|
||||
GAME_COMMAND_SET_RIDE_APPEARANCE,
|
||||
GAME_COMMAND_SET_RIDE_STATUS,
|
||||
GAME_COMMAND_SET_RIDE_VEHICLES,
|
||||
GAME_COMMAND_SET_RIDE_SETTING,
|
||||
GAME_COMMAND_SET_RIDE_PRICE,
|
||||
GAME_COMMAND_SET_BRAKES_SPEED
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_SCENERY, "PERMISSION_SCENERY",
|
||||
{
|
||||
GAME_COMMAND_REMOVE_SCENERY,
|
||||
GAME_COMMAND_PLACE_SCENERY,
|
||||
GAME_COMMAND_SET_BRAKES_SPEED,
|
||||
GAME_COMMAND_REMOVE_FENCE,
|
||||
GAME_COMMAND_PLACE_FENCE,
|
||||
GAME_COMMAND_REMOVE_LARGE_SCENERY,
|
||||
GAME_COMMAND_PLACE_LARGE_SCENERY,
|
||||
GAME_COMMAND_PLACE_BANNER,
|
||||
GAME_COMMAND_REMOVE_BANNER,
|
||||
GAME_COMMAND_SET_SCENERY_COLOUR,
|
||||
GAME_COMMAND_SET_FENCE_COLOUR,
|
||||
GAME_COMMAND_SET_LARGE_SCENERY_COLOUR,
|
||||
GAME_COMMAND_SET_BANNER_COLOUR,
|
||||
GAME_COMMAND_SET_BANNER_NAME,
|
||||
GAME_COMMAND_SET_SIGN_NAME,
|
||||
GAME_COMMAND_SET_BANNER_STYLE,
|
||||
GAME_COMMAND_SET_SIGN_STYLE
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_PATH, "PERMISSION_PATH",
|
||||
{
|
||||
GAME_COMMAND_PLACE_PATH,
|
||||
GAME_COMMAND_PLACE_PATH_FROM_TRACK,
|
||||
GAME_COMMAND_REMOVE_PATH
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_CLEAR_LANDSCAPE, "PERMISSION_CLEAR_LANDSCAPE",
|
||||
{
|
||||
GAME_COMMAND_CLEAR_SCENERY
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_GUEST, "PERMISSION_GUEST",
|
||||
{
|
||||
GAME_COMMAND_SET_GUEST_NAME
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_STAFF, "PERMISSION_STAFF",
|
||||
{
|
||||
GAME_COMMAND_HIRE_NEW_STAFF_MEMBER,
|
||||
GAME_COMMAND_SET_STAFF_PATROL,
|
||||
GAME_COMMAND_FIRE_STAFF_MEMBER,
|
||||
GAME_COMMAND_SET_STAFF_ORDER,
|
||||
GAME_COMMAND_SET_STAFF_COLOUR,
|
||||
GAME_COMMAND_SET_STAFF_NAME
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_PARK_PROPERTIES, "PERMISSION_PARK_PROPERTIES",
|
||||
{
|
||||
GAME_COMMAND_SET_PARK_NAME,
|
||||
GAME_COMMAND_SET_PARK_OPEN,
|
||||
GAME_COMMAND_SET_PARK_ENTRANCE_FEE,
|
||||
GAME_COMMAND_SET_LAND_OWNERSHIP,
|
||||
GAME_COMMAND_BUY_LAND_RIGHTS,
|
||||
GAME_COMMAND_PLACE_PARK_ENTRANCE,
|
||||
GAME_COMMAND_REMOVE_PARK_ENTRANCE
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_PARK_FUNDING, "PERMISSION_PARK_FUNDING",
|
||||
{
|
||||
GAME_COMMAND_SET_CURRENT_LOAN,
|
||||
GAME_COMMAND_SET_RESEARCH_FUNDING,
|
||||
GAME_COMMAND_START_MARKETING_CAMPAIGN
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_KICK_PLAYER, "PERMISSION_KICK_PLAYER",
|
||||
{
|
||||
GAME_COMMAND_KICK_PLAYER
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_MODIFY_GROUPS, "PERMISSION_MODIFY_GROUPS",
|
||||
{
|
||||
GAME_COMMAND_MODIFY_GROUPS
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_SET_PLAYER_GROUP, "PERMISSION_SET_PLAYER_GROUP",
|
||||
{
|
||||
GAME_COMMAND_SET_PLAYER_GROUP
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_CHEAT, "PERMISSION_CHEAT",
|
||||
{
|
||||
GAME_COMMAND_CHEAT
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_TOGGLE_SCENERY_CLUSTER, "PERMISSION_TOGGLE_SCENERY_CLUSTER",
|
||||
{
|
||||
-2
|
||||
}
|
||||
}, {
|
||||
STR_ACTION_PASSWORDLESS_LOGIN, "PERMISSION_PASSWORDLESS_LOGIN",
|
||||
{
|
||||
-3
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
#endif
|
||||
38
src/network/NetworkAction.h
Normal file
38
src/network/NetworkAction.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "../common.h"
|
||||
|
||||
class NetworkAction
|
||||
{
|
||||
public:
|
||||
rct_string_id Name;
|
||||
std::string PermissionName;
|
||||
std::vector<int> Commands;
|
||||
};
|
||||
|
||||
class NetworkActions
|
||||
{
|
||||
public:
|
||||
static const std::vector<NetworkAction> Actions;
|
||||
|
||||
static int FindCommand(int command);
|
||||
static int FindCommandByPermissionName(const std::string &permission_name);
|
||||
};
|
||||
127
src/network/NetworkAddress.cpp
Normal file
127
src/network/NetworkAddress.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include <string>
|
||||
#include <SDL_thread.h>
|
||||
#include "NetworkAddress.h"
|
||||
|
||||
NetworkAddress::NetworkAddress()
|
||||
{
|
||||
_result = std::make_shared<ResolveResult>();
|
||||
_result->status = RESOLVE_NONE;
|
||||
_resolveMutex = SDL_CreateMutex();
|
||||
}
|
||||
|
||||
NetworkAddress::~NetworkAddress()
|
||||
{
|
||||
SDL_DestroyMutex(_resolveMutex);
|
||||
}
|
||||
|
||||
void NetworkAddress::Resolve(const char * host, uint16 port)
|
||||
{
|
||||
SDL_LockMutex(_resolveMutex);
|
||||
{
|
||||
// Create a new result store
|
||||
_result = std::make_shared<ResolveResult>();
|
||||
_result->status = RESOLVE_INPROGRESS;
|
||||
|
||||
// Create a new request
|
||||
auto req = new ResolveRequest();
|
||||
req->Host = std::string(host == nullptr ? "" : host);
|
||||
req->Port = port;;
|
||||
req->Result = _result;
|
||||
|
||||
// Resolve synchronously
|
||||
ResolveWorker(req);
|
||||
}
|
||||
SDL_UnlockMutex(_resolveMutex);
|
||||
}
|
||||
|
||||
void NetworkAddress::ResolveAsync(const char * host, uint16 port)
|
||||
{
|
||||
SDL_LockMutex(_resolveMutex);
|
||||
{
|
||||
// Create a new result store
|
||||
_result = std::make_shared<ResolveResult>();
|
||||
_result->status = RESOLVE_INPROGRESS;
|
||||
|
||||
// Create a new request
|
||||
auto req = new ResolveRequest();
|
||||
req->Host = std::string(host);
|
||||
req->Port = port;
|
||||
req->Result = _result;
|
||||
|
||||
// Spin off a worker thread for resolving the address
|
||||
SDL_CreateThread([](void * pointer) -> int
|
||||
{
|
||||
ResolveWorker((ResolveRequest *)pointer);
|
||||
return 0;
|
||||
}, 0, req);
|
||||
}
|
||||
SDL_UnlockMutex(_resolveMutex);
|
||||
}
|
||||
|
||||
NetworkAddress::RESOLVE_STATUS NetworkAddress::GetResult(sockaddr_storage * ss, int * ss_len)
|
||||
{
|
||||
SDL_LockMutex(_resolveMutex);
|
||||
{
|
||||
const ResolveResult * result = _result.get();
|
||||
if (result->status == RESOLVE_OK)
|
||||
{
|
||||
*ss = result->ss;
|
||||
*ss_len = result->ss_len;
|
||||
}
|
||||
return result->status;
|
||||
}
|
||||
SDL_UnlockMutex(_resolveMutex);
|
||||
}
|
||||
|
||||
void NetworkAddress::ResolveWorker(ResolveRequest * req)
|
||||
{
|
||||
// Resolve the address
|
||||
const char * nodeName = req->Host.c_str();
|
||||
std::string serviceName = std::to_string(req->Port);
|
||||
|
||||
addrinfo hints = { 0 };
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
if (req->Host.empty())
|
||||
{
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
nodeName = nullptr;
|
||||
}
|
||||
|
||||
addrinfo * result;
|
||||
getaddrinfo(nodeName, serviceName.c_str(), &hints, &result);
|
||||
|
||||
// Store the result
|
||||
ResolveResult * resolveResult = req->Result.get();
|
||||
if (result != nullptr)
|
||||
{
|
||||
resolveResult->status = RESOLVE_OK;
|
||||
memcpy(&resolveResult->ss, result->ai_addr, result->ai_addrlen);
|
||||
resolveResult->ss_len = result->ai_addrlen;
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
resolveResult->status = RESOLVE_FAILED;
|
||||
}
|
||||
delete req;
|
||||
}
|
||||
|
||||
#endif
|
||||
71
src/network/NetworkAddress.h
Normal file
71
src/network/NetworkAddress.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <SDL_mutex.h>
|
||||
#include "NetworkTypes.h"
|
||||
#include "../common.h"
|
||||
|
||||
class NetworkAddress final
|
||||
{
|
||||
public:
|
||||
enum RESOLVE_STATUS
|
||||
{
|
||||
RESOLVE_NONE,
|
||||
RESOLVE_INPROGRESS,
|
||||
RESOLVE_OK,
|
||||
RESOLVE_FAILED
|
||||
};
|
||||
|
||||
NetworkAddress();
|
||||
~NetworkAddress();
|
||||
|
||||
void Resolve(const char * host, uint16 port);
|
||||
void ResolveAsync(const char * host, uint16 port);
|
||||
|
||||
RESOLVE_STATUS GetResult(sockaddr_storage * ss, int * ss_len);
|
||||
|
||||
private:
|
||||
struct ResolveResult
|
||||
{
|
||||
RESOLVE_STATUS status;
|
||||
sockaddr_storage ss;
|
||||
int ss_len;
|
||||
};
|
||||
|
||||
struct ResolveRequest
|
||||
{
|
||||
std::string Host;
|
||||
uint16 Port;
|
||||
std::shared_ptr<ResolveResult> Result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Store for the async result. A new store is created for every request.
|
||||
* Old requests simply write to an old store that will then be
|
||||
* automatically deleted by std::shared_ptr.
|
||||
*/
|
||||
std::shared_ptr<ResolveResult> _result;
|
||||
|
||||
/**
|
||||
* Mutex so synchronoise the requests.
|
||||
*/
|
||||
SDL_mutex * _resolveMutex;
|
||||
|
||||
static void ResolveWorker(ResolveRequest * req);
|
||||
};
|
||||
221
src/network/NetworkConnection.cpp
Normal file
221
src/network/NetworkConnection.cpp
Normal file
@@ -0,0 +1,221 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include "NetworkConnection.h"
|
||||
#include "../core/String.hpp"
|
||||
#include <SDL.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../localisation/localisation.h"
|
||||
}
|
||||
|
||||
constexpr size_t NETWORK_DISCONNECT_REASON_BUFFER_SIZE = 256;
|
||||
|
||||
NetworkConnection::NetworkConnection()
|
||||
{
|
||||
ResetLastPacketTime();
|
||||
}
|
||||
|
||||
NetworkConnection::~NetworkConnection()
|
||||
{
|
||||
if (Socket != INVALID_SOCKET)
|
||||
{
|
||||
closesocket(Socket);
|
||||
}
|
||||
if (_lastDisconnectReason)
|
||||
{
|
||||
delete[] _lastDisconnectReason;
|
||||
}
|
||||
}
|
||||
|
||||
int NetworkConnection::ReadPacket()
|
||||
{
|
||||
if (InboundPacket.transferred < sizeof(InboundPacket.size))
|
||||
{
|
||||
// read packet size
|
||||
int readBytes = recv(Socket, &((char*)&InboundPacket.size)[InboundPacket.transferred], sizeof(InboundPacket.size) - InboundPacket.transferred, 0);
|
||||
if (readBytes == SOCKET_ERROR || readBytes == 0)
|
||||
{
|
||||
if (LAST_SOCKET_ERROR() != EWOULDBLOCK && LAST_SOCKET_ERROR() != EAGAIN)
|
||||
{
|
||||
return NETWORK_READPACKET_DISCONNECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NETWORK_READPACKET_NO_DATA;
|
||||
}
|
||||
}
|
||||
InboundPacket.transferred += readBytes;
|
||||
if (InboundPacket.transferred == sizeof(InboundPacket.size))
|
||||
{
|
||||
InboundPacket.size = ntohs(InboundPacket.size);
|
||||
if (InboundPacket.size == 0) // Can't have a size 0 packet
|
||||
{
|
||||
return NETWORK_READPACKET_DISCONNECTED;
|
||||
}
|
||||
InboundPacket.data->resize(InboundPacket.size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// read packet data
|
||||
if (InboundPacket.data->capacity() > 0)
|
||||
{
|
||||
int readBytes = recv(Socket,
|
||||
(char*)&InboundPacket.GetData()[InboundPacket.transferred - sizeof(InboundPacket.size)],
|
||||
sizeof(InboundPacket.size) + InboundPacket.size - InboundPacket.transferred,
|
||||
0);
|
||||
if (readBytes == SOCKET_ERROR || readBytes == 0)
|
||||
{
|
||||
if (LAST_SOCKET_ERROR() != EWOULDBLOCK && LAST_SOCKET_ERROR() != EAGAIN)
|
||||
{
|
||||
return NETWORK_READPACKET_DISCONNECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NETWORK_READPACKET_NO_DATA;
|
||||
}
|
||||
}
|
||||
InboundPacket.transferred += readBytes;
|
||||
}
|
||||
if (InboundPacket.transferred == sizeof(InboundPacket.size) + InboundPacket.size)
|
||||
{
|
||||
_lastPacketTime = SDL_GetTicks();
|
||||
return NETWORK_READPACKET_SUCCESS;
|
||||
}
|
||||
}
|
||||
return NETWORK_READPACKET_MORE_DATA;
|
||||
}
|
||||
|
||||
bool NetworkConnection::SendPacket(NetworkPacket& packet)
|
||||
{
|
||||
uint16 sizen = htons(packet.size);
|
||||
std::vector<uint8> tosend;
|
||||
tosend.reserve(sizeof(sizen) + packet.size);
|
||||
tosend.insert(tosend.end(), (uint8*)&sizen, (uint8*)&sizen + sizeof(sizen));
|
||||
tosend.insert(tosend.end(), packet.data->begin(), packet.data->end());
|
||||
while (true)
|
||||
{
|
||||
int sentBytes = send(Socket, (const char*)&tosend[packet.transferred], tosend.size() - packet.transferred, FLAG_NO_PIPE);
|
||||
if (sentBytes == SOCKET_ERROR)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
packet.transferred += sentBytes;
|
||||
if (packet.transferred == tosend.size())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front)
|
||||
{
|
||||
if (AuthStatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
|
||||
{
|
||||
packet->size = (uint16)packet->data->size();
|
||||
if (front)
|
||||
{
|
||||
_outboundPackets.push_front(std::move(packet));
|
||||
}
|
||||
else
|
||||
{
|
||||
_outboundPackets.push_back(std::move(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkConnection::SendQueuedPackets()
|
||||
{
|
||||
while (_outboundPackets.size() > 0 && SendPacket(*(_outboundPackets.front()).get()))
|
||||
{
|
||||
_outboundPackets.remove(_outboundPackets.front());
|
||||
}
|
||||
}
|
||||
|
||||
bool NetworkConnection::SetTCPNoDelay(bool on)
|
||||
{
|
||||
return setsockopt(Socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on)) == 0;
|
||||
}
|
||||
|
||||
bool NetworkConnection::SetNonBlocking(bool on)
|
||||
{
|
||||
return SetNonBlocking(Socket, on);
|
||||
}
|
||||
|
||||
bool NetworkConnection::SetNonBlocking(SOCKET socket, bool on)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
u_long nonblocking = on;
|
||||
return ioctlsocket(socket, FIONBIO, &nonblocking) == 0;
|
||||
#else
|
||||
int flags = fcntl(socket, F_GETFL, 0);
|
||||
return fcntl(socket, F_SETFL, on ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK)) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void NetworkConnection::ResetLastPacketTime()
|
||||
{
|
||||
_lastPacketTime = SDL_GetTicks();
|
||||
}
|
||||
|
||||
bool NetworkConnection::ReceivedPacketRecently()
|
||||
{
|
||||
#ifndef DEBUG
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastPacketTime + 7000))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
const utf8 * NetworkConnection::GetLastDisconnectReason() const
|
||||
{
|
||||
return this->_lastDisconnectReason;
|
||||
}
|
||||
|
||||
void NetworkConnection::SetLastDisconnectReason(const utf8 * src)
|
||||
{
|
||||
if (src == nullptr)
|
||||
{
|
||||
if (_lastDisconnectReason)
|
||||
{
|
||||
delete[] _lastDisconnectReason;
|
||||
_lastDisconnectReason = nullptr;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (_lastDisconnectReason == nullptr)
|
||||
{
|
||||
_lastDisconnectReason = new utf8[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
|
||||
}
|
||||
String::Set(_lastDisconnectReason, NETWORK_DISCONNECT_REASON_BUFFER_SIZE, src);
|
||||
}
|
||||
|
||||
void NetworkConnection::SetLastDisconnectReason(const rct_string_id string_id, void *args)
|
||||
{
|
||||
char buffer[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
|
||||
format_string(buffer, string_id, args);
|
||||
SetLastDisconnectReason(buffer);
|
||||
}
|
||||
|
||||
#endif
|
||||
63
src/network/NetworkConnection.h
Normal file
63
src/network/NetworkConnection.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "NetworkTypes.h"
|
||||
#include "NetworkKey.h"
|
||||
#include "NetworkPacket.h"
|
||||
#include "../common.h"
|
||||
|
||||
class NetworkPlayer;
|
||||
|
||||
class NetworkConnection
|
||||
{
|
||||
public:
|
||||
SOCKET Socket = INVALID_SOCKET;
|
||||
NetworkPacket InboundPacket;
|
||||
NETWORK_AUTH AuthStatus = NETWORK_AUTH_NONE;
|
||||
NetworkPlayer * Player = nullptr;
|
||||
uint32 PingTime = 0;
|
||||
NetworkKey Key;
|
||||
std::vector<uint8> Challenge;
|
||||
|
||||
NetworkConnection();
|
||||
~NetworkConnection();
|
||||
|
||||
int ReadPacket();
|
||||
void QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front = false);
|
||||
void SendQueuedPackets();
|
||||
bool SetTCPNoDelay(bool on);
|
||||
bool SetNonBlocking(bool on);
|
||||
void ResetLastPacketTime();
|
||||
bool ReceivedPacketRecently();
|
||||
|
||||
const utf8 * GetLastDisconnectReason() const;
|
||||
void SetLastDisconnectReason(const utf8 * src);
|
||||
void SetLastDisconnectReason(const rct_string_id string_id, void * args = nullptr);
|
||||
|
||||
static bool SetNonBlocking(SOCKET socket, bool on);
|
||||
|
||||
private:
|
||||
std::list<std::unique_ptr<NetworkPacket>> _outboundPackets;
|
||||
uint32 _lastPacketTime;
|
||||
utf8 * _lastDisconnectReason = nullptr;
|
||||
|
||||
bool SendPacket(NetworkPacket &packet);
|
||||
};
|
||||
142
src/network/NetworkGroup.cpp
Normal file
142
src/network/NetworkGroup.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include "NetworkTypes.h"
|
||||
#include "NetworkAction.h"
|
||||
#include "NetworkGroup.h"
|
||||
#include "../core/Exception.hpp"
|
||||
|
||||
NetworkGroup::NetworkGroup()
|
||||
{
|
||||
ActionsAllowed = { 0 };
|
||||
}
|
||||
|
||||
NetworkGroup::~NetworkGroup()
|
||||
{
|
||||
}
|
||||
|
||||
NetworkGroup NetworkGroup::FromJson(const json_t * json)
|
||||
{
|
||||
NetworkGroup group;
|
||||
json_t * jsonId = json_object_get(json, "id");
|
||||
json_t * jsonName = json_object_get(json, "name");
|
||||
json_t * jsonPermissions = json_object_get(json, "permissions");
|
||||
if (jsonId == nullptr || jsonName == nullptr || jsonPermissions == nullptr)
|
||||
{
|
||||
throw Exception("Missing group data");
|
||||
}
|
||||
group.Id = (uint8)json_integer_value(jsonId);
|
||||
group._name = std::string(json_string_value(jsonName));
|
||||
for (size_t i = 0; i < group.ActionsAllowed.size(); i++) {
|
||||
group.ActionsAllowed[i] = 0;
|
||||
}
|
||||
for (size_t i = 0; i < json_array_size(jsonPermissions); i++) {
|
||||
json_t * jsonPermissionValue = json_array_get(jsonPermissions, i);
|
||||
const char * perm_name = json_string_value(jsonPermissionValue);
|
||||
if (perm_name == nullptr) {
|
||||
continue;
|
||||
}
|
||||
int action_id = NetworkActions::FindCommandByPermissionName(perm_name);
|
||||
if (action_id != -1) {
|
||||
group.ToggleActionPermission(action_id);
|
||||
}
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
json_t * NetworkGroup::ToJson() const
|
||||
{
|
||||
json_t * jsonGroup = json_object();
|
||||
json_object_set_new(jsonGroup, "id", json_integer(Id));
|
||||
json_object_set_new(jsonGroup, "name", json_string(GetName().c_str()));
|
||||
json_t * actionsArray = json_array();
|
||||
for (size_t i = 0; i < NetworkActions::Actions.size(); i++)
|
||||
{
|
||||
if (CanPerformAction(i))
|
||||
{
|
||||
const char * perm_name = NetworkActions::Actions[i].PermissionName.c_str();
|
||||
json_array_append_new(actionsArray, json_string(perm_name));
|
||||
}
|
||||
}
|
||||
json_object_set_new(jsonGroup, "permissions", actionsArray);
|
||||
return jsonGroup;
|
||||
}
|
||||
|
||||
const std::string & NetworkGroup::GetName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
void NetworkGroup::SetName(std::string name)
|
||||
{
|
||||
_name = name;
|
||||
}
|
||||
|
||||
void NetworkGroup::Read(NetworkPacket &packet)
|
||||
{
|
||||
packet >> Id;
|
||||
SetName(packet.ReadString());
|
||||
for (size_t i = 0; i < ActionsAllowed.size(); i++)
|
||||
{
|
||||
packet >> ActionsAllowed[i];
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkGroup::Write(NetworkPacket &packet)
|
||||
{
|
||||
packet << Id;
|
||||
packet.WriteString(GetName().c_str());
|
||||
for (size_t i = 0; i < ActionsAllowed.size(); i++)
|
||||
{
|
||||
packet << ActionsAllowed[i];
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkGroup::ToggleActionPermission(size_t index)
|
||||
{
|
||||
size_t byte = index / 8;
|
||||
size_t bit = index % 8;
|
||||
if (byte >= ActionsAllowed.size())
|
||||
{
|
||||
return;
|
||||
}
|
||||
ActionsAllowed[byte] ^= (1 << bit);
|
||||
}
|
||||
|
||||
bool NetworkGroup::CanPerformAction(size_t index) const
|
||||
{
|
||||
size_t byte = index / 8;
|
||||
size_t bit = index % 8;
|
||||
if (byte >= ActionsAllowed.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (ActionsAllowed[byte] & (1 << bit)) ? true : false;
|
||||
}
|
||||
|
||||
bool NetworkGroup::CanPerformCommand(int command) const
|
||||
{
|
||||
int action = NetworkActions::FindCommand(command);
|
||||
if (action != -1)
|
||||
{
|
||||
return CanPerformAction(action);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
48
src/network/NetworkGroup.h
Normal file
48
src/network/NetworkGroup.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <jansson.h>
|
||||
#include "NetworkPacket.h"
|
||||
#include "../common.h"
|
||||
|
||||
class NetworkGroup
|
||||
{
|
||||
public:
|
||||
std::array<uint8, 8> ActionsAllowed;
|
||||
uint8 Id = 0;
|
||||
|
||||
static NetworkGroup FromJson(const json_t * json);
|
||||
|
||||
NetworkGroup();
|
||||
~NetworkGroup();
|
||||
|
||||
const std::string & GetName() const;
|
||||
void SetName(std::string name);
|
||||
|
||||
void Read(NetworkPacket &packet);
|
||||
void Write(NetworkPacket &packet);
|
||||
void ToggleActionPermission(size_t index);
|
||||
bool CanPerformAction(size_t index) const;
|
||||
bool CanPerformCommand(int command) const;
|
||||
|
||||
json_t * ToJson() const;
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
};
|
||||
118
src/network/NetworkPacket.cpp
Normal file
118
src/network/NetworkPacket.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include "NetworkTypes.h"
|
||||
#include "NetworkPacket.h"
|
||||
|
||||
NetworkPacket::NetworkPacket()
|
||||
{
|
||||
transferred = 0;
|
||||
read = 0;
|
||||
size = 0;
|
||||
data = std::make_shared<std::vector<uint8>>();
|
||||
}
|
||||
|
||||
std::unique_ptr<NetworkPacket> NetworkPacket::Allocate()
|
||||
{
|
||||
return std::unique_ptr<NetworkPacket>(new NetworkPacket); // change to make_unique in c++14
|
||||
}
|
||||
|
||||
std::unique_ptr<NetworkPacket> NetworkPacket::Duplicate(NetworkPacket &packet)
|
||||
{
|
||||
return std::unique_ptr<NetworkPacket>(new NetworkPacket(packet)); // change to make_unique in c++14
|
||||
}
|
||||
|
||||
uint8 * NetworkPacket::GetData()
|
||||
{
|
||||
return &(*data)[0];
|
||||
}
|
||||
|
||||
uint32 NetworkPacket::GetCommand()
|
||||
{
|
||||
if (data->size() >= sizeof(uint32))
|
||||
{
|
||||
return ByteSwapBE(*(uint32 *)(&(*data)[0]));
|
||||
}
|
||||
else
|
||||
{
|
||||
return NETWORK_COMMAND_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkPacket::Clear()
|
||||
{
|
||||
transferred = 0;
|
||||
read = 0;
|
||||
data->clear();
|
||||
}
|
||||
|
||||
bool NetworkPacket::CommandRequiresAuth()
|
||||
{
|
||||
switch (GetCommand()) {
|
||||
case NETWORK_COMMAND_PING:
|
||||
case NETWORK_COMMAND_AUTH:
|
||||
case NETWORK_COMMAND_TOKEN:
|
||||
case NETWORK_COMMAND_GAMEINFO:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkPacket::Write(const uint8 * bytes, uint32 size)
|
||||
{
|
||||
data->insert(data->end(), bytes, bytes + size);
|
||||
}
|
||||
|
||||
void NetworkPacket::WriteString(const utf8 * string)
|
||||
{
|
||||
Write((uint8 *)string, strlen(string) + 1);
|
||||
}
|
||||
|
||||
const uint8 * NetworkPacket::Read(uint32 size)
|
||||
{
|
||||
if (read + size > NetworkPacket::size)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8 * data = &GetData()[read];
|
||||
read += size;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
const utf8 * NetworkPacket::ReadString()
|
||||
{
|
||||
char * str = (char *)&GetData()[read];
|
||||
char * strend = str;
|
||||
while (read < size && *strend != 0)
|
||||
{
|
||||
read++;
|
||||
strend++;
|
||||
}
|
||||
if (*strend != 0)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
read++;
|
||||
return str;
|
||||
}
|
||||
|
||||
#endif
|
||||
71
src/network/NetworkPacket.h
Normal file
71
src/network/NetworkPacket.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "NetworkTypes.h"
|
||||
#include "../common.h"
|
||||
|
||||
class NetworkPacket
|
||||
{
|
||||
public:
|
||||
uint16 size;
|
||||
std::shared_ptr<std::vector<uint8>> data;
|
||||
uint32 transferred;
|
||||
sint32 read;
|
||||
|
||||
static std::unique_ptr<NetworkPacket> Allocate();
|
||||
static std::unique_ptr<NetworkPacket> Duplicate(NetworkPacket& packet);
|
||||
|
||||
NetworkPacket();
|
||||
|
||||
uint8 * GetData();
|
||||
uint32 GetCommand();
|
||||
|
||||
void Clear();
|
||||
bool CommandRequiresAuth();
|
||||
|
||||
const uint8 * Read(uint32 size);
|
||||
const utf8 * ReadString();
|
||||
|
||||
void Write(const uint8 * bytes, uint32 size);
|
||||
void WriteString(const utf8 * string);
|
||||
|
||||
template <typename T>
|
||||
NetworkPacket & operator >>(T &value)
|
||||
{
|
||||
if (read + sizeof(value) > size)
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = ByteSwapBE(*((T *)&GetData()[read]));
|
||||
read += sizeof(value);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
NetworkPacket & operator <<(T value) {
|
||||
T swapped = ByteSwapBE(value);
|
||||
uint8 * bytes = (uint8 *)&swapped;
|
||||
data->insert(data->end(), bytes, bytes + sizeof(value));
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
55
src/network/NetworkPlayer.cpp
Normal file
55
src/network/NetworkPlayer.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include "NetworkPacket.h"
|
||||
#include "NetworkPlayer.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../interface/window.h"
|
||||
#include "../localisation/localisation.h"
|
||||
}
|
||||
|
||||
void NetworkPlayer::SetName(const std::string &name)
|
||||
{
|
||||
// 36 == 31 + strlen(" #255");
|
||||
NetworkPlayer::name = name.substr(0, 36);
|
||||
utf8_remove_format_codes((utf8 *)NetworkPlayer::name.data(), false);
|
||||
}
|
||||
|
||||
void NetworkPlayer::Read(NetworkPacket &packet)
|
||||
{
|
||||
const utf8 * name = packet.ReadString();
|
||||
SetName(name);
|
||||
packet >> id >> flags >> group;
|
||||
}
|
||||
|
||||
void NetworkPlayer::Write(NetworkPacket &packet)
|
||||
{
|
||||
packet.WriteString((const char*)name.c_str());
|
||||
packet << id << flags << group;
|
||||
}
|
||||
|
||||
void NetworkPlayer::AddMoneySpent(money32 cost)
|
||||
{
|
||||
money_spent += cost;
|
||||
commands_ran++;
|
||||
window_invalidate_by_number(WC_PLAYER, id);
|
||||
}
|
||||
|
||||
#endif
|
||||
51
src/network/NetworkPlayer.h
Normal file
51
src/network/NetworkPlayer.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "../common.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../world/map.h"
|
||||
}
|
||||
|
||||
class NetworkPacket;
|
||||
|
||||
class NetworkPlayer
|
||||
{
|
||||
public:
|
||||
uint8 id = 0;
|
||||
std::string name;
|
||||
uint16 ping = 0;
|
||||
uint8 flags = 0;
|
||||
uint8 group = 0;
|
||||
money32 money_spent = MONEY(0, 0);
|
||||
uint32 commands_ran = 0;
|
||||
int last_action = -999;
|
||||
uint32 last_action_time = 0;
|
||||
rct_xyz16 last_action_coord = { 0 };
|
||||
std::string keyhash;
|
||||
|
||||
NetworkPlayer() = default;
|
||||
|
||||
void SetName(const std::string &name);
|
||||
|
||||
void Read(NetworkPacket &packet);
|
||||
void Write(NetworkPacket &packet);
|
||||
void AddMoneySpent(money32 cost);
|
||||
};
|
||||
142
src/network/NetworkTypes.h
Normal file
142
src/network/NetworkTypes.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||||
/*****************************************************************************
|
||||
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||||
*
|
||||
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||||
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* A full copy of the GNU General Public License can be found in licence.txt
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL_endian.h>
|
||||
#include <SDL_platform.h>
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
#ifdef __WINDOWS__
|
||||
// winsock2 must be included before windows.h
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#define LAST_SOCKET_ERROR() WSAGetLastError()
|
||||
#undef EWOULDBLOCK
|
||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||
#ifndef SHUT_RD
|
||||
#define SHUT_RD SD_RECEIVE
|
||||
#endif
|
||||
#ifndef SHUT_RDWR
|
||||
#define SHUT_RDWR SD_BOTH
|
||||
#endif
|
||||
#define FLAG_NO_PIPE 0
|
||||
#else
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
typedef int SOCKET;
|
||||
#define SOCKET_ERROR -1
|
||||
#define INVALID_SOCKET -1
|
||||
#define LAST_SOCKET_ERROR() errno
|
||||
#define closesocket close
|
||||
#define ioctlsocket ioctl
|
||||
#if defined(__LINUX__)
|
||||
#define FLAG_NO_PIPE MSG_NOSIGNAL
|
||||
#else
|
||||
#define FLAG_NO_PIPE 0
|
||||
#endif // defined(__LINUX__)
|
||||
#endif // __WINDOWS__
|
||||
|
||||
#include "../common.h"
|
||||
#endif
|
||||
|
||||
enum NETWORK_READPACKET
|
||||
{
|
||||
NETWORK_READPACKET_SUCCESS,
|
||||
NETWORK_READPACKET_NO_DATA,
|
||||
NETWORK_READPACKET_MORE_DATA,
|
||||
NETWORK_READPACKET_DISCONNECTED
|
||||
};
|
||||
|
||||
enum NETWORK_AUTH
|
||||
{
|
||||
NETWORK_AUTH_NONE,
|
||||
NETWORK_AUTH_REQUESTED,
|
||||
NETWORK_AUTH_OK,
|
||||
NETWORK_AUTH_BADVERSION,
|
||||
NETWORK_AUTH_BADNAME,
|
||||
NETWORK_AUTH_BADPASSWORD,
|
||||
NETWORK_AUTH_VERIFICATIONFAILURE,
|
||||
NETWORK_AUTH_FULL,
|
||||
NETWORK_AUTH_REQUIREPASSWORD,
|
||||
NETWORK_AUTH_VERIFIED,
|
||||
NETWORK_AUTH_UNKNOWN_KEY_DISALLOWED,
|
||||
};
|
||||
|
||||
enum NETWORK_COMMAND
|
||||
{
|
||||
NETWORK_COMMAND_AUTH,
|
||||
NETWORK_COMMAND_MAP,
|
||||
NETWORK_COMMAND_CHAT,
|
||||
NETWORK_COMMAND_GAMECMD,
|
||||
NETWORK_COMMAND_TICK,
|
||||
NETWORK_COMMAND_PLAYERLIST,
|
||||
NETWORK_COMMAND_PING,
|
||||
NETWORK_COMMAND_PINGLIST,
|
||||
NETWORK_COMMAND_SETDISCONNECTMSG,
|
||||
NETWORK_COMMAND_GAMEINFO,
|
||||
NETWORK_COMMAND_SHOWERROR,
|
||||
NETWORK_COMMAND_GROUPLIST,
|
||||
NETWORK_COMMAND_EVENT,
|
||||
NETWORK_COMMAND_TOKEN,
|
||||
NETWORK_COMMAND_MAX,
|
||||
NETWORK_COMMAND_INVALID = -1
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
template <size_t size>
|
||||
struct ByteSwapT { };
|
||||
|
||||
template <>
|
||||
struct ByteSwapT<1>
|
||||
{
|
||||
static uint8 SwapBE(uint8 value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ByteSwapT<2>
|
||||
{
|
||||
static uint16 SwapBE(uint16 value)
|
||||
{
|
||||
return SDL_SwapBE16(value);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ByteSwapT<4>
|
||||
{
|
||||
static uint32 SwapBE(uint32 value)
|
||||
{
|
||||
return SDL_SwapBE32(value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
T ByteSwapBE(const T& value)
|
||||
{
|
||||
return ByteSwapT<sizeof(T)>::SwapBE(value);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -14,6 +14,8 @@
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
#include "../core/Json.hpp"
|
||||
@@ -241,3 +243,5 @@ void NetworkUserManager::GetStorePath(utf8 * buffer, size_t bufferSize)
|
||||
platform_get_user_directory(buffer, nullptr);
|
||||
Path::Append(buffer, bufferSize, USER_STORE_FILENAME);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -27,20 +27,6 @@ enum {
|
||||
NETWORK_PLAYER_FLAG_ISSERVER = 1 << 0,
|
||||
};
|
||||
|
||||
enum {
|
||||
NETWORK_AUTH_NONE,
|
||||
NETWORK_AUTH_REQUESTED,
|
||||
NETWORK_AUTH_OK,
|
||||
NETWORK_AUTH_BADVERSION,
|
||||
NETWORK_AUTH_BADNAME,
|
||||
NETWORK_AUTH_BADPASSWORD,
|
||||
NETWORK_AUTH_VERIFICATIONFAILURE,
|
||||
NETWORK_AUTH_FULL,
|
||||
NETWORK_AUTH_REQUIREPASSWORD,
|
||||
NETWORK_AUTH_VERIFIED,
|
||||
NETWORK_AUTH_UNKNOWN_KEY_DISALLOWED,
|
||||
};
|
||||
|
||||
enum {
|
||||
NETWORK_STATUS_NONE,
|
||||
NETWORK_STATUS_READY,
|
||||
@@ -62,6 +48,8 @@ extern "C" {
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#include "NetworkTypes.h"
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
// This define specifies which version of network stream current build uses.
|
||||
@@ -70,41 +58,6 @@ extern "C" {
|
||||
#define NETWORK_STREAM_VERSION "9"
|
||||
#define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION
|
||||
|
||||
#define NETWORK_DISCONNECT_REASON_BUFFER_SIZE 256
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#define LAST_SOCKET_ERROR() WSAGetLastError()
|
||||
#undef EWOULDBLOCK
|
||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||
#ifndef SHUT_RD
|
||||
#define SHUT_RD SD_RECEIVE
|
||||
#endif
|
||||
#ifndef SHUT_RDWR
|
||||
#define SHUT_RDWR SD_BOTH
|
||||
#endif
|
||||
#define FLAG_NO_PIPE 0
|
||||
#else
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
typedef int SOCKET;
|
||||
#define SOCKET_ERROR -1
|
||||
#define INVALID_SOCKET -1
|
||||
#define LAST_SOCKET_ERROR() errno
|
||||
#define closesocket close
|
||||
#define ioctlsocket ioctl
|
||||
#if defined(__LINUX__)
|
||||
#define FLAG_NO_PIPE MSG_NOSIGNAL
|
||||
#else
|
||||
#define FLAG_NO_PIPE 0
|
||||
#endif // defined(__LINUX__)
|
||||
#endif // __WINDOWS__
|
||||
|
||||
// Fixes issues on OS X
|
||||
#if defined(_RCT2_H_) && !defined(_MSC_VER)
|
||||
// use similar struct packing as MSVC for our structs
|
||||
@@ -123,225 +76,14 @@ extern "C" {
|
||||
#include <SDL.h>
|
||||
#include "../core/Json.hpp"
|
||||
#include "../core/Nullable.hpp"
|
||||
#include "NetworkAddress.h"
|
||||
#include "NetworkConnection.h"
|
||||
#include "NetworkGroup.h"
|
||||
#include "NetworkKey.h"
|
||||
#include "NetworkPacket.h"
|
||||
#include "NetworkPlayer.h"
|
||||
#include "NetworkUser.h"
|
||||
|
||||
template <std::size_t size>
|
||||
struct ByteSwapT { };
|
||||
template <>
|
||||
struct ByteSwapT<1> { static uint8 SwapBE(uint8 value) { return value; } };
|
||||
template <>
|
||||
struct ByteSwapT<2> { static uint16 SwapBE(uint16 value) { return SDL_SwapBE16(value); } };
|
||||
template <>
|
||||
struct ByteSwapT<4> { static uint32 SwapBE(uint32 value) { return SDL_SwapBE32(value); } };
|
||||
template <typename T>
|
||||
T ByteSwapBE(const T& value) { return ByteSwapT<sizeof(T)>::SwapBE(value); }
|
||||
|
||||
class NetworkPacket
|
||||
{
|
||||
public:
|
||||
NetworkPacket();
|
||||
static std::unique_ptr<NetworkPacket> Allocate();
|
||||
static std::unique_ptr<NetworkPacket> Duplicate(NetworkPacket& packet);
|
||||
uint8* GetData();
|
||||
uint32 GetCommand();
|
||||
template <typename T>
|
||||
NetworkPacket& operator<<(T value) {
|
||||
T swapped = ByteSwapBE(value); uint8* bytes = (uint8*)&swapped; data->insert(data->end(), bytes, bytes + sizeof(value));
|
||||
return *this;
|
||||
}
|
||||
void Write(const uint8* bytes, unsigned int size);
|
||||
void WriteString(const char* string);
|
||||
template <typename T>
|
||||
NetworkPacket& operator>>(T& value) {
|
||||
if (read + sizeof(value) > size) { value = 0; } else { value = ByteSwapBE(*((T*)&GetData()[read])); read += sizeof(value); }
|
||||
return *this;
|
||||
}
|
||||
const uint8* Read(unsigned int size);
|
||||
const char* ReadString();
|
||||
void Clear();
|
||||
bool CommandRequiresAuth();
|
||||
|
||||
uint16 size;
|
||||
std::shared_ptr<std::vector<uint8>> data;
|
||||
unsigned int transferred;
|
||||
int read;
|
||||
};
|
||||
|
||||
class NetworkPlayer
|
||||
{
|
||||
public:
|
||||
NetworkPlayer() = default;
|
||||
void Read(NetworkPacket& packet);
|
||||
void Write(NetworkPacket& packet);
|
||||
void SetName(const std::string &name);
|
||||
void AddMoneySpent(money32 cost);
|
||||
uint8 id = 0;
|
||||
std::string name;
|
||||
uint16 ping = 0;
|
||||
uint8 flags = 0;
|
||||
uint8 group = 0;
|
||||
money32 money_spent = MONEY(0, 0);
|
||||
unsigned int commands_ran = 0;
|
||||
int last_action = -999;
|
||||
uint32 last_action_time = 0;
|
||||
rct_xyz16 last_action_coord = { 0 };
|
||||
std::string keyhash;
|
||||
};
|
||||
|
||||
class NetworkAction
|
||||
{
|
||||
public:
|
||||
rct_string_id name;
|
||||
std::string permission_name;
|
||||
std::vector<int> commands;
|
||||
};
|
||||
|
||||
class NetworkActions
|
||||
{
|
||||
public:
|
||||
int FindCommand(int command);
|
||||
int FindCommandByPermissionName(const std::string &permission_name);
|
||||
const std::vector<NetworkAction> actions = {
|
||||
{STR_ACTION_CHAT, "PERMISSION_CHAT",
|
||||
{-1}},
|
||||
{STR_ACTION_TERRAFORM, "PERMISSION_TERRAFORM",
|
||||
{GAME_COMMAND_SET_LAND_HEIGHT, GAME_COMMAND_RAISE_LAND, GAME_COMMAND_LOWER_LAND,
|
||||
GAME_COMMAND_EDIT_LAND_SMOOTH, GAME_COMMAND_CHANGE_SURFACE_STYLE}},
|
||||
{STR_ACTION_SET_WATER_LEVEL, "PERMISSION_SET_WATER_LEVEL",
|
||||
{GAME_COMMAND_SET_WATER_HEIGHT, GAME_COMMAND_RAISE_WATER, GAME_COMMAND_LOWER_WATER}},
|
||||
{STR_ACTION_TOGGLE_PAUSE, "PERMISSION_TOGGLE_PAUSE",
|
||||
{GAME_COMMAND_TOGGLE_PAUSE}},
|
||||
{STR_ACTION_CREATE_RIDE, "PERMISSION_CREATE_RIDE",
|
||||
{GAME_COMMAND_CREATE_RIDE}},
|
||||
{STR_ACTION_REMOVE_RIDE, "PERMISSION_REMOVE_RIDE",
|
||||
{GAME_COMMAND_DEMOLISH_RIDE}},
|
||||
{STR_ACTION_BUILD_RIDE, "PERMISSION_BUILD_RIDE",
|
||||
{GAME_COMMAND_PLACE_TRACK, GAME_COMMAND_REMOVE_TRACK, GAME_COMMAND_SET_MAZE_TRACK,
|
||||
GAME_COMMAND_PLACE_TRACK_DESIGN, GAME_COMMAND_PLACE_MAZE_DESIGN, GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT,
|
||||
GAME_COMMAND_REMOVE_RIDE_ENTRANCE_OR_EXIT}},
|
||||
{STR_ACTION_RIDE_PROPERTIES, "PERMISSION_RIDE_PROPERTIES",
|
||||
{GAME_COMMAND_SET_RIDE_NAME, GAME_COMMAND_SET_RIDE_APPEARANCE, GAME_COMMAND_SET_RIDE_STATUS,
|
||||
GAME_COMMAND_SET_RIDE_VEHICLES, GAME_COMMAND_SET_RIDE_SETTING, GAME_COMMAND_SET_RIDE_PRICE,
|
||||
GAME_COMMAND_SET_BRAKES_SPEED}},
|
||||
{STR_ACTION_SCENERY, "PERMISSION_SCENERY",
|
||||
{GAME_COMMAND_REMOVE_SCENERY, GAME_COMMAND_PLACE_SCENERY, GAME_COMMAND_SET_BRAKES_SPEED,
|
||||
GAME_COMMAND_REMOVE_FENCE, GAME_COMMAND_PLACE_FENCE, GAME_COMMAND_REMOVE_LARGE_SCENERY,
|
||||
GAME_COMMAND_PLACE_LARGE_SCENERY, GAME_COMMAND_PLACE_BANNER, GAME_COMMAND_REMOVE_BANNER,
|
||||
GAME_COMMAND_SET_SCENERY_COLOUR, GAME_COMMAND_SET_FENCE_COLOUR, GAME_COMMAND_SET_LARGE_SCENERY_COLOUR,
|
||||
GAME_COMMAND_SET_BANNER_COLOUR, GAME_COMMAND_SET_BANNER_NAME, GAME_COMMAND_SET_SIGN_NAME,
|
||||
GAME_COMMAND_SET_BANNER_STYLE, GAME_COMMAND_SET_SIGN_STYLE}},
|
||||
{STR_ACTION_PATH, "PERMISSION_PATH",
|
||||
{GAME_COMMAND_PLACE_PATH, GAME_COMMAND_PLACE_PATH_FROM_TRACK, GAME_COMMAND_REMOVE_PATH}},
|
||||
{STR_ACTION_CLEAR_LANDSCAPE, "PERMISSION_CLEAR_LANDSCAPE",
|
||||
{GAME_COMMAND_CLEAR_SCENERY}},
|
||||
{STR_ACTION_GUEST, "PERMISSION_GUEST",
|
||||
{GAME_COMMAND_SET_GUEST_NAME}},
|
||||
{STR_ACTION_STAFF, "PERMISSION_STAFF",
|
||||
{GAME_COMMAND_HIRE_NEW_STAFF_MEMBER, GAME_COMMAND_SET_STAFF_PATROL, GAME_COMMAND_FIRE_STAFF_MEMBER,
|
||||
GAME_COMMAND_SET_STAFF_ORDER, GAME_COMMAND_SET_STAFF_COLOUR, GAME_COMMAND_SET_STAFF_NAME}},
|
||||
{STR_ACTION_PARK_PROPERTIES, "PERMISSION_PARK_PROPERTIES",
|
||||
{GAME_COMMAND_SET_PARK_NAME, GAME_COMMAND_SET_PARK_OPEN, GAME_COMMAND_SET_PARK_ENTRANCE_FEE,
|
||||
GAME_COMMAND_SET_LAND_OWNERSHIP, GAME_COMMAND_BUY_LAND_RIGHTS, GAME_COMMAND_PLACE_PARK_ENTRANCE,
|
||||
GAME_COMMAND_REMOVE_PARK_ENTRANCE}},
|
||||
{STR_ACTION_PARK_FUNDING, "PERMISSION_PARK_FUNDING",
|
||||
{GAME_COMMAND_SET_CURRENT_LOAN, GAME_COMMAND_SET_RESEARCH_FUNDING, GAME_COMMAND_START_MARKETING_CAMPAIGN}},
|
||||
{STR_ACTION_KICK_PLAYER, "PERMISSION_KICK_PLAYER",
|
||||
{GAME_COMMAND_KICK_PLAYER}},
|
||||
{STR_ACTION_MODIFY_GROUPS, "PERMISSION_MODIFY_GROUPS",
|
||||
{GAME_COMMAND_MODIFY_GROUPS}},
|
||||
{STR_ACTION_SET_PLAYER_GROUP, "PERMISSION_SET_PLAYER_GROUP",
|
||||
{GAME_COMMAND_SET_PLAYER_GROUP}},
|
||||
{STR_ACTION_CHEAT, "PERMISSION_CHEAT",
|
||||
{GAME_COMMAND_CHEAT}},
|
||||
{STR_ACTION_TOGGLE_SCENERY_CLUSTER, "PERMISSION_TOGGLE_SCENERY_CLUSTER",
|
||||
{-2}},
|
||||
{STR_ACTION_PASSWORDLESS_LOGIN, "PERMISSION_PASSWORDLESS_LOGIN",
|
||||
{-3}},
|
||||
};
|
||||
};
|
||||
|
||||
class NetworkGroup
|
||||
{
|
||||
public:
|
||||
NetworkGroup();
|
||||
~NetworkGroup();
|
||||
void Read(NetworkPacket& packet);
|
||||
void Write(NetworkPacket& packet);
|
||||
json_t * ToJson() const;
|
||||
static NetworkGroup FromJson(const json_t * json);
|
||||
void ToggleActionPermission(size_t index);
|
||||
bool CanPerformAction(size_t index) const;
|
||||
bool CanPerformCommand(int command) const;
|
||||
const std::string& GetName() const;
|
||||
void SetName(std::string name);
|
||||
std::array<uint8, 8> actions_allowed;
|
||||
uint8 id = 0;
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class NetworkConnection
|
||||
{
|
||||
public:
|
||||
NetworkConnection();
|
||||
~NetworkConnection();
|
||||
int ReadPacket();
|
||||
void QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front = false);
|
||||
void SendQueuedPackets();
|
||||
bool SetTCPNoDelay(bool on);
|
||||
bool SetNonBlocking(bool on);
|
||||
static bool SetNonBlocking(SOCKET socket, bool on);
|
||||
void ResetLastPacketTime();
|
||||
bool ReceivedPacketRecently();
|
||||
|
||||
const char *getLastDisconnectReason() const;
|
||||
void setLastDisconnectReason(const char *src);
|
||||
void setLastDisconnectReason(const rct_string_id string_id, void *args = nullptr);
|
||||
|
||||
SOCKET socket = INVALID_SOCKET;
|
||||
NetworkPacket inboundpacket;
|
||||
int authstatus = NETWORK_AUTH_NONE;
|
||||
NetworkPlayer* player;
|
||||
uint32 ping_time = 0;
|
||||
NetworkKey key;
|
||||
std::vector<uint8> challenge;
|
||||
|
||||
private:
|
||||
char* last_disconnect_reason;
|
||||
bool SendPacket(NetworkPacket& packet);
|
||||
std::list<std::unique_ptr<NetworkPacket>> outboundpackets;
|
||||
uint32 last_packet_time;
|
||||
};
|
||||
|
||||
class NetworkAddress
|
||||
{
|
||||
public:
|
||||
NetworkAddress();
|
||||
void Resolve(const char* host, unsigned short port, bool nonblocking = true);
|
||||
int GetResolveStatus(void);
|
||||
|
||||
std::shared_ptr<sockaddr_storage> ss;
|
||||
std::shared_ptr<int> ss_len;
|
||||
|
||||
enum {
|
||||
RESOLVE_NONE,
|
||||
RESOLVE_INPROGRESS,
|
||||
RESOLVE_OK,
|
||||
RESOLVE_FAILED
|
||||
};
|
||||
|
||||
private:
|
||||
static int ResolveFunc(void* pointer);
|
||||
|
||||
const char* host = nullptr;
|
||||
unsigned short port = 0;
|
||||
SDL_mutex* mutex = nullptr;
|
||||
SDL_cond* cond = nullptr;
|
||||
std::shared_ptr<int> status;
|
||||
};
|
||||
|
||||
class Network
|
||||
{
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user