Network - Added MessagePlayerPos
Added MessagePlayerPos Fixed bugg with adding float array.
This commit is contained in:
parent
10c786b745
commit
d78b48319d
|
@ -111,7 +111,6 @@ void MessageHeader::PackFloat(float i, OysterByte& bytes)
|
||||||
|
|
||||||
void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
|
void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
|
||||||
{
|
{
|
||||||
bytes.AddSize(4);
|
|
||||||
//Pack number of elements
|
//Pack number of elements
|
||||||
PackUnsignedInt(elementCount, bytes);
|
PackUnsignedInt(elementCount, bytes);
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,9 @@ namespace Oyster
|
||||||
void PackStr(char str[], OysterByte& bytes);
|
void PackStr(char str[], OysterByte& bytes);
|
||||||
void PackStr(std::string str, OysterByte& bytes);
|
void PackStr(std::string str, OysterByte& bytes);
|
||||||
|
|
||||||
|
//Maybe
|
||||||
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
|
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
|
||||||
|
|
||||||
|
|
||||||
//Unpack variables from message
|
//Unpack variables from message
|
||||||
bool UnpackBool(OysterByte& bytes);
|
bool UnpackBool(OysterByte& bytes);
|
||||||
|
@ -71,6 +72,7 @@ namespace Oyster
|
||||||
|
|
||||||
std::string UnpackStr(OysterByte& bytes);
|
std::string UnpackStr(OysterByte& bytes);
|
||||||
|
|
||||||
|
//Maybe
|
||||||
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc.
|
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -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
|
|
@ -7,5 +7,6 @@
|
||||||
|
|
||||||
#include "MessageHeader.h"
|
#include "MessageHeader.h"
|
||||||
#include "MessageTest.h"
|
#include "MessageTest.h"
|
||||||
|
#include "MessagePlayerPos.h"
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -155,6 +155,7 @@
|
||||||
<ClCompile Include="Listener.cpp" />
|
<ClCompile Include="Listener.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Messages\MessageHeader.cpp" />
|
<ClCompile Include="Messages\MessageHeader.cpp" />
|
||||||
|
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
|
||||||
<ClCompile Include="Messages\MessageTest.cpp" />
|
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||||
<ClCompile Include="OysterByte.cpp" />
|
<ClCompile Include="OysterByte.cpp" />
|
||||||
<ClCompile Include="Packing.cpp" />
|
<ClCompile Include="Packing.cpp" />
|
||||||
|
@ -168,6 +169,7 @@
|
||||||
<ClInclude Include="IPostBox.h" />
|
<ClInclude Include="IPostBox.h" />
|
||||||
<ClInclude Include="Listener.h" />
|
<ClInclude Include="Listener.h" />
|
||||||
<ClInclude Include="Messages\MessageHeader.h" />
|
<ClInclude Include="Messages\MessageHeader.h" />
|
||||||
|
<ClInclude Include="Messages\MessagePlayerPos.h" />
|
||||||
<ClInclude Include="Messages\MessagesInclude.h" />
|
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||||
<ClInclude Include="Messages\MessageTest.h" />
|
<ClInclude Include="Messages\MessageTest.h" />
|
||||||
<ClInclude Include="OysterByte.h" />
|
<ClInclude Include="OysterByte.h" />
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
<ClCompile Include="Listener.cpp" />
|
<ClCompile Include="Listener.cpp" />
|
||||||
<ClCompile Include="WinsockFunctions.cpp" />
|
<ClCompile Include="WinsockFunctions.cpp" />
|
||||||
<ClCompile Include="OysterByte.cpp" />
|
<ClCompile Include="OysterByte.cpp" />
|
||||||
|
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Connection.h" />
|
<ClInclude Include="Connection.h" />
|
||||||
|
@ -27,5 +28,6 @@
|
||||||
<ClInclude Include="OysterByte.h" />
|
<ClInclude Include="OysterByte.h" />
|
||||||
<ClInclude Include="IPostBox.h" />
|
<ClInclude Include="IPostBox.h" />
|
||||||
<ClInclude Include="PostBox.h" />
|
<ClInclude Include="PostBox.h" />
|
||||||
|
<ClInclude Include="Messages\MessagePlayerPos.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -87,7 +87,7 @@ namespace Oyster
|
||||||
//floating point (32, 64-bit)
|
//floating point (32, 64-bit)
|
||||||
void Pack(unsigned char buffer[], float i)
|
void Pack(unsigned char buffer[], float i)
|
||||||
{
|
{
|
||||||
int tempFloat = Pack754(i, 32, 8);
|
int tempFloat = (int)Pack754(i, 32, 8);
|
||||||
Pack(buffer, tempFloat);
|
Pack(buffer, tempFloat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ namespace Oyster
|
||||||
fnorm = fnorm - 1.0;
|
fnorm = fnorm - 1.0;
|
||||||
|
|
||||||
// calculate the binary form (non-float) of the significand data
|
// 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
|
// get the biased exponent
|
||||||
exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias
|
exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias
|
||||||
|
@ -169,7 +169,7 @@ namespace Oyster
|
||||||
//bool (1-bit)
|
//bool (1-bit)
|
||||||
bool Unpackb(unsigned char buffer[])
|
bool Unpackb(unsigned char buffer[])
|
||||||
{
|
{
|
||||||
return (bool)buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
//char (8-bit)
|
//char (8-bit)
|
||||||
|
@ -305,7 +305,7 @@ namespace Oyster
|
||||||
return 0.0;
|
return 0.0;
|
||||||
|
|
||||||
// pull the significand
|
// 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 /= (1LL << significandbits); // convert back to float
|
||||||
result += 1.0f; // add the one back on
|
result += 1.0f; // add the one back on
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ namespace Oyster
|
||||||
PackageType_header,
|
PackageType_header,
|
||||||
PackageType_test,
|
PackageType_test,
|
||||||
PackageType_input,
|
PackageType_input,
|
||||||
PackageType_update_position
|
PackageType_player_pos,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ProtocolHeader
|
struct ProtocolHeader
|
||||||
|
@ -45,6 +45,16 @@ namespace Oyster
|
||||||
virtual ~ProtocolTest() { delete[] f; }
|
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.
|
//Holding every protocol in an union.
|
||||||
//Used because we now don't have to type case our protocol when we recieve them.
|
//Used because we now don't have to type case our protocol when we recieve them.
|
||||||
|
@ -56,6 +66,7 @@ namespace Oyster
|
||||||
{
|
{
|
||||||
ProtocolHeader* pHeader;
|
ProtocolHeader* pHeader;
|
||||||
ProtocolTest *pTest;
|
ProtocolTest *pTest;
|
||||||
|
ProtocolPlayerPos *pPlayerPos;
|
||||||
|
|
||||||
}Protocol;
|
}Protocol;
|
||||||
|
|
||||||
|
@ -75,6 +86,12 @@ namespace Oyster
|
||||||
delete Protocol.pTest;
|
delete Protocol.pTest;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case PackageType_player_pos:
|
||||||
|
if(Protocol.pPlayerPos)
|
||||||
|
{
|
||||||
|
delete Protocol.pPlayerPos;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,6 +17,10 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
|
||||||
case PackageType_test:
|
case PackageType_test:
|
||||||
message = new MessageTest();
|
message = new MessageTest();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case PackageType_player_pos:
|
||||||
|
message = new MessagePlayerPos();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message != NULL)
|
if(message != NULL)
|
||||||
|
@ -52,6 +56,12 @@ void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
|
||||||
set->Protocol.pTest = new ProtocolTest;
|
set->Protocol.pTest = new ProtocolTest;
|
||||||
message->Unpack(bytes, *set->Protocol.pTest);
|
message->Unpack(bytes, *set->Protocol.pTest);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case PackageType_player_pos:
|
||||||
|
message = new MessagePlayerPos();
|
||||||
|
set->Protocol.pPlayerPos = new ProtocolPlayerPos;
|
||||||
|
message->Unpack(bytes, *set->Protocol.pPlayerPos);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message)
|
if(message)
|
||||||
|
|
|
@ -54,13 +54,13 @@ void chat(Client &client)
|
||||||
string msgSend = "";
|
string msgSend = "";
|
||||||
|
|
||||||
ProtocolSet* set = new ProtocolSet;
|
ProtocolSet* set = new ProtocolSet;
|
||||||
ProtocolTest test;
|
ProtocolPlayerPos test;
|
||||||
test.numOfFloats = 5;
|
test.ID = 5;
|
||||||
test.f = new float[test.numOfFloats];
|
test.matrix = new float[16];
|
||||||
float temp = 12345.5654f;
|
float temp = 10;
|
||||||
for(int i = 0; i < 5; i++)
|
for(int i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
test.f[i] = temp;
|
test.matrix[i] = temp;
|
||||||
temp++;
|
temp++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,12 +78,20 @@ void chat(Client &client)
|
||||||
break;
|
break;
|
||||||
case PackageType_test:
|
case PackageType_test:
|
||||||
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
|
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 << set->Protocol.pTest->f[i] << ' ' ;
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
break;
|
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();
|
set->Release();
|
||||||
|
|
|
@ -40,17 +40,15 @@ int main()
|
||||||
|
|
||||||
//Start listening
|
//Start listening
|
||||||
//Accept a client
|
//Accept a client
|
||||||
ProtocolTest test;
|
ProtocolPlayerPos test;
|
||||||
test.clientID = 0;
|
test.clientID = 0;
|
||||||
test.size = 2;
|
test.ID = 5;
|
||||||
test.textMessage = "hej";
|
test.nrOfFloats = 16;
|
||||||
test.numOfFloats = 0;
|
test.matrix = new float[test.nrOfFloats];
|
||||||
test.f = new float[test.numOfFloats];
|
|
||||||
float temp = 395.456f;
|
for(int i = 0; i < test.nrOfFloats; i++)
|
||||||
for(int i = 0; i < (int)test.numOfFloats; i++)
|
|
||||||
{
|
{
|
||||||
test.f[i] = temp;
|
test.matrix[i] = i;
|
||||||
temp--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Pack(test, recvBuffer);
|
t.Pack(test, recvBuffer);
|
||||||
|
|
Loading…
Reference in New Issue