diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp new file mode 100644 index 00000000..16468bd7 --- /dev/null +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -0,0 +1,65 @@ +#include "MessageHeader.h" +#include "../Packing.h" + +using namespace Oyster::Network::Messages; +using namespace Oyster::Network::Packing; + +MessageHeader::MessageHeader() +{ + size = 0; + msg = new unsigned char[1024]; +} + +MessageHeader::~MessageHeader() +{ + delete[] msg; +} + +void MessageHeader::Translate(/*Message& msg*/) +{ + size = 0; + + AddInt(4); + AddInt(5); + AddInt(6); +} + +void MessageHeader::Translate(unsigned char message[]) +{ + size = 0; + + int i = GetInt(message); + int j = GetInt(message); + int k = GetInt(message); +} + +unsigned char* MessageHeader::GetMsg() +{ + return msg; +} + +void MessageHeader::AddInt(int i) +{ + Pack(&msg[size], i); + size += 4; +} + +void MessageHeader::AddStr(std::string str) +{ + Pack(&msg[size], str); + size += 2 + str.length(); +} + +int MessageHeader::GetInt(unsigned char message[]) +{ + int i = Unpacki(&message[size]); + size += 4; + return i; +} + +std::string MessageHeader::GetStr(unsigned char message[]) +{ + std::string std = UnpackStr(&message[size]); + size += 2 + std.length(); + return std; +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.h b/Code/Network/NetworkDependencies/Messages/MessageHeader.h new file mode 100644 index 00000000..d6d79597 --- /dev/null +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.h @@ -0,0 +1,46 @@ +#ifndef NETWORK_DEPENDENCIES_MESSAGE_HEADER_H +#define NETWORK_DEPENDENCIES_MESSAGE_HEADER_H + +///////////////////////////////////////////////////////////////////// +// Created by Pontus Fransson 2013 +///////////////////////////////////////////////////////////////////// + +#include + +namespace Oyster +{ + namespace Network + { + namespace Messages + { + class MessageHeader + { + public: + MessageHeader(); + virtual ~MessageHeader(); + + virtual void Translate(/*Message& msg*/); + virtual void Translate(unsigned char message[]); + + unsigned char* GetMsg(); + + protected: + //Add variables to messages + void AddInt(int i); + void AddStr(std::string str); + + //Get variables from message + int GetInt(unsigned char message[]); + std::string GetStr(unsigned char message[]); + + private: + static const int max_message_length = 1024; + unsigned char* msg; + int size; + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp new file mode 100644 index 00000000..ba15c464 --- /dev/null +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp @@ -0,0 +1,26 @@ +#include "MessageTest.h" + +using namespace Oyster::Network::Messages; + +MessageTest::MessageTest() +{ + +} + +MessageTest::~MessageTest() +{ +} + +void MessageTest::Translate() +{ + MessageHeader::Translate(); + + AddStr("Hej hur mår du idag?"); +} + +void MessageTest::Translate(unsigned char message[]) +{ + MessageHeader::Translate(message); + + std::string str = GetStr(message); +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.h b/Code/Network/NetworkDependencies/Messages/MessageTest.h new file mode 100644 index 00000000..8aebda05 --- /dev/null +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.h @@ -0,0 +1,32 @@ +#ifndef NETWORK_DEPENDENCIES_MESSAGE_TEST_H +#define NETWORK_DEPENDENCIES_MESSAGE_TEST_H + +///////////////////////////////////////////////////////////////////// +// Created by Pontus Fransson 2013 +///////////////////////////////////////////////////////////////////// + +#include "MessageHeader.h" + +namespace Oyster +{ + namespace Network + { + namespace Messages + { + class MessageTest : public MessageHeader + { + public: + MessageTest(); + virtual ~MessageTest(); + + virtual void Translate(); + virtual void Translate(unsigned char message[]); + + private: + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 4c42ba1d..2be0652e 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -27,7 +27,7 @@ StaticLibrary true v110 - MultiByte + Unicode StaticLibrary @@ -153,10 +153,16 @@ + + + + + + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 57da6b71..0547fb8c 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -21,6 +21,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + @@ -29,5 +38,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp new file mode 100644 index 00000000..6c6edfda --- /dev/null +++ b/Code/Network/NetworkDependencies/Packing.cpp @@ -0,0 +1,470 @@ +#include "Packing.h" + +/*************************** + Packing +***************************/ + +#include + +namespace Oyster +{ + namespace Network + { + namespace Packing + { + //bool (1-bit) + void Pack(unsigned char buffer[], bool i) + { + *buffer++ = i; + } + + //char (8-bit) + void Pack(unsigned char buffer[], char i) + { + *buffer++ = i; + } + + void Pack(unsigned char buffer[], unsigned char i) + { + *buffer++ = i; + } + + //short (16-bit) + void Pack(unsigned char buffer[], short i) + { + *buffer++ = i >> 8; + *buffer++ = i; + } + + void Pack(unsigned char buffer[], unsigned short i) + { + *buffer++ = i >> 8; + *buffer++ = i; + } + + //int (32-bit) + void Pack(unsigned char buffer[], int i) + { + *buffer++ = i >> 24; + *buffer++ = i >> 16; + *buffer++ = i >> 8; + *buffer++ = i; + } + + void Pack(unsigned char buffer[], unsigned int i) + { + *buffer++ = i >> 24; + *buffer++ = i >> 16; + *buffer++ = i >> 8; + *buffer++ = i; + } + + //__int64 (64-bit) + void Pack(unsigned char buffer[], __int64 i) + { + *buffer++ = i >> 56; + *buffer++ = i >> 48; + *buffer++ = i >> 40; + *buffer++ = i >> 32; + *buffer++ = i >> 24; + *buffer++ = i >> 16; + *buffer++ = i >> 8; + *buffer++ = i; + } + + void Pack(unsigned char buffer[], unsigned __int64 i) + { + *buffer++ = i >> 56; + *buffer++ = i >> 48; + *buffer++ = i >> 40; + *buffer++ = i >> 32; + *buffer++ = i >> 24; + *buffer++ = i >> 16; + *buffer++ = i >> 8; + *buffer++ = i; + } + + //floating point (32, 64-bit) + void Pack(unsigned char buffer[], float i) + { + int tempFloat = Pack754(i, 32, 8); + Pack(buffer, tempFloat); + } + + void Pack(unsigned char buffer[], double i) + { + __int64 tempDouble = Pack754(i, 64, 11); + Pack(buffer, tempDouble); + } + + //string + void Pack(unsigned char buffer[], char str[]) + { + short len = strlen(str); + Pack(buffer, len); + buffer += 2; + memcpy(buffer, str, len); + } + + void Pack(unsigned char buffer[], std::string& str) + { + short len = str.length(); + Pack(buffer, len); + buffer += 2; + memcpy(buffer, str.c_str(), len); + } + + unsigned __int64 Pack754(long double f, unsigned bits, unsigned expbits) + { + long double fnorm; + int shift; + long long sign, exp, significand; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit + + if (f == 0.0) + return 0; // get this special case out of the way + + // check sign and begin normalization + if (f < 0) + { + sign = 1; + fnorm = -f; + } + else + { + sign = 0; + fnorm = f; + } + + // get the normalized form of f and track the exponent + shift = 0; + while(fnorm >= 2.0) + { + fnorm /= 2.0; + shift++; + } + + while(fnorm < 1.0) + { + fnorm *= 2.0; + shift--; + } + + fnorm = fnorm - 1.0; + + // calculate the binary form (non-float) of the significand data + significand = fnorm * ((1LL << significandbits) + 0.5f); + + // get the biased exponent + exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias + + // return the final answer + return (sign << (bits - 1)) | (exp << (bits - expbits - 1)) | significand; + } + + /****************************** + Unpacking + ******************************/ + + //bool (1-bit) + bool Unpackb(unsigned char buffer[]) + { + return (bool)buffer; + } + + //char (8-bit) + char Unpackc(unsigned char buffer[]) + { + if(*buffer <= 0x7f) + { + return *buffer; + } + else + { + return (-1 - (unsigned char)(0xffu - *buffer)); + } + } + + unsigned char UnpackC(unsigned char buffer[]) + { + return *buffer; + } + + //short (16-bit) + short Unpacks(unsigned char buffer[]) + { + short i = ((short)buffer[0] << 8) | buffer[1]; + + if(i > 0x7fffu) + { + i = -1 - (unsigned short)(0xffffu - i); + } + + return i; + } + + unsigned short UnpackS(unsigned char buffer[]) + { + return ((unsigned int)buffer[0] << 8) | buffer[1]; + } + + //int (32-bit) + int Unpacki(unsigned char buffer[]) + { + int i = ((int)buffer[0] << 24) | + ((int)buffer[1] << 16) | + ((int)buffer[2] << 8) | + ((int)buffer[3]); + + if(i > 0x7fffffffu) + { + i = -1 - (int)(0xffffffffu - i); + } + + return i; + } + + unsigned int UnpackI(unsigned char buffer[]) + { + return ((unsigned int)buffer[0] << 24) | + ((unsigned int)buffer[1] << 16) | + ((unsigned int)buffer[2] << 8) | + ((unsigned int)buffer[3]); + } + + //__int64 (64-bit) + __int64 Unpacki64(unsigned char buffer[]) + { + __int64 i = ((__int64)buffer[0] << 56) | + ((__int64)buffer[1] << 48) | + ((__int64)buffer[2] << 40) | + ((__int64)buffer[3] << 32) | + ((__int64)buffer[4] << 24) | + ((__int64)buffer[5] << 16) | + ((__int64)buffer[6] << 8) | + (buffer[7]); + + if(i > 0x7fffffffffffffffu) + { + i = -1 - (__int64)(0xffffffffffffffffu - i); + } + + return i; + } + + unsigned __int64 UnpackI64(unsigned char buffer[]) + { + + return ((__int64)buffer[0] << 56) | + ((__int64)buffer[1] << 48) | + ((__int64)buffer[2] << 40) | + ((__int64)buffer[3] << 32) | + ((__int64)buffer[4] << 24) | + ((__int64)buffer[5] << 16) | + ((__int64)buffer[6] << 8) | + ((__int64)buffer[7]); + } + + //floating point (32, 64-bit) + float Unpackf(unsigned char buffer[]) + { + int tempFloat = Unpacki(buffer); + return Unpack754(tempFloat, 32, 8); + } + + double Unpackd(unsigned char buffer[]) + { + __int64 tempDouble = Unpacki64(buffer); + return Unpack754(tempDouble, 64, 11); + } + + //string + std::string UnpackStr(unsigned char buffer[]) + { + short len = UnpackS(buffer); + std::string temp; + temp.resize(len); + + buffer += 2; + for(int i = 0; i < len; i++) + { + temp[i] = buffer[i]; + } + + return temp; + } + + long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits) + { + long double result; + long long shift; + unsigned bias; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit + + if (i == 0) + return 0.0; + + // pull the significand + result = (i&((1LL << significandbits) - 1)); // mask + result /= (1LL << significandbits); // convert back to float + result += 1.0f; // add the one back on + + // deal with the exponent + bias = (1 << (expbits - 1)) - 1; + shift = ((i >> significandbits) & ((1LL << expbits) - 1)) - bias; + while(shift > 0) + { + result *= 2.0; + shift--; + } + while(shift < 0) + { + result /= 2.0; + shift++; + } + + // sign it + result *= (i >> (bits - 1)) & 1 ? -1.0 : 1.0; + + return result; + } + } + } +} + +/* +int32_t pack(unsigned char* buffer, char* format, ...) +{ + va_list ap; + int16_t h; + int32_t l; + int8_t c; + float f; + double d; + char* s; + int32_t size = 0, len; + + va_start(ap, format); + + for(; *format != '\0'; format++) + { + switch(*format) + { + case 'h': // 16-bit + size += 2; + h = (int16_t)va_arg(ap, int); + packi16(buffer, h); + buffer += 2; + break; + case 'l': // 32-bit + size += 4; + l = va_arg(ap, int32_t); + packi32(buffer, l); + buffer += 4; + break; + case 'c': // 8-bit + size += 1; + c = (int8_t)va_arg(ap, int); + *buffer++ = (c >> 0)&0xff; + break; + case 'f': // float (32-bit) + size += 4; + f = (float)va_arg(ap, double); + //l = pack754(f, 32, 8); + packi32(buffer, l); + buffer += 4; + break; + case 'd': // double (64-bit) + size += 8; + d = (float)va_arg(ap, double); + //l = pack754(f, 64, 11); + packi32(buffer, l); + buffer += 4; + break; + case 's': // string + s = va_arg(ap, char*); + len = strlen(s); + size += len + 2; + packi16(buffer, len); + buffer += 2; + memcpy(buffer, s, len); + buffer += len; + break; + } + } + + va_end(ap); + + return size; +} +*/ + +/* +void unpack(unsigned char* buffer, char* format, ...) +{ + va_list ap; + int16_t* h; + int32_t* l; + int32_t pf; + int64_t pd; + int8_t* c; + float* f; + double* d; + char* s; + int32_t len, count, maxstrlen = 0; + + va_start(ap, format); + + for(; *format != '\0'; format++) + { + switch(*format) + { + case 'h': // 16-bit + h = va_arg(ap, int16_t*); + *h = unpacki16(buffer); + buffer += 2; + break; + case 'l': // 32-bit + l = va_arg(ap, int32_t*); + *l = unpacki32(buffer); + buffer += 4; + break; + case 'c': // 8-bit + c = va_arg(ap, int8_t*); + *c = *buffer++; + break; + case 'f': // float + f = va_arg(ap, float*); + pf = unpacki32(buffer); + buffer += 4; + //*f = unpack754(pf, 32, 8); + break; + case 'd': // double (64-bit) + d = va_arg(ap, double*); + pd = unpacki64(buffer); + buffer += 8; + //*d = unpack754(pf, 64, 11); + break; + case 's': // string + s = va_arg(ap, char*); + len = unpacki16(buffer); + buffer += 2; + if (maxstrlen > 0 && len > maxstrlen) count = maxstrlen - 1; + else count = len; + memcpy(s, buffer, count); + s[count] = '\0'; + buffer += len; + break; + default: + if (isdigit(*format)) // track max str len + { + maxstrlen = maxstrlen * 10 + (*format-'0'); + } + } + + if(!isdigit(*format)) + maxstrlen = 0; + } + + va_end(ap); +}*/ \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Packing.h b/Code/Network/NetworkDependencies/Packing.h new file mode 100644 index 00000000..8339ba68 --- /dev/null +++ b/Code/Network/NetworkDependencies/Packing.h @@ -0,0 +1,106 @@ +#ifndef PACKING_H +#define PACKING_H + +///////////////////////////////////////////////////////////////////// +// Created by Pontus Fransson 2013 +///////////////////////////////////////////////////////////////////// + +#include + +/****************************** + Packing +******************************/ +namespace Oyster +{ + namespace Network + { + namespace Packing + { + //bool (1-bit) + void Pack(unsigned char buffer[], bool i); + + //char (8-bit) + void Pack(unsigned char buffer[], char i); + void Pack(unsigned char buffer[], unsigned char i); // unsigned + + //short (16-bit) + void Pack(unsigned char buffer[], short i); + void Pack(unsigned char buffer[], unsigned short i); // unsigned + + //int (32-bit) + void Pack(unsigned char buffer[], int i); + void Pack(unsigned char buffer[], unsigned int i); // unsigned + + //__int64 (64-bit) + void Pack(unsigned char buffer[], __int64 i); + void Pack(unsigned char buffer[], unsigned __int64 i); // unsigned + + //floating point (32, 64-bit) + void Pack(unsigned char buffer[], float i); + void Pack(unsigned char buffer[], double i); + + //string + void Pack(unsigned char buffer[], char str[]); + void Pack(unsigned char buffer[], std::string& str); + + unsigned __int64 Pack754(long double f, unsigned bits, unsigned expbits); + + /****************************** + Unpacking + ******************************/ + + //bool (1-bit) + bool Unpackb(unsigned char buffer[]); + + //char (8-bit) + char Unpackc(unsigned char buffer[]); + unsigned char UnpackC(unsigned char buffer[]); // unsigned + + //short (16-bit) + short Unpacks(unsigned char buffer[]); + unsigned short UnpackS(unsigned char buffer[]); // unsigned + + //int (32-bit) + int Unpacki(unsigned char buffer[]); + unsigned int UnpackI(unsigned char buffer[]); // unsigned + + //__int64 (64-bit) + __int64 Unpacki64(unsigned char buffer[]); + unsigned __int64 UnpackI64(unsigned char buffer[]); // unsigned + + //floating point (32, 64-bit) + float Unpackf(unsigned char buffer[]); + double Unpackd(unsigned char buffer[]); + + //string + std::string UnpackStr(unsigned char buffer[]); + + long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits); + } + } +} + + +//int32_t pack(unsigned char* buffer, char* format, ...); + +//void unpack(unsigned char* buffer, char* format, ...); + + +/*********************************************** +* This table is used for naming pack/unpack functions. +* It's also used to identify types in the 'format' string in function pack()/unpack() +* +* bits |signed unsigned float string +* -----+---------------------------------- +* 1 | b +* 8 | c C +* 16 | s S f +* 32 | i I d +* 64 | q Q g +* - | str +* +* (16-bit unsigned length is automatically added in front of strings) +* +*/ + +#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/Client.h b/Code/Network/OysterNetworkClient/Client.h index 1fd9f1f6..d7a50e00 100644 --- a/Code/Network/OysterNetworkClient/Client.h +++ b/Code/Network/OysterNetworkClient/Client.h @@ -2,7 +2,7 @@ #define NETWORK_CLIENT_CLIENT_H ///////////////////////////////////////////////////////////////////// -// Created by Pontus 2013 +// Created by Pontus Fransson 2013 ///////////////////////////////////////////////////////////////////// #include "../NetworkDependencies/Connection.h" diff --git a/Code/Network/OysterNetworkServer/Client.h b/Code/Network/OysterNetworkServer/Client.h index 3c29c620..2b151af1 100644 --- a/Code/Network/OysterNetworkServer/Client.h +++ b/Code/Network/OysterNetworkServer/Client.h @@ -2,7 +2,7 @@ #define NETWORK_SERVER_CLIENT_H ///////////////////////////////////////////////////////////////////// -// Created by Pontus 2013 +// Created by Pontus Fransson 2013 ///////////////////////////////////////////////////////////////////// #include "../NetworkDependencies/Connection.h" @@ -16,7 +16,6 @@ public: void Send(char buffer[]); void Recv(char buffer[]); - private: Connection* connection; diff --git a/Code/Network/OysterNetworkServer/IListener.h b/Code/Network/OysterNetworkServer/IListener.h index 9a427f29..93b7a935 100644 --- a/Code/Network/OysterNetworkServer/IListener.h +++ b/Code/Network/OysterNetworkServer/IListener.h @@ -2,7 +2,7 @@ #define NETWORK_SERVER_ILISTENER_H ///////////////////////////////////////////////////////////////////// -// Created by Pontus 2013 +// Created by Pontus Fransson 2013 ///////////////////////////////////////////////////////////////////// class IListener diff --git a/Code/Network/OysterNetworkServer/Listener.h b/Code/Network/OysterNetworkServer/Listener.h index 8e5f99b7..316491e2 100644 --- a/Code/Network/OysterNetworkServer/Listener.h +++ b/Code/Network/OysterNetworkServer/Listener.h @@ -2,7 +2,7 @@ #define NETWORK_SERVER_LISTENER_H ///////////////////////////////////////////////////////////////////// -// Created by Pontus 2013 +// Created by Pontus Fransson 2013 ///////////////////////////////////////////////////////////////////// #include "IListener.h" diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 4fbe8ece..b63ea190 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -9,11 +9,19 @@ using namespace std; void ShutdownSockets(); bool InitSockets(); +#include "../NetworkDependencies/Messages/MessageTest.h" +using namespace Oyster::Network::Messages; + int main() { cout << "Server" << endl; - char recvBuffer[255]; + unsigned char* recvBuffer = new unsigned char[255]; + + MessageTest msg; + msg.Translate(); + recvBuffer = msg.GetMsg(); + msg.Translate(recvBuffer); if(!InitSockets()) { @@ -39,13 +47,13 @@ int main() while(1) { - client1.Recv(recvBuffer); + /*client1.Recv(recvBuffer); cout << "Client1: " << recvBuffer << endl; client2.Send(recvBuffer); client2.Recv(recvBuffer); cout << "Client2: " << recvBuffer << endl; - client1.Send(recvBuffer); + client1.Send(recvBuffer);*/ } ShutdownSockets();