Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codechange] Overhauled the network code #991

Merged
merged 6 commits into from Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions source/main/CMakeLists.txt
Expand Up @@ -171,10 +171,6 @@ set( SOURCE_FILES
gui/panels/GUI_VehicleDescription.{h,cpp}
gui/panels/GUI_VehicleDescriptionLayout.{h,cpp}
network/Network.{h,cpp}
network/NetworkStreamManager.{h,cpp}
network/Streamable.{h,cpp}
network/StreamableFactory.h
network/StreamableFactoryInterface.h
physics/ApproxMath.h
physics/Beam.{h,cpp}
physics/BeamData.h
Expand Down
2 changes: 2 additions & 0 deletions source/main/ForwardDeclarations.h
Expand Up @@ -80,7 +80,9 @@ struct hook_t;
struct ground_model_t;
struct client_t;
struct authorinfo_t;
struct header_t;
struct user_info_t;
struct stream_register_t;

namespace MOC
{
Expand Down
4 changes: 2 additions & 2 deletions source/main/GlobalEnvironment.h
Expand Up @@ -33,7 +33,7 @@ class GlobalEnvironment
, frameListener(0)
, mainCamera(0)
, mrTime(0)
, network(0)
, multiplayer(false)
, player(0)
, sceneManager(0)
, sky(0)
Expand All @@ -50,11 +50,11 @@ class GlobalEnvironment
Character *player;
Collisions *collisions;
SurveyMapManager *surveyMap;
Network *network;
RoRFrameListener *frameListener;
SkyManager *sky;
TerrainManager *terrainManager;
ThreadPool *threadPool;

float mrTime;
bool multiplayer;
};
123 changes: 32 additions & 91 deletions source/main/MainThread.cpp
Expand Up @@ -284,53 +284,31 @@ void MainThread::Go()
else
strncpy(gEnv->frameListener->m_screenshot_format, screenshotFormatString.c_str(), 10);

bool enable_network = BSETTING("Network enable", false);
gEnv->multiplayer = BSETTING("Network enable", false);

String preselected_map = SSETTING("Preselected Map", "");

// initiate player colours
PlayerColours::getSingleton();

// you always need that, even if you are not using the network
NetworkStreamManager::getSingleton();

// new factory for characters, net is INVALID, will be set later
new CharacterFactory();
new ChatSystemFactory();

new BeamFactory();

// notice: all factories must be available before starting the network!
#ifdef USE_SOCKETW

if (enable_network)
if (gEnv->multiplayer)
{
RoR::Application::GetContentManager()->AddResourcePack(ContentManager::ResourcePack::MESHES);
RoR::Application::GetContentManager()->AddResourcePack(ContentManager::ResourcePack::MATERIALS);

std::string server_name = SSETTING("Server name", "").c_str();

long server_port = ISETTING("Server port", 1337);

if (server_port==0)
{
ErrorUtils::ShowError(_L("A network error occured"), _L("Bad server port"));
exit(123);
return;
}
LOG("trying to join server '" + String(server_name) + "' on port " + TOSTRING(server_port) + "'...");
RoR::Application::GetContentManager()->AddResourcePack(ContentManager::ResourcePack::FLAGS);
RoR::Application::GetContentManager()->AddResourcePack(ContentManager::ResourcePack::ICONS);

LoadingWindow::getSingleton().setAutotrack(_L("Trying to connect to server ..."));

// important note: all new network code is written in order to allow also the old network protocol to further exist.
// at some point you need to decide with what type of server you communicate below and choose the correct class

gEnv->network = new Network(server_name, server_port);

bool connres = gEnv->network->connect();

LoadingWindow::getSingleton().hide();

new GUI_Multiplayer();
GUI_Multiplayer::getSingleton().update();
bool connres = RoR::Networking::Connect();

if (!connres)
{
Expand All @@ -339,40 +317,26 @@ void MainThread::Go()
//fatal
exit(1);
}
char *terrn = gEnv->network->getTerrainName();
bool isAnyTerrain = (terrn && !strcmp(terrn, "any"));
if (preselected_map.empty() && isAnyTerrain)
{
// so show the terrain selection
preselected_map = "";
}
else if (!isAnyTerrain)
{
preselected_map = getASCIIFromCharString(terrn, 255);
}

// --------------------------------------------------------------------
// network chat stuff
int colourNum = 0;
if (gEnv->network->getLocalUserData())
LoadingWindow::getSingleton().hide();

new GUI_Multiplayer();
GUI_Multiplayer::getSingleton().update();

String terrain_name = RoR::Networking::GetTerrainName();
if (terrain_name != "any")
{
colourNum = gEnv->network->getLocalUserData()->colournum;
preselected_map = terrain_name;
}

ChatSystem* net_chat = ChatSystemFactory::getSingleton().createLocal(colourNum);
RoR::ChatSystem::SendStreamSetup();

// TODO: separate console and chatbox.

Application::GetGuiManager()->SetNetChat(net_chat);
#ifdef USE_MUMBLE
new MumbleIntegration();
#endif // USE_MUMBLE

}
}
#endif //SOCKETW

new BeamFactory();

// ========================================================================
// Main loop (switches application states)
// ========================================================================
Expand Down Expand Up @@ -427,7 +391,7 @@ void MainThread::Go()
SoundScriptManager::getSingleton().trigStart(-1, SS_TRIG_MAIN_MENU);
}

if (gEnv->network != nullptr || BSETTING("SkipMainMenu", false))
if (gEnv->multiplayer || BSETTING("SkipMainMenu", false))
{
// Multiplayer started from configurator / MainMenu disabled -> go directly to map selector (traditional behavior)
RoR::Application::GetGuiManager()->getMainSelector()->Show(LT_Terrain);
Expand All @@ -448,7 +412,7 @@ void MainThread::Go()
// Simulation
// ================================================================

if (SetupGameplayLoop(enable_network, preselected_map))
if (SetupGameplayLoop(preselected_map))
{
previous_application_state = Application::STATE_SIMULATION;
EnterGameplayLoop();
Expand Down Expand Up @@ -488,11 +452,9 @@ void MainThread::Go()
RoR::Application::GetGuiManager()->getMainSelector()->~MainSelector();

#ifdef USE_SOCKETW
if (gEnv->network)
if (gEnv->multiplayer)
{
gEnv->network->disconnect();
delete gEnv->network;
gEnv->network = nullptr;
RoR::Networking::Disconnect();
}
#endif //SOCKETW

Expand Down Expand Up @@ -532,7 +494,7 @@ void MainThread::Go()

}

bool MainThread::SetupGameplayLoop(bool enable_network, Ogre::String preselected_map)
bool MainThread::SetupGameplayLoop(Ogre::String preselected_map)
{
if (!m_base_resource_loaded)
{
Expand Down Expand Up @@ -609,29 +571,19 @@ bool MainThread::SetupGameplayLoop(bool enable_network, Ogre::String preselected
new DustManager(); // setup particle manager singleton. TODO: Move under Application
}

if (enable_network)
int colourNum = -1;
if (gEnv->multiplayer)
{
wchar_t tmp[255] = L"";
UTFString format = _L("Press %ls to start chatting");
swprintf(tmp, 255, format.asWStr_c_str(), ANSI_TO_WCHAR(RoR::Application::GetInputEngine()->getKeyForCommand(EV_COMMON_ENTER_CHATMODE)).c_str());
Application::GetGuiManager()->pushMessageChatBox(UTFString(tmp));

// NOTE: create player _AFTER_ network, important
int colourNum = 0;
if (gEnv->network->getLocalUserData())
{
colourNum = gEnv->network->getLocalUserData()->colournum;
}
gEnv->player = (Character *)CharacterFactory::getSingleton().createLocal(colourNum);
}
else
{
gEnv->player = (Character *)CharacterFactory::getSingleton().createLocal(-1);
if (gEnv->player != nullptr)
{
gEnv->player->setVisible(false);
}
user_info_t info = RoR::Networking::GetLocalUserData();
colourNum = info.colournum;
}
// NOTE: create player _AFTER_ network, important
gEnv->player = CharacterFactory::getSingleton().createLocal(colourNum);

// heathaze effect
if (BSETTING("HeatHaze", false) && RoR::Application::GetContentManager()->isLoaded(ContentManager::ResourcePack::HEATHAZE.mask))
Expand Down Expand Up @@ -934,23 +886,12 @@ void MainThread::MainMenuLoopUpdate(float seconds_since_last_frame)
return;
}

// update GUI
RoR::Application::GetInputEngine()->Capture();

#ifdef USE_SOCKETW
#ifdef USE_MYGUI
// Update network gui every two seconds
if (gEnv->network)
if (gEnv->multiplayer)
{
netcheck_gui_timer += seconds_since_last_frame;
if (netcheck_gui_timer > 2.0f)
{
GUI_Multiplayer::getSingleton().update();
netcheck_gui_timer = 0.0f;
}
GUI_Multiplayer::getSingleton().update();
}
#endif // USE_MYGUI
#endif // USE_SOCKETW

RoR::Application::GetInputEngine()->Capture();

MainMenuLoopUpdateEvents(seconds_since_last_frame);

Expand Down
4 changes: 1 addition & 3 deletions source/main/MainThread.h
Expand Up @@ -70,7 +70,7 @@ class MainThread
/**
* @return True if everything was prepared OK and simulation may start.
*/
bool SetupGameplayLoop(bool enable_network, Ogre::String preselected_map);
bool SetupGameplayLoop(Ogre::String preselected_map);

void UnloadTerrain();

Expand All @@ -97,8 +97,6 @@ class MainThread
Application::State m_application_state;
bool m_base_resource_loaded;

float netcheck_gui_timer;

std::map<std::string, bool> isLoadedMap;
};

Expand Down