Merge with physics
This commit is contained in:
commit
a8055cfbf7
|
@ -9,6 +9,8 @@
|
|||
#include "GameState.h"
|
||||
#include "../Network/NetworkAPI/NetworkClient.h"
|
||||
|
||||
#include <Protocols.h>
|
||||
|
||||
#include "EventHandler\EventHandler.h"
|
||||
#include "Buttons\ButtonRectangle.h"
|
||||
#include "Buttons\TextField.h"
|
||||
|
@ -21,6 +23,7 @@ using namespace ::Oyster;
|
|||
using namespace ::Oyster::Network;
|
||||
using namespace ::Oyster::Event;
|
||||
using namespace ::Oyster::Math3D;
|
||||
using namespace ::GameLogic;
|
||||
|
||||
struct LanMenuState::MyData
|
||||
{
|
||||
|
@ -34,6 +37,9 @@ struct LanMenuState::MyData
|
|||
|
||||
TextField<LanMenuState*> *connectIP;
|
||||
unsigned short connectPort;
|
||||
|
||||
std::string ip;
|
||||
|
||||
} privData;
|
||||
|
||||
void OnButtonInteract_Connect( Oyster::Event::ButtonEvent<LanMenuState*>& e );
|
||||
|
@ -82,6 +88,11 @@ bool LanMenuState::Init( SharedStateContent &shared )
|
|||
|
||||
this->privData->connectPort = 15151;
|
||||
|
||||
if(!this->privData->nwClient->StartListeningForBroadcasting(this->privData->connectPort))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -95,6 +106,8 @@ GameClientState::ClientState LanMenuState::Update( float deltaTime )
|
|||
|
||||
EventHandler::Instance().Update( mouseState );
|
||||
|
||||
this->privData->nwClient->Update();
|
||||
|
||||
return this->privData->nextState;
|
||||
}
|
||||
|
||||
|
@ -116,6 +129,11 @@ bool LanMenuState::Render( )
|
|||
|
||||
bool LanMenuState::Release()
|
||||
{
|
||||
if(privData)
|
||||
{
|
||||
this->privData->nwClient->StopListeningForBroadcasting();
|
||||
}
|
||||
|
||||
privData = NULL;
|
||||
return true;
|
||||
}
|
||||
|
@ -156,3 +174,41 @@ void OnButtonInteract_Exit( Oyster::Event::ButtonEvent<LanMenuState*>& e )
|
|||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
const GameClientState::NetEvent& LanMenuState::DataRecieved( const NetEvent &message )
|
||||
{
|
||||
if( message.args.type == NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend )
|
||||
{ // TODO: Reconnect
|
||||
const char *breakpoint = "temp trap";
|
||||
this->privData->nwClient->Disconnect();
|
||||
this->ChangeState( GameClientState::ClientState_Main );
|
||||
}
|
||||
|
||||
// fetching the id data.
|
||||
short ID = message.args.data.protocol[0].value.netShort;
|
||||
|
||||
CustomNetProtocol data = message.args.data.protocol;
|
||||
|
||||
switch(ID)
|
||||
{
|
||||
case protocol_Broadcast_Test:
|
||||
{
|
||||
Protocol_Broadcast_Test decoded(data);
|
||||
|
||||
unsigned short port = decoded.port;
|
||||
std::string ip = decoded.ip;
|
||||
std::string name = decoded.name;
|
||||
printf("Broadcast message: %d: %s: %s\n", port, ip.c_str(), name.c_str());
|
||||
|
||||
//this->privData->connectPort = port;
|
||||
//this->privData->ip = ip;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return message;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ namespace DanBias
|
|||
virtual bool Render();
|
||||
virtual bool Release();
|
||||
void ChangeState( ClientState next );
|
||||
|
||||
virtual const NetEvent & DataRecieved( const NetEvent &message );
|
||||
|
||||
private:
|
||||
struct MyData;
|
||||
|
|
|
@ -65,20 +65,17 @@ void Player::initPlayerData()
|
|||
|
||||
void Player::BeginFrame()
|
||||
{
|
||||
if( this->playerState != PLAYER_STATE_DEAD && PLAYER_STATE_DIED)
|
||||
if( this->playerState != PLAYER_STATE_DEAD && this->playerState != PLAYER_STATE_DIED)
|
||||
{
|
||||
weapon->Update(0.002f);
|
||||
|
||||
Oyster::Math::Float maxSpeed = 30;
|
||||
|
||||
// Rotate player accordingly
|
||||
this->rigidBody->AddRotationAroundY(this->rotationUp);
|
||||
this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized());
|
||||
Oyster::Math::Quaternion firstUp = this->rigidBody->GetState().quaternion;
|
||||
this->rigidBody->SetRotationAsAngularAxis(Oyster::Math3D::Float4(this->rigidBody->GetState().centerPos.GetNormalized(), this->rotationUp));
|
||||
Oyster::Math::Quaternion secondTurn = this->rigidBody->GetState().quaternion;
|
||||
this->rotationUp = 0;
|
||||
|
||||
this->rigidBody->SetRotation(secondTurn*firstUp);
|
||||
|
||||
// Direction data
|
||||
Oyster::Math::Float4x4 xform;
|
||||
xform = this->rigidBody->GetState().GetOrientation();
|
||||
|
@ -125,7 +122,7 @@ void Player::BeginFrame()
|
|||
}
|
||||
|
||||
// Dampen velocity if certain keys are not pressed
|
||||
if(key_jump <= 0.001 && this->rigidBody->GetLambda() < 0.9f)
|
||||
if(key_jump <= 0.001 && IsWalking())
|
||||
{
|
||||
if(key_forward <= 0.001 && key_backward <= 0.001)
|
||||
{
|
||||
|
@ -152,7 +149,7 @@ void Player::BeginFrame()
|
|||
walkDirection.Normalize();
|
||||
|
||||
// If on the ground, accelerate normally
|
||||
if(this->rigidBody->GetLambda() < 0.9f)
|
||||
if(IsWalking())
|
||||
{
|
||||
if(forwardSpeed < maxSpeed)
|
||||
{
|
||||
|
@ -164,7 +161,7 @@ void Player::BeginFrame()
|
|||
}
|
||||
}
|
||||
// If in the air, accelerate slower
|
||||
if(this->rigidBody->GetLambda() >= 0.9f)
|
||||
if(IsJumping())
|
||||
{
|
||||
if(forwardSpeed < maxSpeed)
|
||||
{
|
||||
|
@ -193,8 +190,8 @@ void Player::BeginFrame()
|
|||
//Jump
|
||||
if(key_jump > 0.001)
|
||||
{
|
||||
this->key_jump -= this->gameInstance->GetFrameTime();
|
||||
if(this->rigidBody->GetLambda() < 0.9f)
|
||||
this->key_jump -= this->gameInstance->GetFrameTime();
|
||||
if(IsWalking())
|
||||
{
|
||||
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos.GetNormalized();
|
||||
this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass * 20);
|
||||
|
@ -306,15 +303,15 @@ void Player::Jump()
|
|||
|
||||
bool Player::IsWalking()
|
||||
{
|
||||
return (this->playerState == PLAYER_STATE::PLAYER_STATE_WALKING);
|
||||
return (this->rigidBody->GetLambda() < 0.99f);
|
||||
}
|
||||
bool Player::IsJumping()
|
||||
{
|
||||
return (this->playerState == PLAYER_STATE::PLAYER_STATE_JUMPING);
|
||||
return (this->rigidBody->GetLambda() == 1.0f);
|
||||
}
|
||||
bool Player::IsIdle()
|
||||
{
|
||||
return (this->playerState == PLAYER_STATE::PLAYER_STATE_IDLE);
|
||||
return (this->rigidBody->GetLambda() == 1.0f && this->rigidBody->GetLinearVelocity().GetMagnitude() < 0.0001f);
|
||||
}
|
||||
|
||||
void Player::Inactivate()
|
||||
|
|
|
@ -72,5 +72,50 @@ namespace GameLogic
|
|||
Oyster::Network::CustomNetProtocol protocol;
|
||||
};
|
||||
|
||||
//#define protocol_Broadcast_Test 102
|
||||
struct Protocol_Broadcast_Test :public Oyster::Network::CustomProtocolObject
|
||||
{
|
||||
unsigned short port;
|
||||
std::string ip;
|
||||
std::string name;
|
||||
|
||||
Protocol_Broadcast_Test()
|
||||
{
|
||||
this->protocol[0].type = Oyster::Network::NetAttributeType_Short;
|
||||
this->protocol[0].value.netShort = protocol_Broadcast_Test;
|
||||
this->protocol[1].type = Oyster::Network::NetAttributeType_UnsignedShort;
|
||||
this->protocol[2].type = Oyster::Network::NetAttributeType_CharArray;
|
||||
this->protocol[3].type = Oyster::Network::NetAttributeType_CharArray;
|
||||
this->port = 0;
|
||||
}
|
||||
Protocol_Broadcast_Test(unsigned short port, std::string ip, std::string name)
|
||||
{
|
||||
this->protocol[0].type = Oyster::Network::NetAttributeType_Short;
|
||||
this->protocol[0].value.netShort = protocol_Broadcast_Test;
|
||||
this->protocol[1].type = Oyster::Network::NetAttributeType_UnsignedShort;
|
||||
this->port = port;
|
||||
this->protocol[2].type = Oyster::Network::NetAttributeType_CharArray;
|
||||
this->ip = ip;
|
||||
this->protocol[3].type = Oyster::Network::NetAttributeType_CharArray;
|
||||
this->name = name;
|
||||
}
|
||||
Protocol_Broadcast_Test(Oyster::Network::CustomNetProtocol& p)
|
||||
{
|
||||
this->port = p[1].value.netUShort;
|
||||
this->ip.assign(p[2].value.netCharPtr);
|
||||
this->name.assign(p[3].value.netCharPtr);
|
||||
}
|
||||
Oyster::Network::CustomNetProtocol GetProtocol() override
|
||||
{
|
||||
this->protocol[1].value = this->port;
|
||||
this->protocol.Set(2, ip);
|
||||
this->protocol.Set(3, name);
|
||||
|
||||
return protocol;
|
||||
}
|
||||
|
||||
private:
|
||||
Oyster::Network::CustomNetProtocol protocol;
|
||||
};
|
||||
}
|
||||
#endif //!GAMELOGIC_CONTROL_PROTOCOLS_H
|
|
@ -20,6 +20,7 @@
|
|||
#define protocol_GeneralMIN 100
|
||||
#define protocol_General_Status 100
|
||||
#define protocol_General_Text 101
|
||||
#define protocol_Broadcast_Test 102
|
||||
#define protocol_GeneralMAX 199
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include "..\GameLobby.h"
|
||||
#include "..\GameSession.h"
|
||||
|
||||
#include "..\GameProtocols\GeneralProtocols.h"
|
||||
|
||||
#include <NetworkServer.h>
|
||||
#include <NetworkClient.h>
|
||||
|
||||
|
@ -18,6 +20,9 @@
|
|||
#include <WinTimer.h>
|
||||
#include <thread>
|
||||
|
||||
//For conversion from wstring to string
|
||||
#include <codecvt>
|
||||
|
||||
using namespace DanBias;
|
||||
using namespace Oyster::Network;
|
||||
using namespace Oyster::Thread;
|
||||
|
@ -41,12 +46,26 @@ namespace
|
|||
}
|
||||
|
||||
|
||||
std::string wstring_to_utf8 (const std::wstring& str)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
|
||||
return myconv.to_bytes(str);
|
||||
}
|
||||
|
||||
DanBiasServerReturn GameServerAPI::ServerInitiate(const ServerInitDesc& desc)
|
||||
{
|
||||
ServerOptions opt;
|
||||
opt.mainOptions.listenPort = desc.listenPort;
|
||||
opt.mainOptions.ownerSession = &lobby;
|
||||
|
||||
std::string serverName = wstring_to_utf8(desc.serverName);
|
||||
|
||||
GameLogic::Protocol_Broadcast_Test broadcastMessage(opt.mainOptions.listenPort, "127.0.0.1", serverName);
|
||||
|
||||
opt.broadcastOptions.broadcast = true;
|
||||
opt.broadcastOptions.broadcastInterval = 1.0f;
|
||||
opt.broadcastOptions.broadcastMessage = broadcastMessage.GetProtocol();
|
||||
|
||||
if(server.Init(opt) == NetworkServer::ServerReturnCode_Error)
|
||||
{
|
||||
return DanBiasServerReturn_Error;
|
||||
|
@ -54,7 +73,10 @@ DanBiasServerReturn GameServerAPI::ServerInitiate(const ServerInitDesc& desc)
|
|||
GameSession::GameDescription d;
|
||||
|
||||
std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort);
|
||||
|
||||
|
||||
GameLogic::Protocol_Broadcast_Test broadcastMessage2(opt.mainOptions.listenPort, server.GetLanAddress(), serverName);
|
||||
server.SetBroadcastMessage(broadcastMessage2.GetProtocol());
|
||||
|
||||
return DanBiasServerReturn_Sucess;
|
||||
}
|
||||
void GameServerAPI::ServerStart()
|
||||
|
|
|
@ -193,8 +193,11 @@ using namespace Utility::DynamicMemory;
|
|||
if(w->msec > 0)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(w->msec));
|
||||
|
||||
while (w->state == OYSTER_THREAD_STATE_IDLE)
|
||||
while (w->state == OYSTER_THREAD_STATE_IDLE)
|
||||
{
|
||||
CheckPriority(w);
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
static void ThreadingFunction(ThreadData* w)
|
||||
{
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "CustomNetProtocol.h"
|
||||
#include "NetworkSession.h"
|
||||
|
||||
#include "../NetworkDependencies/ConnectionUDP.h"
|
||||
#include "../NetworkDependencies/Connection.h"
|
||||
#include "../NetworkDependencies/PostBox.h"
|
||||
#include "../NetworkDependencies/WinsockFunctions.h"
|
||||
|
@ -25,6 +26,8 @@
|
|||
#include <queue>
|
||||
#include <WinSock2.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
//For conversion from wstring to string
|
||||
#include <codecvt>
|
||||
|
||||
|
@ -60,6 +63,15 @@ struct NetworkClient::PrivateData : public IThreadObject
|
|||
HANDLE socketEvents[2];
|
||||
HANDLE shutdownEvent;
|
||||
|
||||
//Broadcasting
|
||||
bool broadcastingStarted;
|
||||
HANDLE broadcastEvent;
|
||||
HANDLE broadcastShutdownEvent;
|
||||
std::thread broadcastThread;
|
||||
ConnectionUDP broadcastConnection;
|
||||
OysterByte broadcastTempMessage;
|
||||
Translator broadcastTranslator;
|
||||
|
||||
//The OysterByte each message is packed in.
|
||||
OysterByte tempMessage;
|
||||
|
||||
|
@ -77,6 +89,7 @@ struct NetworkClient::PrivateData : public IThreadObject
|
|||
, owner(0)
|
||||
, outputEvent(0)
|
||||
{
|
||||
broadcastingStarted = false;
|
||||
numPackages = 0;
|
||||
bufferedSend.Resize(MAX_NETWORK_MESSAGE_SIZE);
|
||||
tempMessage.Resize(MAX_NETWORK_MESSAGE_SIZE);
|
||||
|
@ -120,6 +133,26 @@ struct NetworkClient::PrivateData : public IThreadObject
|
|||
CloseHandle(shutdownEvent);
|
||||
}
|
||||
|
||||
//Thread for receiving broadcast messages
|
||||
void BroadcastThread()
|
||||
{
|
||||
WSANETWORKEVENTS wsaEvents;
|
||||
|
||||
while(WaitForSingleObject(broadcastShutdownEvent, 0) != WAIT_OBJECT_0)
|
||||
{
|
||||
int result = WSAWaitForMultipleEvents(1, &broadcastEvent, FALSE, 100, FALSE) - WSA_WAIT_EVENT_0;
|
||||
if(result == 0)
|
||||
{
|
||||
WSAEnumNetworkEvents(this->broadcastConnection.GetSocket(), broadcastEvent, &wsaEvents);
|
||||
if((wsaEvents.lNetworkEvents & FD_READ) && (wsaEvents.iErrorCode[FD_READ_BIT] == 0))
|
||||
{
|
||||
//Recieve a message
|
||||
RecvUDP();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DoWork() override
|
||||
{
|
||||
WSANETWORKEVENTS wsaEvents;
|
||||
|
@ -128,7 +161,7 @@ struct NetworkClient::PrivateData : public IThreadObject
|
|||
{
|
||||
if(!this->connection.IsConnected()) return false;
|
||||
|
||||
int result = WSAWaitForMultipleEvents(2, socketEvents, FALSE, 100, FALSE) - WSA_WAIT_EVENT_0;
|
||||
int result = WSAWaitForMultipleEvents(3, socketEvents, FALSE, 100, FALSE) - WSA_WAIT_EVENT_0;
|
||||
if(result == 0)
|
||||
{
|
||||
WSAEnumNetworkEvents(this->connection.GetSocket(), socketEvents[0], &wsaEvents);
|
||||
|
@ -161,6 +194,47 @@ struct NetworkClient::PrivateData : public IThreadObject
|
|||
return false;
|
||||
}
|
||||
|
||||
void RecvUDP()
|
||||
{
|
||||
int errorCode = -1;
|
||||
|
||||
errorCode = this->broadcastConnection.Recieve(broadcastTempMessage);
|
||||
|
||||
if(errorCode == 0 && broadcastTempMessage.GetSize())
|
||||
{
|
||||
CustomNetProtocol protocol;
|
||||
bool ok = this->broadcastTranslator.Unpack(protocol, broadcastTempMessage);
|
||||
|
||||
//Check if the protocol was unpacked correctly
|
||||
if(ok)
|
||||
{
|
||||
CEA parg;
|
||||
parg.type = CEA::EventType_ProtocolRecieved;
|
||||
parg.data.protocol = protocol;
|
||||
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e;
|
||||
e.sender = this->parent;
|
||||
e.args.data.protocol = parg.data.protocol;
|
||||
e.args.type = parg.type;
|
||||
|
||||
this->recieveQueue.Push(e);
|
||||
|
||||
if(this->outputEvent)
|
||||
{
|
||||
printf("\t(ID: %i | IP: %s | Protocol: %i) Message recieved!\n", this->ID, this->connection.GetIpAddress().c_str(), protocol[0].value.netShort);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this->outputEvent)
|
||||
{
|
||||
printf("\t(ID: %i | IP: %s) Failed to unpack CustomNetProtocol!\n", this->ID, this->connection.GetIpAddress().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
broadcastTempMessage.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
void SendBuffer()
|
||||
{
|
||||
if(bufferedSend.GetSize() > 0)
|
||||
|
@ -482,7 +556,7 @@ bool NetworkClient::Connect(unsigned short port, const char serverIP[])
|
|||
|
||||
if(!this->privateData)
|
||||
this->privateData = new PrivateData();
|
||||
|
||||
|
||||
int result = this->privateData->connection.Connect(port, serverIP, true);
|
||||
|
||||
//Connect has succeeded
|
||||
|
@ -598,4 +672,86 @@ std::string NetworkClient::GetIpAddress()
|
|||
void NetworkClient::OutputEventData(bool output)
|
||||
{
|
||||
this->privateData->outputEvent;
|
||||
}
|
||||
}
|
||||
|
||||
bool NetworkClient::StartListeningForBroadcasting(unsigned short port)
|
||||
{
|
||||
//Create privateData if it doesn't exists.
|
||||
if(this->privateData == NULL)
|
||||
{
|
||||
privateData = new PrivateData;
|
||||
}
|
||||
|
||||
//Initiate broadcasting only if it's not started.
|
||||
if(!this->privateData->broadcastingStarted)
|
||||
{
|
||||
//Create UDP connection
|
||||
int result = this->privateData->broadcastConnection.InitiateBroadcastClient(port);
|
||||
if(result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Create event for closing the thread.
|
||||
this->privateData->broadcastShutdownEvent = CreateEvent(NULL, true, false, NULL);
|
||||
if(this->privateData->broadcastShutdownEvent == NULL)
|
||||
{
|
||||
this->privateData->broadcastConnection.Disconnect();
|
||||
return false;
|
||||
}
|
||||
|
||||
//Creating event for broadcast messages on the UDP connection.
|
||||
this->privateData->broadcastEvent = WSACreateEvent();
|
||||
if(this->privateData->broadcastEvent == WSA_INVALID_EVENT)
|
||||
{
|
||||
this->privateData->broadcastConnection.Disconnect();
|
||||
CloseHandle(this->privateData->broadcastShutdownEvent);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Set the event for only Receiving.
|
||||
if(WSAEventSelect(this->privateData->broadcastConnection.GetSocket(), this->privateData->broadcastEvent, FD_READ) == SOCKET_ERROR)
|
||||
{
|
||||
this->privateData->broadcastConnection.Disconnect();
|
||||
CloseHandle(this->privateData->broadcastShutdownEvent);
|
||||
WSACloseEvent(this->privateData->broadcastEvent);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Start thread receiving broadcast messages.
|
||||
this->privateData->broadcastThread = thread(&PrivateData::BroadcastThread, this->privateData);
|
||||
if(!this->privateData->broadcastThread.joinable())
|
||||
{
|
||||
this->privateData->broadcastConnection.Disconnect();
|
||||
CloseHandle(this->privateData->broadcastShutdownEvent);
|
||||
WSACloseEvent(this->privateData->broadcastEvent);
|
||||
return false;
|
||||
}
|
||||
|
||||
this->privateData->broadcastingStarted = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NetworkClient::StopListeningForBroadcasting()
|
||||
{
|
||||
if(this->privateData)
|
||||
{
|
||||
if(this->privateData->broadcastingStarted)
|
||||
{
|
||||
//Tell the thread to shutdown
|
||||
WSASetEvent(this->privateData->broadcastShutdownEvent);
|
||||
|
||||
//Wait for thread
|
||||
this->privateData->broadcastThread.join();
|
||||
|
||||
WSACloseEvent(this->privateData->broadcastEvent);
|
||||
CloseHandle(this->privateData->broadcastShutdownEvent);
|
||||
|
||||
this->privateData->broadcastConnection.Disconnect();
|
||||
|
||||
this->privateData->broadcastingStarted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -153,6 +153,19 @@ namespace Oyster
|
|||
*/
|
||||
void SetOutputEventDataStream(FILE out);
|
||||
|
||||
|
||||
/** Starts a seperate thread for listening to broadcast messages.
|
||||
* @param port: The port to listen for messages on.
|
||||
* @return Returns true if broadcasting is already started or if it started now.
|
||||
Returns false if it failed to start broadcasting.
|
||||
*/
|
||||
bool StartListeningForBroadcasting(unsigned short port);
|
||||
|
||||
/** Stops the seperate thread for broadcast messages.
|
||||
*
|
||||
*/
|
||||
void StopListeningForBroadcasting();
|
||||
|
||||
private:
|
||||
NetworkClient(const NetworkClient& obj);
|
||||
NetworkClient& operator =(const NetworkClient& obj);
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
#include "NetworkServer.h"
|
||||
|
||||
#include "Translator.h"
|
||||
#include "../NetworkDependencies/ConnectionUDP.h"
|
||||
#include "../NetworkDependencies/Listener.h"
|
||||
#include "../NetworkDependencies/PostBox.h"
|
||||
#include "../NetworkDependencies/WinsockFunctions.h"
|
||||
|
@ -18,6 +20,7 @@
|
|||
#include "Thread/OysterThread.h"
|
||||
#include "WinTimer.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
using namespace Oyster::Network;
|
||||
using namespace Utility::DynamicMemory;
|
||||
|
@ -81,7 +84,15 @@ public:
|
|||
, port(-1)
|
||||
, broadcast(0)
|
||||
, broadcastTime(1.0f, 0.0f)
|
||||
{ }
|
||||
, broadcastMutex(new std::mutex)
|
||||
{
|
||||
InitWinSock();
|
||||
serverOptions.broadcastOptions.broadcastInterval = 1.0f;
|
||||
serverOptions.broadcastOptions.broadcast = true;
|
||||
broadcastMessage.Resize(MAX_NETWORK_MESSAGE_SIZE);
|
||||
|
||||
broadcastConnection.InitiateBroadcastServer(15151, "255.255.255.255");
|
||||
}
|
||||
~PrivateData()
|
||||
{
|
||||
if(listener)
|
||||
|
@ -106,6 +117,12 @@ public:
|
|||
int port;
|
||||
bool broadcast;
|
||||
|
||||
ServerOptions serverOptions;
|
||||
|
||||
SmartPointer<std::mutex> broadcastMutex;
|
||||
ConnectionUDP broadcastConnection;
|
||||
OysterByte broadcastMessage;
|
||||
|
||||
TimeInstance broadcastTime;
|
||||
|
||||
Utility::WinTimer serverTimer;
|
||||
|
@ -113,11 +130,18 @@ public:
|
|||
|
||||
bool NetworkServer::PrivateData::DoWork()
|
||||
{
|
||||
if(broadcast)
|
||||
if(serverOptions.broadcastOptions.broadcast)
|
||||
{
|
||||
if( (this->serverTimer.getElapsedSeconds() - this->broadcastTime.previous) >= this->broadcastTime.length )
|
||||
if( (this->serverTimer.getElapsedSeconds() - this->broadcastTime.previous) >= this->serverOptions.broadcastOptions.broadcastInterval )
|
||||
{
|
||||
Broadcast();
|
||||
//broadcastMessage.Clear();
|
||||
//Translator t;
|
||||
//t.Pack(broadcastMessage, serverOptions.broadcastOptions.broadcastMessage);
|
||||
serverTimer.reset();
|
||||
|
||||
broadcastMutex->lock();
|
||||
broadcastConnection.Send(broadcastMessage);
|
||||
broadcastMutex->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -197,6 +221,8 @@ NetworkServer::ServerReturnCode NetworkServer::Init(ServerOptions& options)
|
|||
return NetworkServer::ServerReturnCode_Error;
|
||||
}
|
||||
|
||||
this->privateData->serverOptions.broadcastOptions = options.broadcastOptions;
|
||||
|
||||
this->privateData->isInitiated = true;
|
||||
this->privateData->isReleased = false;
|
||||
return NetworkServer::ServerReturnCode_Sucess;
|
||||
|
@ -319,6 +345,47 @@ int NetworkServer::GetPort()
|
|||
return this->privateData->port;
|
||||
}
|
||||
|
||||
/***************************************
|
||||
Broadcast functions
|
||||
***************************************/
|
||||
//Set broadcast settings.
|
||||
void NetworkServer::SetBroadcast(CustomNetProtocol& broadcastMessage, float interval, bool enable)
|
||||
{
|
||||
this->privateData->broadcastMutex->lock();
|
||||
this->privateData->serverOptions.broadcastOptions.broadcast = enable;
|
||||
this->privateData->serverOptions.broadcastOptions.broadcastMessage = broadcastMessage;
|
||||
this->privateData->serverOptions.broadcastOptions.broadcastInterval = interval;
|
||||
|
||||
this->privateData->broadcastMessage.Clear();
|
||||
Translator t;
|
||||
t.Pack(this->privateData->broadcastMessage, broadcastMessage);
|
||||
this->privateData->broadcastMutex->unlock();
|
||||
}
|
||||
|
||||
//Set broadcast settings.
|
||||
void NetworkServer::SetBroadcastMessage(CustomNetProtocol& broadcastMessage)
|
||||
{
|
||||
this->privateData->broadcastMutex->lock();
|
||||
this->privateData->serverOptions.broadcastOptions.broadcastMessage = broadcastMessage;
|
||||
|
||||
this->privateData->broadcastMessage.Clear();
|
||||
Translator t;
|
||||
t.Pack(this->privateData->broadcastMessage, broadcastMessage);
|
||||
this->privateData->broadcastMutex->unlock();
|
||||
}
|
||||
|
||||
//Enable/disable broadcast.
|
||||
void NetworkServer::SetBroadcast(bool enable)
|
||||
{
|
||||
this->privateData->broadcastMutex->lock();
|
||||
this->privateData->serverOptions.broadcastOptions.broadcast = enable;
|
||||
this->privateData->broadcastMutex->unlock();
|
||||
}
|
||||
|
||||
//Set interval between each broadcast message in seconds.
|
||||
void NetworkServer::SetBroadcastInterval(float interval)
|
||||
{
|
||||
this->privateData->broadcastMutex->lock();
|
||||
this->privateData->serverOptions.broadcastOptions.broadcastInterval = interval;
|
||||
this->privateData->broadcastMutex->unlock();
|
||||
}
|
|
@ -41,8 +41,24 @@ int ConnectionUDP::Connect(unsigned short port, const char serverName[])
|
|||
}
|
||||
|
||||
|
||||
//this->Address = htonl(0xffffffff);
|
||||
this->Address = *(unsigned long*)hostEnt->h_addr;
|
||||
this->port = htons(this->port);
|
||||
|
||||
if(this->Address == 0)
|
||||
{
|
||||
sockaddr_in addr;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = this->port;
|
||||
|
||||
int result = bind(socket, (const sockaddr*)&addr, sizeof(addr));
|
||||
if(result)
|
||||
{
|
||||
int a = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -89,19 +105,46 @@ int ConnectionUDP::InitiateClient()
|
|||
return InitiateSocket();
|
||||
}
|
||||
|
||||
int ConnectionUDP::InitiateBroadcast(unsigned short port)
|
||||
int ConnectionUDP::InitiateBroadcastServer(unsigned short port, const char serverName[])
|
||||
{
|
||||
int result = InitiateSocket();
|
||||
|
||||
|
||||
int flag = 1;
|
||||
result = setsockopt(this->socket, /* socket affected */
|
||||
SOL_SOCKET, /* set option at TCP level */
|
||||
SO_BROADCAST, /* name of option */
|
||||
(char *) &flag, /* the cast is historical cruft */
|
||||
sizeof(flag)); /* length of option value */
|
||||
result = setsockopt(this->socket, SOL_SOCKET, SO_BROADCAST, (char *) &flag, sizeof(flag));
|
||||
if (result < 0)
|
||||
return -1;
|
||||
|
||||
closed = false;
|
||||
stillSending = true;
|
||||
|
||||
struct hostent *hostEnt;
|
||||
if((hostEnt = gethostbyname(serverName)) == NULL)
|
||||
{
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
this->Address = *(unsigned long*)hostEnt->h_addr;
|
||||
this->port = htons(port);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConnectionUDP::InitiateBroadcastClient(unsigned short port)
|
||||
{
|
||||
int result = InitiateSocket();
|
||||
|
||||
struct sockaddr_in recvAddr;
|
||||
recvAddr.sin_family = AF_INET;
|
||||
recvAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
recvAddr.sin_port = htons(port);
|
||||
|
||||
if(bind(this->socket, (sockaddr*)&recvAddr, sizeof(recvAddr)) == SOCKET_ERROR)
|
||||
{
|
||||
closesocket(this->socket);
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int ConnectionUDP::Send(OysterByte &bytes)
|
||||
|
@ -201,6 +244,12 @@ int ConnectionUDP::SetBlockingMode( bool blocking )
|
|||
//Success
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConnectionUDP::GetSocket()
|
||||
{
|
||||
return socket;
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
// Private Methods
|
||||
//////////////////////////////////////
|
||||
|
|
|
@ -22,18 +22,23 @@ namespace Oyster
|
|||
virtual int InitiateServer( unsigned short port );
|
||||
virtual int InitiateClient();
|
||||
|
||||
virtual int InitiateBroadcast(unsigned short port);
|
||||
virtual int InitiateBroadcastServer(unsigned short port, const char serverName[]);
|
||||
virtual int InitiateBroadcastClient(unsigned short port);
|
||||
|
||||
virtual int Send ( OysterByte &byte );
|
||||
virtual int Recieve( OysterByte &byte );
|
||||
|
||||
virtual int Connect( unsigned short port, const char serverName[] );
|
||||
|
||||
//Doesn't do anything now.
|
||||
virtual int Reconnect() { return 0; }
|
||||
|
||||
virtual int Disconnect();
|
||||
|
||||
bool IsSending();
|
||||
bool IsConnected();
|
||||
int GetIpSize();
|
||||
int GetSocket();
|
||||
|
||||
int SetBlockingMode( bool blocking );
|
||||
|
||||
|
|
|
@ -163,30 +163,19 @@ void SimpleRigidBody::SetRotation(::Oyster::Math::Float4x4 rotation)
|
|||
this->state.quaternion = Quaternion(Float3(quaternion.x(), quaternion.y(), quaternion.z()), quaternion.w());
|
||||
}
|
||||
|
||||
void SimpleRigidBody::SetRotationAsAngularAxis(::Oyster::Math::Float4 angularAxis)
|
||||
void SimpleRigidBody::AddRotationAroundY(::Oyster::Math::Float angle)
|
||||
{
|
||||
if(angularAxis.xyz.GetMagnitude() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float s = sin(angularAxis.w/2);
|
||||
float x = angularAxis.x * s;
|
||||
float y = angularAxis.y * s;
|
||||
float z = angularAxis.z * s;
|
||||
float w = cos(angularAxis.w/2);
|
||||
|
||||
btTransform trans;
|
||||
btVector3 vector(angularAxis.x, angularAxis.y, angularAxis.z);
|
||||
btQuaternion quaternion(x,y,z,w);
|
||||
btQuaternion quaternion;
|
||||
|
||||
trans = this->rigidBody->getWorldTransform();
|
||||
trans.setRotation(quaternion);
|
||||
|
||||
quaternion = btQuaternion(trans.getBasis().getColumn(1), angle);
|
||||
trans.setRotation(quaternion*trans.getRotation());
|
||||
this->rigidBody->setWorldTransform(trans);
|
||||
|
||||
this->state.quaternion = Quaternion(Float3(quaternion.x(), quaternion.y(), quaternion.z()), quaternion.w());
|
||||
}
|
||||
|
||||
void SimpleRigidBody::SetAngularFactor(Float factor)
|
||||
{
|
||||
this->rigidBody->setAngularFactor(factor);
|
||||
|
@ -245,23 +234,45 @@ void SimpleRigidBody::SetUpAndForward(::Oyster::Math::Float3 up, ::Oyster::Math:
|
|||
|
||||
void SimpleRigidBody::SetUp(::Oyster::Math::Float3 up)
|
||||
{
|
||||
Float3 vector = Float3(0, 1, 0).Cross(up);
|
||||
|
||||
if(vector == Float3::null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Float sine = vector.GetLength();
|
||||
Float cosine = acos(Float3(0, 1, 0).Dot(up));
|
||||
|
||||
btQuaternion quaternion(btVector3(vector.x, vector.y, vector.z),cosine);
|
||||
|
||||
btQuaternion newRotation;
|
||||
btTransform trans;
|
||||
trans = this->rigidBody->getWorldTransform();
|
||||
trans.setRotation(quaternion);
|
||||
|
||||
btVector3 v1 = trans.getBasis().getColumn(1);
|
||||
btVector3 v2(up.x, up.y, up.z);
|
||||
|
||||
btQuaternion q;
|
||||
btVector3 a = v1.cross(v2);
|
||||
|
||||
if (v1.dot(v2) < -0.999999)
|
||||
{
|
||||
btVector3 xCrossPre = btVector3(1, 0 ,0).cross(v1);
|
||||
if(xCrossPre.length() < 0.000001)
|
||||
xCrossPre = btVector3(0, 1 ,0).cross(v1);
|
||||
xCrossPre.normalize();
|
||||
q.setRotation(xCrossPre, 3.1415);
|
||||
}
|
||||
else if (v1.dot(v2) > 0.999999)
|
||||
{
|
||||
q = btQuaternion(0, 0, 0, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
q.setX(a.x());
|
||||
q.setY(a.y());
|
||||
q.setZ(a.z());
|
||||
|
||||
q.setW(1 + v1.dot(v2));
|
||||
|
||||
q.normalize();
|
||||
}
|
||||
|
||||
newRotation = q*trans.getRotation();
|
||||
|
||||
trans.setRotation(newRotation);
|
||||
this->rigidBody->setWorldTransform(trans);
|
||||
this->state.quaternion = Quaternion(Float3(quaternion.x(), quaternion.y(), quaternion.z()), quaternion.w());
|
||||
|
||||
this->state.quaternion = Quaternion(Float3(newRotation.x(), newRotation.y(), newRotation.z()), newRotation.w());
|
||||
}
|
||||
|
||||
Float4x4 SimpleRigidBody::GetRotation() const
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace Oyster
|
|||
void SetRotation(Math::Quaternion quaternion);
|
||||
void SetRotation(Math::Float3 eulerAngles);
|
||||
void SetRotation(::Oyster::Math::Float4x4 rotation);
|
||||
void SetRotationAsAngularAxis(Math::Float4 angularAxis);
|
||||
void AddRotationAroundY(::Oyster::Math::Float angle);
|
||||
void SetAngularFactor(Math::Float factor);
|
||||
void SetMass(Math::Float mass);
|
||||
|
||||
|
|
|
@ -147,7 +147,7 @@ namespace Oyster
|
|||
virtual void SetRotation(::Oyster::Math::Quaternion quaternion) = 0;
|
||||
virtual void SetRotation(::Oyster::Math::Float3 eulerAngles) = 0;
|
||||
virtual void SetRotation(::Oyster::Math::Float4x4 rotation) = 0;
|
||||
virtual void SetRotationAsAngularAxis(::Oyster::Math::Float4 angularAxis) = 0;
|
||||
virtual void AddRotationAroundY(::Oyster::Math::Float angle) = 0;
|
||||
virtual void SetAngularFactor(::Oyster::Math::Float factor) = 0;
|
||||
virtual void SetMass(::Oyster::Math::Float mass) = 0;
|
||||
|
||||
|
|
Loading…
Reference in New Issue