mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-17 20:13:07 +01:00
refactor network, create ITcpSocket
Abstracts all socket code into a new class TcpSocket which is only exposed by a light interface, ITcpSocket. This now means that platform specific headers like winsock2.h and sys/socket.h do not have to be included in OpenRCT2 header files reducing include load and other issues.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
#ifndef DISABLE_NETWORK
|
||||
|
||||
#include "Network.h"
|
||||
#include "NetworkConnection.h"
|
||||
#include "../core/String.hpp"
|
||||
#include <SDL.h>
|
||||
@@ -34,10 +35,7 @@ NetworkConnection::NetworkConnection()
|
||||
|
||||
NetworkConnection::~NetworkConnection()
|
||||
{
|
||||
if (Socket != INVALID_SOCKET)
|
||||
{
|
||||
closesocket(Socket);
|
||||
}
|
||||
delete Socket;
|
||||
if (_lastDisconnectReason)
|
||||
{
|
||||
delete[] _lastDisconnectReason;
|
||||
@@ -49,22 +47,19 @@ 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)
|
||||
void * buffer = &((char*)&InboundPacket.size)[InboundPacket.transferred];
|
||||
size_t bufferLength = sizeof(InboundPacket.size) - InboundPacket.transferred;
|
||||
size_t readBytes;
|
||||
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
|
||||
if (status != NETWORK_READPACKET_SUCCESS)
|
||||
{
|
||||
if (LAST_SOCKET_ERROR() != EWOULDBLOCK && LAST_SOCKET_ERROR() != EAGAIN)
|
||||
{
|
||||
return NETWORK_READPACKET_DISCONNECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NETWORK_READPACKET_NO_DATA;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
InboundPacket.transferred += readBytes;
|
||||
if (InboundPacket.transferred == sizeof(InboundPacket.size))
|
||||
{
|
||||
InboundPacket.size = ntohs(InboundPacket.size);
|
||||
InboundPacket.size = Convert::NetworkToHost(InboundPacket.size);
|
||||
if (InboundPacket.size == 0) // Can't have a size 0 packet
|
||||
{
|
||||
return NETWORK_READPACKET_DISCONNECTED;
|
||||
@@ -77,21 +72,15 @@ int NetworkConnection::ReadPacket()
|
||||
// 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)
|
||||
void * buffer = &InboundPacket.GetData()[InboundPacket.transferred - sizeof(InboundPacket.size)];
|
||||
size_t bufferLength = sizeof(InboundPacket.size) + InboundPacket.size - InboundPacket.transferred;
|
||||
size_t readBytes;
|
||||
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
|
||||
if (status != NETWORK_READPACKET_SUCCESS)
|
||||
{
|
||||
if (LAST_SOCKET_ERROR() != EWOULDBLOCK && LAST_SOCKET_ERROR() != EAGAIN)
|
||||
{
|
||||
return NETWORK_READPACKET_DISCONNECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NETWORK_READPACKET_NO_DATA;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
InboundPacket.transferred += readBytes;
|
||||
}
|
||||
if (InboundPacket.transferred == sizeof(InboundPacket.size) + InboundPacket.size)
|
||||
@@ -105,25 +94,23 @@ int NetworkConnection::ReadPacket()
|
||||
|
||||
bool NetworkConnection::SendPacket(NetworkPacket& packet)
|
||||
{
|
||||
uint16 sizen = htons(packet.size);
|
||||
uint16 sizen = Convert::HostToNetwork(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)
|
||||
|
||||
const void * buffer = &tosend[packet.transferred];
|
||||
size_t bufferSize = tosend.size() - packet.transferred;
|
||||
if (Socket->SendData(buffer, bufferSize))
|
||||
{
|
||||
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;
|
||||
}
|
||||
packet.transferred += bufferSize;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front)
|
||||
@@ -150,27 +137,6 @@ void NetworkConnection::SendQueuedPackets()
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user