Network - Added MessagePlayerPos

Added MessagePlayerPos
Fixed bugg with adding float array.
This commit is contained in:
Pontus Fransson 2013-12-03 11:46:46 +01:00
parent 10c786b745
commit d78b48319d
12 changed files with 125 additions and 22 deletions

View File

@ -111,7 +111,6 @@ void MessageHeader::PackFloat(float i, OysterByte& bytes)
void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
{
bytes.AddSize(4);
//Pack number of elements
PackUnsignedInt(elementCount, bytes);

View File

@ -47,6 +47,7 @@ namespace Oyster
void PackStr(char str[], OysterByte& bytes);
void PackStr(std::string str, OysterByte& bytes);
//Maybe
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
@ -71,6 +72,7 @@ namespace Oyster
std::string UnpackStr(OysterByte& bytes);
//Maybe
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc.

View File

@ -0,0 +1,30 @@
#include "MessagePlayerPos.h"
using namespace Oyster::Network;
using namespace Oyster::Network::Messages;
using namespace Oyster::Network::Protocols;
MessagePlayerPos::MessagePlayerPos()
{
}
MessagePlayerPos::~MessagePlayerPos()
{
}
void MessagePlayerPos::Pack(Protocols::ProtocolHeader& header, OysterByte& bytes)
{
MessageHeader::Pack(header, bytes);
PackInt(static_cast<ProtocolPlayerPos*>(&header)->ID, bytes);
PackFloat(static_cast<ProtocolPlayerPos*>(&header)->matrix, static_cast<ProtocolPlayerPos*>(&header)->nrOfFloats, bytes);
SetSize(bytes);
}
void MessagePlayerPos::Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header)
{
MessageHeader::Unpack(bytes, header);
static_cast<ProtocolPlayerPos*>(&header)->ID = UnpackInt(bytes);
static_cast<ProtocolPlayerPos*>(&header)->matrix = UnpackFloat(static_cast<ProtocolPlayerPos*>(&header)->nrOfFloats, bytes);
}

View File

@ -0,0 +1,34 @@
#ifndef NETWORK_DEPENDENCIES_MESSAGE_PLAYER_POS_H
#define NETWORK_DEPENDENCIES_MESSAGE_PLAYER_POS_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "MessageHeader.h"
namespace Oyster
{
namespace Network
{
namespace Messages
{
class MessagePlayerPos : public MessageHeader
{
public:
MessagePlayerPos();
virtual ~MessagePlayerPos();
virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes);
virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header);
private:
};
}
}
}
#endif

View File

@ -7,5 +7,6 @@
#include "MessageHeader.h"
#include "MessageTest.h"
#include "MessagePlayerPos.h"
#endif

View File

@ -155,6 +155,7 @@
<ClCompile Include="Listener.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="OysterByte.cpp" />
<ClCompile Include="Packing.cpp" />
@ -168,6 +169,7 @@
<ClInclude Include="IPostBox.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessagePlayerPos.h" />
<ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Messages\MessageTest.h" />
<ClInclude Include="OysterByte.h" />

View File

@ -10,6 +10,7 @@
<ClCompile Include="Listener.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
<ClCompile Include="OysterByte.cpp" />
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Connection.h" />
@ -27,5 +28,6 @@
<ClInclude Include="OysterByte.h" />
<ClInclude Include="IPostBox.h" />
<ClInclude Include="PostBox.h" />
<ClInclude Include="Messages\MessagePlayerPos.h" />
</ItemGroup>
</Project>

View File

@ -87,7 +87,7 @@ namespace Oyster
//floating point (32, 64-bit)
void Pack(unsigned char buffer[], float i)
{
int tempFloat = Pack754(i, 32, 8);
int tempFloat = (int)Pack754(i, 32, 8);
Pack(buffer, tempFloat);
}
@ -153,7 +153,7 @@ namespace Oyster
fnorm = fnorm - 1.0;
// calculate the binary form (non-float) of the significand data
significand = fnorm * ((1LL << significandbits) + 0.5f);
significand = (long long)(fnorm * ((1LL << significandbits) + 0.5f));
// get the biased exponent
exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias
@ -169,7 +169,7 @@ namespace Oyster
//bool (1-bit)
bool Unpackb(unsigned char buffer[])
{
return (bool)buffer;
return buffer;
}
//char (8-bit)
@ -305,7 +305,7 @@ namespace Oyster
return 0.0;
// pull the significand
result = (i&((1LL << significandbits) - 1)); // mask
result = (long double)(i&((1LL << significandbits) - 1)); // mask
result /= (1LL << significandbits); // convert back to float
result += 1.0f; // add the one back on

View File

@ -22,7 +22,7 @@ namespace Oyster
PackageType_header,
PackageType_test,
PackageType_input,
PackageType_update_position
PackageType_player_pos,
};
struct ProtocolHeader
@ -45,6 +45,16 @@ namespace Oyster
virtual ~ProtocolTest() { delete[] f; }
};
struct ProtocolPlayerPos : public ProtocolHeader
{
int ID;
unsigned int nrOfFloats;
float *matrix;
ProtocolPlayerPos() { this->packageType = PackageType_player_pos; }
virtual ~ProtocolPlayerPos() { delete[] matrix; }
};
//Holding every protocol in an union.
//Used because we now don't have to type case our protocol when we recieve them.
@ -56,6 +66,7 @@ namespace Oyster
{
ProtocolHeader* pHeader;
ProtocolTest *pTest;
ProtocolPlayerPos *pPlayerPos;
}Protocol;
@ -75,6 +86,12 @@ namespace Oyster
delete Protocol.pTest;
}
break;
case PackageType_player_pos:
if(Protocol.pPlayerPos)
{
delete Protocol.pPlayerPos;
}
break;
}
}
};

View File

@ -17,6 +17,10 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
case PackageType_test:
message = new MessageTest();
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
break;
}
if(message != NULL)
@ -52,6 +56,12 @@ void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
set->Protocol.pTest = new ProtocolTest;
message->Unpack(bytes, *set->Protocol.pTest);
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
set->Protocol.pPlayerPos = new ProtocolPlayerPos;
message->Unpack(bytes, *set->Protocol.pPlayerPos);
break;
}
if(message)

View File

@ -54,13 +54,13 @@ void chat(Client &client)
string msgSend = "";
ProtocolSet* set = new ProtocolSet;
ProtocolTest test;
test.numOfFloats = 5;
test.f = new float[test.numOfFloats];
float temp = 12345.5654f;
ProtocolPlayerPos test;
test.ID = 5;
test.matrix = new float[16];
float temp = 10;
for(int i = 0; i < 5; i++)
{
test.f[i] = temp;
test.matrix[i] = temp;
temp++;
}
@ -78,12 +78,20 @@ void chat(Client &client)
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
case PackageType_player_pos:
cout << "Server: ID " << set->Protocol.pPlayerPos->ID << endl;
for(int i = 0; i < set->Protocol.pPlayerPos->nrOfFloats; i++)
{
cout << set->Protocol.pPlayerPos->matrix[i] << ' ';
}
cout << endl;
break;
}
set->Release();

View File

@ -40,17 +40,15 @@ int main()
//Start listening
//Accept a client
ProtocolTest test;
ProtocolPlayerPos test;
test.clientID = 0;
test.size = 2;
test.textMessage = "hej";
test.numOfFloats = 0;
test.f = new float[test.numOfFloats];
float temp = 395.456f;
for(int i = 0; i < (int)test.numOfFloats; i++)
test.ID = 5;
test.nrOfFloats = 16;
test.matrix = new float[test.nrOfFloats];
for(int i = 0; i < test.nrOfFloats; i++)
{
test.f[i] = temp;
temp--;
test.matrix[i] = i;
}
t.Pack(test, recvBuffer);