Skip to content

Commit

Permalink
conform to name convention
Browse files Browse the repository at this point in the history
  • Loading branch information
IntelOrca authored and Goddesen committed Jun 1, 2016
1 parent 9ebb55e commit 5cb81e8
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 198 deletions.
4 changes: 2 additions & 2 deletions src/network/NetworkAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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++)
for (auto it = action.Commands.begin(); it != action.Commands.end(); it++)
{
if ((*it) == command)
{
Expand All @@ -47,7 +47,7 @@ int NetworkActions::FindCommandByPermissionName(const std::string &permission_na
{
auto it = std::find_if(Actions.begin(), Actions.end(), [&permission_name](NetworkAction const &action)
{
if (action.permission_name == permission_name)
if (action.PermissionName == permission_name)
{
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/network/NetworkAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
class NetworkAction
{
public:
rct_string_id name;
std::string permission_name;
std::vector<int> commands;
rct_string_id Name;
std::string PermissionName;
std::vector<int> Commands;
};

class NetworkActions
Expand Down
82 changes: 39 additions & 43 deletions src/network/NetworkConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,27 @@ constexpr size_t NETWORK_DISCONNECT_REASON_BUFFER_SIZE = 256;

NetworkConnection::NetworkConnection()
{
authstatus = NETWORK_AUTH_NONE;
player = 0;
socket = INVALID_SOCKET;
ResetLastPacketTime();
last_disconnect_reason = nullptr;
}

NetworkConnection::~NetworkConnection()
{
if (socket != INVALID_SOCKET)
if (Socket != INVALID_SOCKET)
{
closesocket(socket);
closesocket(Socket);
}
if (last_disconnect_reason)
if (_lastDisconnectReason)
{
delete[] last_disconnect_reason;
delete[] _lastDisconnectReason;
}
}

int NetworkConnection::ReadPacket()
{
if (inboundpacket.transferred < sizeof(inboundpacket.size))
if (InboundPacket.transferred < sizeof(InboundPacket.size))
{
// read packet size
int readBytes = recv(socket, &((char*)&inboundpacket.size)[inboundpacket.transferred], sizeof(inboundpacket.size) - inboundpacket.transferred, 0);
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)
Expand All @@ -63,25 +59,25 @@ int NetworkConnection::ReadPacket()
return NETWORK_READPACKET_NO_DATA;
}
}
inboundpacket.transferred += readBytes;
if (inboundpacket.transferred == sizeof(inboundpacket.size))
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
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);
InboundPacket.data->resize(InboundPacket.size);
}
}
else
{
// read packet data
if (inboundpacket.data->capacity() > 0)
if (InboundPacket.data->capacity() > 0)
{
int readBytes = recv(socket,
(char*)&inboundpacket.GetData()[inboundpacket.transferred - sizeof(inboundpacket.size)],
sizeof(inboundpacket.size) + inboundpacket.size - inboundpacket.transferred,
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)
{
Expand All @@ -94,11 +90,11 @@ int NetworkConnection::ReadPacket()
return NETWORK_READPACKET_NO_DATA;
}
}
inboundpacket.transferred += readBytes;
InboundPacket.transferred += readBytes;
}
if (inboundpacket.transferred == sizeof(inboundpacket.size) + inboundpacket.size)
if (InboundPacket.transferred == sizeof(InboundPacket.size) + InboundPacket.size)
{
last_packet_time = SDL_GetTicks();
_lastPacketTime = SDL_GetTicks();
return NETWORK_READPACKET_SUCCESS;
}
}
Expand All @@ -114,7 +110,7 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet)
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);
int sentBytes = send(Socket, (const char*)&tosend[packet.transferred], tosend.size() - packet.transferred, FLAG_NO_PIPE);
if (sentBytes == SOCKET_ERROR)
{
return false;
Expand All @@ -130,36 +126,36 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet)

void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front)
{
if (authstatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
if (AuthStatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
{
packet->size = (uint16)packet->data->size();
if (front)
{
outboundpackets.push_front(std::move(packet));
_outboundPackets.push_front(std::move(packet));
}
else
{
outboundpackets.push_back(std::move(packet));
_outboundPackets.push_back(std::move(packet));
}
}
}

void NetworkConnection::SendQueuedPackets()
{
while (outboundpackets.size() > 0 && SendPacket(*(outboundpackets.front()).get()))
while (_outboundPackets.size() > 0 && SendPacket(*(_outboundPackets.front()).get()))
{
outboundpackets.remove(outboundpackets.front());
_outboundPackets.remove(_outboundPackets.front());
}
}

bool NetworkConnection::SetTCPNoDelay(bool on)
{
return setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on)) == 0;
return setsockopt(Socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on)) == 0;
}

bool NetworkConnection::SetNonBlocking(bool on)
{
return SetNonBlocking(socket, on);
return SetNonBlocking(Socket, on);
}

bool NetworkConnection::SetNonBlocking(SOCKET socket, bool on)
Expand All @@ -175,47 +171,47 @@ bool NetworkConnection::SetNonBlocking(SOCKET socket, bool on)

void NetworkConnection::ResetLastPacketTime()
{
last_packet_time = SDL_GetTicks();
_lastPacketTime = SDL_GetTicks();
}

bool NetworkConnection::ReceivedPacketRecently()
{
#ifndef DEBUG
if (SDL_TICKS_PASSED(SDL_GetTicks(), last_packet_time + 7000))
if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastPacketTime + 7000))
{
return false;
}
#endif
return true;
}

const utf8 * NetworkConnection::getLastDisconnectReason() const
const utf8 * NetworkConnection::GetLastDisconnectReason() const
{
return this->last_disconnect_reason;
return this->_lastDisconnectReason;
}

void NetworkConnection::setLastDisconnectReason(const utf8 * src)
void NetworkConnection::SetLastDisconnectReason(const utf8 * src)
{
if (src == nullptr)
{
if (last_disconnect_reason)
if (_lastDisconnectReason)
{
delete[] last_disconnect_reason;
last_disconnect_reason = nullptr;
delete[] _lastDisconnectReason;
_lastDisconnectReason = nullptr;
}
return;
}

if (last_disconnect_reason == nullptr)
if (_lastDisconnectReason == nullptr)
{
last_disconnect_reason = new utf8[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
_lastDisconnectReason = new utf8[NETWORK_DISCONNECT_REASON_BUFFER_SIZE];
}
String::Set(last_disconnect_reason, NETWORK_DISCONNECT_REASON_BUFFER_SIZE, src);
String::Set(_lastDisconnectReason, NETWORK_DISCONNECT_REASON_BUFFER_SIZE, src);
}

void NetworkConnection::setLastDisconnectReason(const rct_string_id string_id, void *args)
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);
SetLastDisconnectReason(buffer);
}
29 changes: 15 additions & 14 deletions src/network/NetworkConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class NetworkPlayer;
class NetworkConnection
{
public:
SOCKET socket = INVALID_SOCKET;
NetworkPacket inboundpacket;
NETWORK_AUTH authstatus = NETWORK_AUTH_NONE;
NetworkPlayer * player;
uint32 ping_time = 0;
NetworkKey key;
std::vector<uint8> challenge;
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();
Expand All @@ -45,18 +45,19 @@ class NetworkConnection
void SendQueuedPackets();
bool SetTCPNoDelay(bool on);
bool SetNonBlocking(bool on);
static bool SetNonBlocking(SOCKET socket, 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);
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:
utf8 * last_disconnect_reason;
std::list<std::unique_ptr<NetworkPacket>> _outboundPackets;
uint32 _lastPacketTime;
utf8 * _lastDisconnectReason = nullptr;

bool SendPacket(NetworkPacket &packet);
std::list<std::unique_ptr<NetworkPacket>> outboundpackets;
uint32 last_packet_time;
};
38 changes: 19 additions & 19 deletions src/network/NetworkGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

NetworkGroup::NetworkGroup()
{
actions_allowed = { 0 };
ActionsAllowed = { 0 };
}

NetworkGroup::~NetworkGroup()
Expand All @@ -38,10 +38,10 @@ NetworkGroup NetworkGroup::FromJson(const json_t * json)
{
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.actions_allowed.size(); i++) {
group.actions_allowed[i] = 0;
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);
Expand All @@ -60,14 +60,14 @@ NetworkGroup NetworkGroup::FromJson(const json_t * json)
json_t * NetworkGroup::ToJson() const
{
json_t * jsonGroup = json_object();
json_object_set_new(jsonGroup, "id", json_integer(id));
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].permission_name.c_str();
const char * perm_name = NetworkActions::Actions[i].PermissionName.c_str();
json_array_append_new(actionsArray, json_string(perm_name));
}
}
Expand All @@ -77,54 +77,54 @@ json_t * NetworkGroup::ToJson() const

const std::string & NetworkGroup::GetName() const
{
return name;
return _name;
}

void NetworkGroup::SetName(std::string name)
{
NetworkGroup::name = name;
_name = name;
}

void NetworkGroup::Read(NetworkPacket &packet)
{
packet >> id;
packet >> Id;
SetName(packet.ReadString());
for (size_t i = 0; i < actions_allowed.size(); i++)
for (size_t i = 0; i < ActionsAllowed.size(); i++)
{
packet >> actions_allowed[i];
packet >> ActionsAllowed[i];
}
}

void NetworkGroup::Write(NetworkPacket &packet)
{
packet << id;
packet << Id;
packet.WriteString(GetName().c_str());
for (size_t i = 0; i < actions_allowed.size(); i++)
for (size_t i = 0; i < ActionsAllowed.size(); i++)
{
packet << actions_allowed[i];
packet << ActionsAllowed[i];
}
}

void NetworkGroup::ToggleActionPermission(size_t index)
{
size_t byte = index / 8;
size_t bit = index % 8;
if (byte >= actions_allowed.size())
if (byte >= ActionsAllowed.size())
{
return;
}
actions_allowed[byte] ^= (1 << bit);
ActionsAllowed[byte] ^= (1 << bit);
}

bool NetworkGroup::CanPerformAction(size_t index) const
{
size_t byte = index / 8;
size_t bit = index % 8;
if (byte >= actions_allowed.size())
if (byte >= ActionsAllowed.size())
{
return false;
}
return (actions_allowed[byte] & (1 << bit)) ? true : false;
return (ActionsAllowed[byte] & (1 << bit)) ? true : false;
}

bool NetworkGroup::CanPerformCommand(int command) const
Expand Down
Loading

0 comments on commit 5cb81e8

Please sign in to comment.