Skip to content

Commit

Permalink
Network cleanup (#6302)
Browse files Browse the repository at this point in the history
* Cleanup network headers

* Move peerhandler to a specific header to reduce compilation times
* Move socket.cpp/h to network folder

* More work

* Network code cleanups

* Move socket.{cpp,h} to network folder
* Move Address object to network/address.{cpp,h}
* Move network exceptions to network/networkexceptions.h
* Client: use unique_ptr for Connection
* Server/ClientIface: use shared_ptr for Connection

* Format fixes

* Remove socket.cpp socket.h from clang-format whitelist

* Also fix NetworkPacket code style & make it under clang-format
  • Loading branch information
nerzhul committed Aug 24, 2017
1 parent 928609c commit c7160cb
Show file tree
Hide file tree
Showing 27 changed files with 1,060 additions and 925 deletions.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -442,7 +442,6 @@ set(common_SRCS
serverlist.cpp
serverobject.cpp
settings.cpp
socket.cpp
sound.cpp
staticobject.cpp
subgame.cpp
Expand Down
33 changes: 22 additions & 11 deletions src/client.cpp
Expand Up @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <IFileSystem.h>
#include "client.h"
#include "network/clientopcodes.h"
#include "network/connection.h"
#include "network/networkpacket.h"
#include "threading/mutex_auto_lock.h"
#include "client/renderingengine.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ Client::Client(
tsrc, this
),
m_particle_manager(&m_env),
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this),
m_con(new con::Connection(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this)),
m_address_name(address_name),
m_server_ser_ver(SER_FMT_VER_INVALID),
m_last_chat_message_sent(time(NULL)),
Expand Down Expand Up @@ -219,7 +220,7 @@ bool Client::isShutdown()
Client::~Client()
{
m_shutdown = true;
m_con.Disconnect();
m_con->Disconnect();

m_mesh_update_thread.stop();
m_mesh_update_thread.wait();
Expand Down Expand Up @@ -253,8 +254,8 @@ void Client::connect(Address address, bool is_local_server)

initLocalMapSaving(address, m_address_name, is_local_server);

m_con.SetTimeoutMs(0);
m_con.Connect(address);
m_con->SetTimeoutMs(0);
m_con->Connect(address);
}

void Client::step(float dtime)
Expand Down Expand Up @@ -787,7 +788,7 @@ void Client::Receive()
{
DSTACK(FUNCTION_NAME);
NetworkPacket pkt;
m_con.Receive(&pkt);
m_con->Receive(&pkt);
ProcessData(&pkt);
}

Expand Down Expand Up @@ -854,7 +855,7 @@ void Client::ProcessData(NetworkPacket *pkt)

void Client::Send(NetworkPacket* pkt)
{
m_con.Send(PEER_ID_SERVER,
m_con->Send(PEER_ID_SERVER,
serverCommandFactoryTable[pkt->getCommand()].channel,
pkt,
serverCommandFactoryTable[pkt->getCommand()].reliable);
Expand Down Expand Up @@ -1297,7 +1298,7 @@ void Client::sendPlayerPos()
u16 our_peer_id;
{
//MutexAutoLock lock(m_con_mutex); //bulk comment-out
our_peer_id = m_con.GetPeerID();
our_peer_id = m_con->GetPeerID();
}

// Set peer id if not set already
Expand All @@ -1319,7 +1320,7 @@ void Client::sendPlayerItem(u16 item)
if(myplayer == NULL)
return;

u16 our_peer_id = m_con.GetPeerID();
u16 our_peer_id = m_con->GetPeerID();

// Set peer id if not set already
if(myplayer->peer_id == PEER_ID_INEXISTENT)
Expand Down Expand Up @@ -1658,6 +1659,16 @@ ClientEvent Client::getClientEvent()
return event;
}

bool Client::connectedToServer()
{
return m_con->Connected();
}

const Address Client::getServerAddress()
{
return m_con->GetPeerAddress(PEER_ID_SERVER);
}

float Client::mediaReceiveProgress()
{
if (m_media_downloader)
Expand Down Expand Up @@ -1768,13 +1779,13 @@ void Client::afterContentReceived()

float Client::getRTT()
{
return m_con.getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
return m_con->getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
}

float Client::getCurRate()
{
return (m_con.getLocalStat(con::CUR_INC_RATE) +
m_con.getLocalStat(con::CUR_DL_RATE));
return (m_con->getLocalStat(con::CUR_INC_RATE) +
m_con->getLocalStat(con::CUR_DL_RATE));
}

void Client::makeScreenshot()
Expand Down
16 changes: 8 additions & 8 deletions src/client.h
Expand Up @@ -19,7 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "network/connection.h"
#include "clientenvironment.h"
#include "irrlichttypes_extrabloated.h"
#include <ostream>
Expand All @@ -36,6 +35,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapnode.h"
#include "tileanimation.h"
#include "mesh_generator_thread.h"
#include "network/address.h"
#include "network/peerhandler.h"
#include <fstream>

#define CLIENT_CHAT_MESSAGE_LIMIT_PER_10S 10.0f
Expand All @@ -57,6 +58,9 @@ class Minimap;
struct MinimapMapblock;
class Camera;
class NetworkPacket;
namespace con {
class Connection;
}

enum LocalClientState {
LC_Created,
Expand Down Expand Up @@ -472,8 +476,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
u8 getProtoVersion()
{ return m_proto_ver; }

bool connectedToServer()
{ return m_con.Connected(); }
bool connectedToServer();

float mediaReceiveProgress();

Expand Down Expand Up @@ -539,10 +542,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void showGameFog(bool show = true);
void showGameDebug(bool show = true);

const Address getServerAddress()
{
return m_con.GetPeerAddress(PEER_ID_SERVER);
}
const Address getServerAddress();

const std::string &getAddressName() const
{
Expand Down Expand Up @@ -611,7 +611,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
MeshUpdateThread m_mesh_update_thread;
ClientEnvironment m_env;
ParticleManager m_particle_manager;
con::Connection m_con;
std::unique_ptr<con::Connection> m_con;
std::string m_address_name;
Camera *m_camera = nullptr;
Minimap *m_minimap = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/client/clientlauncher.cpp
Expand Up @@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "clientlauncher.h"
#include "version.h"
#include "renderingengine.h"
#include "network/networkexceptions.h"

/* mainmenumanager.h
*/
Expand Down
2 changes: 1 addition & 1 deletion src/clientiface.cpp
Expand Up @@ -587,7 +587,7 @@ u64 RemoteClient::uptime() const
return porting::getTimeS() - m_connection_time;
}

ClientInterface::ClientInterface(con::Connection* con)
ClientInterface::ClientInterface(const std::shared_ptr<con::Connection> & con)
:
m_con(con),
m_env(NULL),
Expand Down
4 changes: 2 additions & 2 deletions src/clientiface.h
Expand Up @@ -418,7 +418,7 @@ class ClientInterface {

friend class Server;

ClientInterface(con::Connection* con);
ClientInterface(const std::shared_ptr<con::Connection> &con);
~ClientInterface();

/* run sync step */
Expand Down Expand Up @@ -487,7 +487,7 @@ class ClientInterface {
void UpdatePlayerList();

// Connection
con::Connection* m_con;
std::shared_ptr<con::Connection> m_con;
std::mutex m_clients_mutex;
// Connected clients (behind the con mutex)
RemoteClientMap m_clients;
Expand Down
4 changes: 2 additions & 2 deletions src/httpfetch.cpp
Expand Up @@ -17,15 +17,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "socket.h" // for select()
#include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
#include "httpfetch.h"
#include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
#include <iostream>
#include <sstream>
#include <list>
#include <map>
#include <cerrno>
#include <mutex>
#include "network/socket.h" // for select()
#include "threading/event.h"
#include "config.h"
#include "exceptions.h"
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Expand Up @@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h"
#include "player.h"
#include "porting.h"
#include "network/socket.h"
#if USE_CURSES
#include "terminal_chat_console.h"
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/network/CMakeLists.txt
@@ -1,8 +1,10 @@
set(common_network_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/address.cpp
${CMAKE_CURRENT_SOURCE_DIR}/connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/networkpacket.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serverpackethandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serveropcodes.cpp
${CMAKE_CURRENT_SOURCE_DIR}/socket.cpp
PARENT_SCOPE
)

Expand Down

0 comments on commit c7160cb

Please sign in to comment.