Fixed protocol messaging. Known crash when recieveing protocol on server after a while...

This commit is contained in:
Dennis Andersen 2014-01-29 15:01:14 +01:00
parent 36f057d2cc
commit 1e89bb4e30
24 changed files with 278 additions and 170 deletions

View File

@ -100,30 +100,6 @@ bool Game::NewFrame()
API::Instance().Update(); API::Instance().Update();
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i]->player) this->players[i]->player->EndFrame();
}
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i]->player) this->players[i]->player->BeginFrame();
}
API::Instance().Update();
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i]->player) this->players[i]->player->EndFrame();
}
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i]->player) this->players[i]->player->BeginFrame();
}
API::Instance().Update();
for (unsigned int i = 0; i < this->players.Size(); i++) for (unsigned int i = 0; i < this->players.Size(); i++)
{ {
if(this->players[i]->player) this->players[i]->player->EndFrame(); if(this->players[i]->player) this->players[i]->player->EndFrame();

View File

@ -39,7 +39,7 @@ namespace GameLogic
Oyster::Math::Float4x4 GetOrientation() override; Oyster::Math::Float4x4 GetOrientation() override;
int GetID() const override; int GetID() const override;
OBJECT_TYPE GetObjectType() const override; OBJECT_TYPE GetObjectType() const override;
void Rotate(const float x, const float y) override; void Rotate(const Oyster::Math3D::Float3 lookDir) override;
Player *player; Player *player;
}; };

View File

@ -79,7 +79,7 @@ namespace GameLogic
* @param x: The relative x axis * @param x: The relative x axis
* @param y: The relative y axis * @param y: The relative y axis
**/ **/
virtual void Rotate(const float x, const float y) = 0; virtual void Rotate(const Oyster::Math3D::Float3 lookDir) = 0;
/******************************************************** /********************************************************
* Uses the chosen players weapon based on input * Uses the chosen players weapon based on input

View File

@ -49,7 +49,7 @@ OBJECT_TYPE Game::PlayerData::GetObjectType() const
{ {
return this->player->GetType(); return this->player->GetType();
} }
void Game::PlayerData::Rotate(const float x, const float y) void Game::PlayerData::Rotate(const Oyster::Math3D::Float3 lookDir)
{ {
this->player->Rotate(x, y);
} }

View File

@ -155,7 +155,6 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="GeneralProtocols.h" /> <ClInclude Include="GeneralProtocols.h" />
<ClInclude Include="GameplayProtocols.h" />
<ClInclude Include="Protocols.h" /> <ClInclude Include="Protocols.h" />
<ClInclude Include="LobbyProtocols.h" /> <ClInclude Include="LobbyProtocols.h" />
<ClInclude Include="ObjectProtocols.h" /> <ClInclude Include="ObjectProtocols.h" />

View File

@ -1,13 +0,0 @@
#ifndef GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H
#define GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H
#include <vector>
#include <CustomNetProtocol.h>
#include "ProtocolIdentificationID.h"
namespace GameLogic
{
}
#endif // !GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H

View File

@ -156,6 +156,13 @@ namespace GameLogic
list.Reserve(10); list.Reserve(10);
} }
Protocol_LobbyGameData(Oyster::Network::CustomNetProtocol& p)
{
this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData;
this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short;
list.Reserve(10);
}
Oyster::Network::CustomNetProtocol* GetProtocol() override Oyster::Network::CustomNetProtocol* GetProtocol() override
{ {
int a = 1; int a = 1;
@ -186,6 +193,10 @@ namespace GameLogic
this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short;
this->protocol[1].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short;
}
Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p)
{
} }
Oyster::Network::CustomNetProtocol* GetProtocol() override Oyster::Network::CustomNetProtocol* GetProtocol() override
{ {

View File

@ -21,6 +21,10 @@ namespace GameLogic
object_ID = -1; object_ID = -1;
pickup_ID = -1; pickup_ID = -1;
}
Protocol_ObjectPickup(Oyster::Network::CustomNetProtocol& p)
{
} }
Protocol_ObjectPickup(int objectID, short pickupID) Protocol_ObjectPickup(int objectID, short pickupID)
{ {
@ -60,6 +64,10 @@ namespace GameLogic
object_ID = -1; object_ID = -1;
health = 0.0f; health = 0.0f;
}
Protocol_ObjectDamage(Oyster::Network::CustomNetProtocol& p)
{
} }
Protocol_ObjectDamage(int id, float hp) Protocol_ObjectDamage(int id, float hp)
{ {
@ -100,6 +108,10 @@ namespace GameLogic
} }
object_ID = -1; object_ID = -1;
memset(&worldMatrix[0], 0, sizeof(float) * 16); memset(&worldMatrix[0], 0, sizeof(float) * 16);
}
Protocol_ObjectPosition(Oyster::Network::CustomNetProtocol& p)
{
} }
Protocol_ObjectPosition(float m[16], int id) Protocol_ObjectPosition(float m[16], int id)
{ {
@ -148,6 +160,10 @@ namespace GameLogic
} }
object_ID = -1; object_ID = -1;
memset(&worldMatrix[0], 0, sizeof(float) * 16); memset(&worldMatrix[0], 0, sizeof(float) * 16);
}
Protocol_ObjectEnable(Oyster::Network::CustomNetProtocol& p)
{
} }
Protocol_ObjectEnable(float m[16], int id) Protocol_ObjectEnable(float m[16], int id)
{ {
@ -188,6 +204,10 @@ namespace GameLogic
this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[1].type = Oyster::Network::NetAttributeType_Int;
this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float;
}
Protocol_ObjectDisable(Oyster::Network::CustomNetProtocol& p)
{
} }
Protocol_ObjectDisable(int id, float time) Protocol_ObjectDisable(int id, float time)
{ {
@ -229,6 +249,10 @@ namespace GameLogic
{ {
this->protocol[i].type = Oyster::Network::NetAttributeType_Float; this->protocol[i].type = Oyster::Network::NetAttributeType_Float;
} }
}
Protocol_ObjectCreate(Oyster::Network::CustomNetProtocol& p)
{
} }
Protocol_ObjectCreate(float m[16], int id, char *path) Protocol_ObjectCreate(float m[16], int id, char *path)
{ {

View File

@ -32,6 +32,10 @@ namespace GameLogic
this->protocol[2].type = Oyster::Network::NetAttributeType_Bool; this->protocol[2].type = Oyster::Network::NetAttributeType_Bool;
this->protocol[3].type = Oyster::Network::NetAttributeType_Bool; this->protocol[3].type = Oyster::Network::NetAttributeType_Bool;
this->protocol[4].type = Oyster::Network::NetAttributeType_Bool; this->protocol[4].type = Oyster::Network::NetAttributeType_Bool;
}
Protocol_PlayerMovement(Oyster::Network::CustomNetProtocol& p)
{
} }
const Protocol_PlayerMovement& operator=(Oyster::Network::CustomNetProtocol& val) const Protocol_PlayerMovement& operator=(Oyster::Network::CustomNetProtocol& val)
{ {
@ -72,6 +76,10 @@ namespace GameLogic
this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float;
this->protocol[3].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float;
}
Protocol_PlayerLook(Oyster::Network::CustomNetProtocol& p)
{
} }
const Protocol_PlayerLook& operator=(Oyster::Network::CustomNetProtocol& val) const Protocol_PlayerLook& operator=(Oyster::Network::CustomNetProtocol& val)
{ {
@ -108,6 +116,10 @@ namespace GameLogic
this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float;
this->protocol[3].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float;
}
Protocol_PlayerChangeWeapon(Oyster::Network::CustomNetProtocol& p)
{
} }
const Protocol_PlayerChangeWeapon& operator=(Oyster::Network::CustomNetProtocol& val) const Protocol_PlayerChangeWeapon& operator=(Oyster::Network::CustomNetProtocol& val)
{ {
@ -132,6 +144,10 @@ namespace GameLogic
this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short;
this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool;
}
Protocol_PlayerShot(Oyster::Network::CustomNetProtocol& p)
{
} }
const Protocol_PlayerShot& operator=(Oyster::Network::CustomNetProtocol& val) const Protocol_PlayerShot& operator=(Oyster::Network::CustomNetProtocol& val)
{ {

View File

@ -5,6 +5,5 @@
#include "PlayerProtocols.h" #include "PlayerProtocols.h"
#include "LobbyProtocols.h" #include "LobbyProtocols.h"
#include "GeneralProtocols.h" #include "GeneralProtocols.h"
#include "GameplayProtocols.h"
#endif // !GAMEPROTOCOLS_GAMEPROTOCOLS_H #endif // !GAMEPROTOCOLS_GAMEPROTOCOLS_H

View File

@ -29,11 +29,11 @@ namespace DanBias
void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Text: void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Text:
void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create: void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create:
void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start: void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start:
void LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join:
void LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login:
void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh:
void LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: void LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData:
void LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData:
private: private:
void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override; void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override;

View File

@ -16,6 +16,7 @@
#include <Queue.h> #include <Queue.h>
#include <NetworkSession.h> #include <NetworkSession.h>
#include <DynamicArray.h> #include <DynamicArray.h>
#include <Protocols.h>
namespace DanBias namespace DanBias
@ -55,11 +56,9 @@ namespace DanBias
//Private member functions //Private member functions
private: private:
//Handles all events recieved // TODO: find out what this method does..
//void ParseEvents();
void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override; void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override;
void ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c);
//Sends a client to the owner, if obj is NULL then all clients is sent //Sends a client to the owner, if obj is NULL then all clients is sent
void SendToOwner(DanBias::GameClient* obj); void SendToOwner(DanBias::GameClient* obj);
@ -67,6 +66,22 @@ namespace DanBias
//Frame function, derived from IThreadObject //Frame function, derived from IThreadObject
bool DoWork ( ) override; bool DoWork ( ) override;
private:
void ParseProtocol (Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c);
void Gameplay_PlayerMovement ( GameLogic::Protocol_PlayerMovement& p, DanBias::GameClient* c );
void Gameplay_PlayerLookDir ( GameLogic::Protocol_PlayerLook& p, DanBias::GameClient* c );
void Gameplay_PlayerChangeWeapon ( GameLogic::Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c );
void Gameplay_PlayerShot ( GameLogic::Protocol_PlayerShot& p, DanBias::GameClient* c );
void Gameplay_ObjectPickup ( GameLogic::Protocol_ObjectPickup& p, DanBias::GameClient* c );
void Gameplay_ObjectDamage ( GameLogic::Protocol_ObjectDamage& p, DanBias::GameClient* c );
void Gameplay_ObjectPosition ( GameLogic::Protocol_ObjectPosition& p, DanBias::GameClient* c );
void Gameplay_ObjectEnabled ( GameLogic::Protocol_ObjectEnable& p, DanBias::GameClient* c );
void Gameplay_ObjectDisabled ( GameLogic::Protocol_ObjectDisable& p, DanBias::GameClient* c );
void Gameplay_ObjectCreate ( GameLogic::Protocol_ObjectCreate& p, DanBias::GameClient* c );
void General_Status ( GameLogic::Protocol_General_Status& p, DanBias::GameClient* c );
void General_Text ( GameLogic::Protocol_General_Text& p, DanBias::GameClient* c );
//Private member variables //Private member variables
private: private:
Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<GameClient>> clients; Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<GameClient>> clients;
@ -78,6 +93,7 @@ namespace DanBias
bool isRunning; bool isRunning;
Utility::WinTimer timer; Utility::WinTimer timer;
//Callback method recieving from gamelogic
static void ObjectMove(GameLogic::IObjectData* movedObject); static void ObjectMove(GameLogic::IObjectData* movedObject);

View File

@ -4,6 +4,7 @@
#include "..\GameLobby.h" #include "..\GameLobby.h"
#include <PlayerProtocols.h> #include <PlayerProtocols.h>
#include <PostBox\PostBox.h> #include <PostBox\PostBox.h>
#include <Protocols.h>
using namespace Utility::DynamicMemory; using namespace Utility::DynamicMemory;
using namespace Oyster::Network; using namespace Oyster::Network;
@ -24,6 +25,9 @@ namespace DanBias
void GameLobby::Update() void GameLobby::Update()
{ {
if(GetAsyncKeyState(VK_DOWN))
this->Send(*GameLogic::Protocol_General_Status().GetProtocol());
this->ProcessClients(); this->ProcessClients();
} }
GameLobby::operator bool() GameLobby::operator bool()
@ -40,15 +44,19 @@ namespace DanBias
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve: case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve:
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
e.sender->Disconnect();
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
this->ParseProtocol(e.args.data.protocol, e.sender); this->ParseProtocol(e.args.data.protocol, e.sender);
break; break;
} }
} }
void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client) void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client)
{ {
//Attach(client); printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str());
Attach(client);
} }
}//End namespace DanBias }//End namespace DanBias

View File

@ -9,30 +9,23 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie
{ {
switch (p[0].value.netShort) switch (p[0].value.netShort)
{ {
//LobbyStartGame(GameLogic::Protocol_LobbyStartGame(p), c); case protocol_General_Status: this->GeneralStatus (Protocol_General_Status (p), c);
//LobbyRefresh(GameLogic::Protocol_LobbyRefresh(p), c);
//LobbyLogin(GameLogic::Protocol_LobbyLogin(p), c);
//LobbyJoin(GameLogic::Protocol_LobbyJoin(p), c);
//GeneralStatus(GameLogic::Protocol_General_Status(p), c);
//GeneralText(GameLogic::Protocol_General_Text(p), c);
case protocol_General_Status:
break; break;
case protocol_General_Text: case protocol_General_Text: this->GeneralText (Protocol_General_Text (p), c);
break; break;
case protocol_Lobby_Create: case protocol_Lobby_Create: this->LobbyCreateGame (Protocol_LobbyCreateGame (p), c);
break; break;
case protocol_Lobby_Start: case protocol_Lobby_Start: this->LobbyStartGame (Protocol_LobbyStartGame (p), c);
break; break;
case protocol_Lobby_Join: case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c);
break; break;
case protocol_Lobby_Login: case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyLogin (p), c);
break; break;
case protocol_Lobby_Refresh: case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c);
break; break;
case protocol_Lobby_MainData: case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyMainData (p), c);
break; break;
case protocol_Lobby_GameData: case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c);
break; break;
} }
} }
@ -69,7 +62,7 @@ void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Ne
{ {
} }
void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c)
{ {
//for (unsigned int i = 0; i < this->gameLobby.Size(); i++) //for (unsigned int i = 0; i < this->gameLobby.Size(); i++)
//{ //{
@ -80,7 +73,7 @@ void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::
// } // }
//} //}
} }
void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c)
{ {
} }
@ -88,11 +81,11 @@ void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Networ
{ {
//Dont need to handle this on the server... //Dont need to handle this on the server...
} }
void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c)
{ {
} }
void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c)
{ {
} }

View File

@ -41,7 +41,7 @@ DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc)
return DanBiasServerReturn_Error; return DanBiasServerReturn_Error;
} }
std::printf("Server created!\t-\t%s: [%i]\n", server.GetLanAddress().c_str(), desc.listenPort); std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort);
return DanBiasServerReturn_Sucess; return DanBiasServerReturn_Sucess;
} }
@ -53,8 +53,7 @@ void GameServerAPI::Start()
while (true) while (true)
{ {
int c = server.ProcessConnectedClients(); server.ProcessConnectedClients();
if(c > 0) printf(" - [%i] client(s) connected!\n", c);
lobby.Update(); lobby.Update();
if(GetAsyncKeyState(0x51)) //Q for exit if(GetAsyncKeyState(0x51)) //Q for exit

View File

@ -45,54 +45,114 @@ namespace DanBias
return this->isRunning; return this->isRunning;
} }
//void GameSession::ParseEvents() void GameSession::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e)
{
}
void GameSession::ObjectMove(GameLogic::IObjectData* movedObject)
{
//GameLogic::IObjectData* obj = NULL;
//if(dynamic_cast<GameLogic::ILevelData*>(movedObject))
// obj =((GameLogic::ILevelData*)movedObject)->GetObjectAt(0);
//if(obj)
//{ //{
// if( !this->box->IsEmpty() ) // if(obj->GetType() == OBJECT_TYPE_BOX)
// { // {
// NetworkSession::NetEvent &e = this->box->Fetch(); // obj->GetID();
// static int ii = 0; // Oyster::Math::Float4x4 world =obj->GetOrientation();
// printf("%i - Message recieved! [%i]\n", ii++, e.protocol[0].value); // Protocol_ObjectPosition p(world, 1);
// // GameSession::gameSession->Send(p.GetProtocol());
// if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return;
//
// if( ProtocolIsGameplay(e.protocol[protocol_INDEX_ID].value.netShort) )
// ParseGameplayEvent(e.protocol, e.gameClient);
//
// if( ProtocolIsGeneral(e.protocol[protocol_INDEX_ID].value.netShort) )
// ParseGeneralEvent(e.protocol, e.gameClient);
// } // }
//} //}
}
//*****************************************************//
//****************** Protocol methods *****************//
//******************************************************************************************************************//
void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c)
{ {
switch (p[protocol_INDEX_ID].value.netShort) switch (p[protocol_INDEX_ID].value.netShort)
{ {
case protocol_Gameplay_PlayerMovement: case protocol_Gameplay_PlayerMovement: this->Gameplay_PlayerMovement ( Protocol_PlayerMovement (p), c );
{ break;
if(p[1].value.netBool) //bool bForward; case protocol_Gameplay_PlayerLookDir: this->Gameplay_PlayerLookDir ( Protocol_PlayerLook (p), c );
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); break;
if(p[2].value.netBool) //bool bBackward; case protocol_Gameplay_PlayerChangeWeapon: this->Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon (p), c );
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); break;
if(p[3].value.netBool) //bool bStrafeLeft; case protocol_Gameplay_PlayerShot: this->Gameplay_PlayerShot ( Protocol_PlayerShot (p), c );
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); break;
if(p[4].value.netBool) //bool bStrafeRight; case protocol_Gameplay_ObjectPickup: this->Gameplay_ObjectPickup ( Protocol_ObjectPickup (p), c );
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); break;
case protocol_Gameplay_ObjectDamage: this->Gameplay_ObjectDamage ( Protocol_ObjectDamage (p), c );
break;
case protocol_Gameplay_ObjectPosition: this->Gameplay_ObjectPosition ( Protocol_ObjectPosition (p), c );
break;
case protocol_Gameplay_ObjectEnabled: this->Gameplay_ObjectEnabled ( Protocol_ObjectEnable (p), c );
break;
case protocol_Gameplay_ObjectDisabled: this->Gameplay_ObjectDisabled ( Protocol_ObjectDisable (p), c );
break;
case protocol_Gameplay_ObjectCreate: this->Gameplay_ObjectCreate ( Protocol_ObjectCreate (p), c );
break;
case protocol_General_Status: this->General_Status ( Protocol_General_Status (p), c );
break;
case protocol_General_Text: this->General_Text ( Protocol_General_Text (p), c );
break;
} }
break;
case protocol_Gameplay_PlayerLookDir:
{
Protocol_PlayerLook m; m = p;
//c->GetPlayer()->Rotate(m.dxMouse, m.dyMouse);
} }
break;
case protocol_Gameplay_PlayerChangeWeapon:
break; void GameSession::Gameplay_PlayerMovement ( Protocol_PlayerMovement& p, DanBias::GameClient* c )
case protocol_Gameplay_ObjectDamage: {
if(p.bForward) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD);
if(p.bBackward) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD);
if(p.bLeft) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT);
if(p.bRight) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT);
}
void GameSession::Gameplay_PlayerLookDir ( Protocol_PlayerLook& p, DanBias::GameClient* c )
{
Oyster::Math3D::Float3 lookDir;
lookDir.x = p.lookDirX;
lookDir.y = p.lookDirY;
lookDir.z = p.lookDirZ;
c->GetPlayer()->Rotate(lookDir);
}
void GameSession::Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c )
{
break; }
case protocol_General_Status: void GameSession::Gameplay_PlayerShot ( Protocol_PlayerShot& p, DanBias::GameClient* c )
switch (p[1].value.netInt) {
c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_PRIMARY_PRESS);
}
void GameSession::Gameplay_ObjectPickup ( Protocol_ObjectPickup& p, DanBias::GameClient* c )
{
}
void GameSession::Gameplay_ObjectDamage ( Protocol_ObjectDamage& p, DanBias::GameClient* c )
{
}
void GameSession::Gameplay_ObjectPosition ( Protocol_ObjectPosition& p, DanBias::GameClient* c )
{
}
void GameSession::Gameplay_ObjectEnabled ( Protocol_ObjectEnable& p, DanBias::GameClient* c )
{
}
void GameSession::Gameplay_ObjectDisabled ( Protocol_ObjectDisable& p, DanBias::GameClient* c )
{
}
void GameSession::Gameplay_ObjectCreate ( Protocol_ObjectCreate& p, DanBias::GameClient* c )
{
}
void GameSession::General_Status ( Protocol_General_Status& p, DanBias::GameClient* c )
{
switch (p.status)
{ {
case GameLogic::Protocol_General_Status::States_disconected: case GameLogic::Protocol_General_Status::States_disconected:
printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID());
@ -111,25 +171,16 @@ namespace DanBias
break; break;
} }
break; }
case protocol_General_Text: void GameSession::General_Text ( Protocol_General_Text& p, DanBias::GameClient* c )
{ {
GameLogic::Protocol_General_Text temp(p); printf("Message recieved from (%i):\t %s\n", c->GetID(), p.text.c_str());
printf("Message recieved from (%i):\t %s\n", c->GetID(), temp.text.c_str());
}
break;
}
} }
void GameSession::ObjectMove(GameLogic::IObjectData* movedObject)
{
movedObject->GetID();
movedObject->GetOrientation();
}
void GameSession::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e)
{
}
}//End namespace DanBias }//End namespace DanBias

View File

@ -171,7 +171,7 @@ void NetworkClient::Update()
} }
} }
bool NetworkClient::Connect(int socket) bool NetworkClient::Connect(ConnectionInfo& socket)
{ {
if(this->IsConnected()) return true; if(this->IsConnected()) return true;
if(this->privateData) return false; if(this->privateData) return false;
@ -250,4 +250,7 @@ void NetworkClient::DataRecieved(NetEvent<NetworkClient*, ClientEventArgs> e)
void NetworkClient::NetworkCallback(Oyster::Network::CustomNetProtocol& p) void NetworkClient::NetworkCallback(Oyster::Network::CustomNetProtocol& p)
{} {}
std::string NetworkClient::GetIpAddress()
{
return this->privateData->connection.GetIpAddress();
}

View File

@ -15,6 +15,7 @@ namespace Oyster
namespace Network namespace Network
{ {
class NetworkSession; class NetworkSession;
struct ConnectionInfo;
extern "C" extern "C"
{ {
@ -54,7 +55,7 @@ namespace Oyster
/** /**
* *
*/ */
bool Connect(int socket); bool Connect(ConnectionInfo& data);
/** /**
* *
@ -102,6 +103,8 @@ namespace Oyster
*/ */
virtual void NetworkCallback(Oyster::Network::CustomNetProtocol& p); virtual void NetworkCallback(Oyster::Network::CustomNetProtocol& p);
virtual std::string GetIpAddress();
private: private:
NetworkClient(const NetworkClient& obj); NetworkClient(const NetworkClient& obj);
NetworkClient& operator =(const NetworkClient& obj); NetworkClient& operator =(const NetworkClient& obj);

View File

@ -77,7 +77,7 @@ public:
public: public:
Listener* listener; Listener* listener;
PostBox<int> postBox; //Postbox for new clients PostBox<ConnectionInfo> postBox; //Postbox for new clients
OysterThread thread; //Server thread OysterThread thread; //Server thread
NetworkSession *mainSession; NetworkSession *mainSession;
Utility::Container::ThreadSafeQueue<SmartPointer<NetworkClient>> clientQueue; Utility::Container::ThreadSafeQueue<SmartPointer<NetworkClient>> clientQueue;
@ -95,9 +95,9 @@ bool NetworkServer::PrivateData::DoWork()
/** Check for new clients **/ /** Check for new clients **/
if(postBox.IsFull()) if(postBox.IsFull())
{ {
int clientSocketNum = postBox.FetchMessage(); ConnectionInfo clientSocketNum = postBox.FetchMessage();
if(clientSocketNum == -1) if(clientSocketNum.socket == -1)
{ {
//Something went wrong somewhere... do we care? //Something went wrong somewhere... do we care?
} }

View File

@ -1,6 +1,7 @@
#include "Connection.h" #include "Connection.h"
#include <winsock2.h> #include <winsock2.h>
#include <WS2tcpip.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fcntl.h> #include <fcntl.h>
@ -40,12 +41,14 @@ Connection::~Connection()
CloseSocket( this->socket ); CloseSocket( this->socket );
} }
int Connection::Connect(int socket, bool blocking) int Connection::Connect(ConnectionInfo info, bool blocking)
{ {
this->socket = socket; this->addr = info.addr;
this->socket = info.socket;
this->stillSending = true; this->stillSending = true;
this->closed = false; this->closed = false;
SetBlockingMode(blocking); SetBlockingMode(blocking);
//connection succesfull! //connection succesfull!
return 0; return 0;
@ -167,17 +170,24 @@ int Connection::Recieve(OysterByte &bytes)
} }
//Listen will only return the correct socket or -1 for failure. //Listen will only return the correct socket or -1 for failure.
int Connection::Listen() ConnectionInfo Connection::Listen()
{ {
if(this->closed) return -1; ConnectionInfo val = { 0 };
if(this->closed) return val;
int clientSocket; SOCKADDR_IN client_info = { 0 };
if((clientSocket = (int)accept(this->socket, NULL, NULL)) == INVALID_SOCKET) int addrsize = sizeof(client_info);
if((val.socket = (int)accept(this->socket, (struct sockaddr*)&client_info, &addrsize)) == INVALID_SOCKET)
{ {
return (int)INVALID_SOCKET;//WSAGetLastError(); val.socket = WSAGetLastError();
}
else
{
val.addr = inet_ntoa(client_info.sin_addr);
} }
return clientSocket; return val;
} }
bool Connection::IsSending() bool Connection::IsSending()
@ -213,6 +223,12 @@ int Connection::SetBlockingMode(bool blocking)
return 0; return 0;
} }
std::string Connection::GetIpAddress()
{
return this->addr;
}
/////////////////////////////////////// ///////////////////////////////////////
//Private functions //Private functions
/////////////////////////////////////// ///////////////////////////////////////

View File

@ -14,7 +14,6 @@ namespace Oyster
{ {
class Connection : public IConnection class Connection : public IConnection
{ {
public: public:
Connection(); Connection();
Connection( int socket ); Connection( int socket );
@ -27,10 +26,10 @@ namespace Oyster
virtual int Recieve( OysterByte &bytes ); virtual int Recieve( OysterByte &bytes );
virtual int Disconnect(); virtual int Disconnect();
virtual int Connect(int socket, bool blocking = false); virtual int Connect(ConnectionInfo info, bool blocking = false);
virtual int Connect(unsigned short port , const char serverName[], bool blocking = false); virtual int Connect(unsigned short port , const char serverName[], bool blocking = false);
virtual int Listen(); virtual ConnectionInfo Listen();
bool IsSending(); bool IsSending();
bool IsConnected(); bool IsConnected();
@ -38,6 +37,8 @@ namespace Oyster
//Setting the socket to blocking/non-blocking mode. //Setting the socket to blocking/non-blocking mode.
int SetBlockingMode( bool blocking ); int SetBlockingMode( bool blocking );
std::string GetIpAddress();
private: private:
int InitiateSocket(); int InitiateSocket();
@ -45,7 +46,7 @@ namespace Oyster
bool stillSending; bool stillSending;
bool closed; bool closed;
std::string addr;
}; };
} }
} }

View File

@ -11,6 +11,12 @@ namespace Oyster
{ {
namespace Network namespace Network
{ {
struct ConnectionInfo
{
int socket;
std::string addr;
};
class OysterByte; class OysterByte;
class IConnection class IConnection
{ {
@ -27,7 +33,7 @@ namespace Oyster
virtual int InitiateClient() { return false; }; virtual int InitiateClient() { return false; };
//Listen function to let client connect, only used by the server //Listen function to let client connect, only used by the server
virtual int Listen() { return -1; }; virtual ConnectionInfo Listen() { return ConnectionInfo(); };
//enables the client to connect with a server with use of name and port //enables the client to connect with a server with use of name and port
//(servers uses Listen instead of connect) //(servers uses Listen instead of connect)

View File

@ -11,7 +11,7 @@ Listener::Listener()
connection = NULL; connection = NULL;
} }
Listener::Listener(Oyster::Network::IPostBox<int>* postBox) Listener::Listener(Oyster::Network::IPostBox<ConnectionInfo>* postBox)
{ {
this->isListening = false; this->isListening = false;
connection = NULL; connection = NULL;
@ -88,7 +88,7 @@ void Listener::Shutdown()
StopListen(); StopListen();
} }
void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox) void Listener::SetPostBox(Oyster::Network::IPostBox<ConnectionInfo>* postBox)
{ {
stdMutex.lock(); stdMutex.lock();
this->postBox = postBox; this->postBox = postBox;
@ -97,21 +97,21 @@ void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
int Listener::Accept() int Listener::Accept()
{ {
int clientSocket = -1; ConnectionInfo clientSocket = {0};
clientSocket = connection->Listen(); clientSocket = connection->Listen();
if(!this->isListening.load()) if(!this->isListening.load())
{ {
return -1; return -1;
} }
if(clientSocket != -1) if(clientSocket.socket != -1)
{ {
stdMutex.lock(); stdMutex.lock();
postBox->PostMessage(clientSocket); postBox->PostMessage(clientSocket);
stdMutex.unlock(); stdMutex.unlock();
} }
return clientSocket; return clientSocket.socket;
} }
void Listener::StopListen() void Listener::StopListen()
{ {

View File

@ -20,7 +20,7 @@ namespace Oyster
{ {
public: public:
Listener(); Listener();
Listener(Oyster::Network::IPostBox<int>* postBox); Listener(Oyster::Network::IPostBox<ConnectionInfo>* postBox);
~Listener(); ~Listener();
bool Init(unsigned int port); bool Init(unsigned int port);
@ -29,7 +29,7 @@ namespace Oyster
void Stop(); void Stop();
void Shutdown(); void Shutdown();
void SetPostBox(IPostBox<int>* postBox); void SetPostBox(IPostBox<ConnectionInfo>* postBox);
private: private:
//Thread functions //Thread functions
@ -48,7 +48,7 @@ namespace Oyster
OysterMutex mutex; OysterMutex mutex;
std::mutex stdMutex; std::mutex stdMutex;
IPostBox<int>* postBox; IPostBox<ConnectionInfo>* postBox;
std::atomic<bool> isListening; std::atomic<bool> isListening;
int port; int port;
}; };