Network - First basic API.

This commit is contained in:
Pontus Fransson 2013-12-10 08:32:08 +01:00
parent d27dcbe5b6
commit e06ee31754
17 changed files with 224 additions and 177 deletions

View File

@ -161,9 +161,18 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="CustomNetProtocol.cpp" /> <ClCompile Include="CustomNetProtocol.cpp" />
<ClCompile Include="NetworkServer.cpp" />
<ClCompile Include="NetworkClient.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="CustomNetProtocol.h" /> <ClInclude Include="CustomNetProtocol.h" />
<ClInclude Include="NetworkServer.h" />
<ClInclude Include="NetworkClient.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -0,0 +1,41 @@
#include "NetworkClient.h"
using namespace Oyster::Network;
/*************************************
PrivateData
*************************************/
struct PrivateData
{
};
/*************************************
NetworkClient
*************************************/
NetworkClient::NetworkClient()
{
}
NetworkClient::~NetworkClient()
{
}
void NetworkClient::Disconnect()
{
}
bool NetworkClient::IsConnected()
{
return false;
}
void NetworkClient::Send()
{
}

View File

@ -0,0 +1,42 @@
#ifndef NETWORK_API_NETWORK_CLIENT_H
#define NETWORK_API_NETWORK_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#ifdef CUSTOM_NET_PROTOCOL_EXPORT
#define NET_PROTOCOL_EXPORT __declspec(dllexport)
#else
#define NET_PROTOCOL_EXPORT __declspec(dllimport)
#endif
class RecieverObject;
namespace Oyster
{
namespace Network
{
extern "C"
{
class NET_PROTOCOL_EXPORT NetworkClient
{
public:
NetworkClient();
virtual ~NetworkClient();
virtual void Disconnect();
virtual bool IsConnected();
virtual void Send();
private:
struct PrivateData;
PrivateData* privateData;
};
}
}
}
#endif

View File

@ -1,7 +1,9 @@
#include "IServer.h" #include "NetworkServer.h"
#include "NetworkClient.h"
#include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Listener.h"
#include "IClient.h"
#include "../NetworkDependencies/PostBox.h" #include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Utilities.h" #include "../../Misc/Utilities.h"
#include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterThread.h"
@ -14,7 +16,7 @@ using namespace Oyster::Thread;
PrivateData PrivateData
*************************************/ *************************************/
struct IServer::PrivateData : public IThreadObject struct NetworkServer::PrivateData : public IThreadObject
{ {
PrivateData(); PrivateData();
~PrivateData(); ~PrivateData();
@ -41,19 +43,19 @@ struct IServer::PrivateData : public IThreadObject
}; };
IServer::PrivateData::PrivateData() NetworkServer::PrivateData::PrivateData()
{ {
listener = 0; listener = 0;
started = false; started = false;
postBox = new PostBox<int>; postBox = new PostBox<int>;
} }
IServer::PrivateData::~PrivateData() NetworkServer::PrivateData::~PrivateData()
{ {
Shutdown(); Shutdown();
} }
bool IServer::PrivateData::Init(INIT_DESC& initDesc) bool NetworkServer::PrivateData::Init(INIT_DESC& initDesc)
{ {
//Check if it's a valid port //Check if it's a valid port
if(initDesc.port == 0) if(initDesc.port == 0)
@ -72,7 +74,7 @@ bool IServer::PrivateData::Init(INIT_DESC& initDesc)
return true; return true;
} }
bool IServer::PrivateData::Start() bool NetworkServer::PrivateData::Start()
{ {
//Start listener //Start listener
((Listener*)listener)->Start(); ((Listener*)listener)->Start();
@ -83,7 +85,7 @@ bool IServer::PrivateData::Start()
return true; return true;
} }
bool IServer::PrivateData::Stop() bool NetworkServer::PrivateData::Stop()
{ {
if(listener) if(listener)
{ {
@ -97,7 +99,7 @@ bool IServer::PrivateData::Stop()
return true; return true;
} }
bool IServer::PrivateData::Shutdown() bool NetworkServer::PrivateData::Shutdown()
{ {
//Stop server main thread //Stop server main thread
thread.Stop(); thread.Stop();
@ -120,7 +122,7 @@ bool IServer::PrivateData::Shutdown()
} }
//Checks for new clients and sends them to the proc function. //Checks for new clients and sends them to the proc function.
void IServer::PrivateData::CheckForNewClient() void NetworkServer::PrivateData::CheckForNewClient()
{ {
if(postBox->IsFull()) if(postBox->IsFull())
{ {
@ -133,13 +135,16 @@ void IServer::PrivateData::CheckForNewClient()
return; return;
} }
//Create the new client //Create client and Proc function if the pointer is not NULL
IClient* client = new IClient(); if(initDesc.proc)
initDesc.proc(client); {
Oyster::Network::NetworkClient* client = new Oyster::Network::NetworkClient();
initDesc.proc((NetworkClient*)client);
}
} }
} }
bool IServer::PrivateData::DoWork() bool NetworkServer::PrivateData::DoWork()
{ {
CheckForNewClient(); CheckForNewClient();
@ -147,15 +152,15 @@ bool IServer::PrivateData::DoWork()
} }
/************************************* /*************************************
IServer NetworkServer
*************************************/ *************************************/
IServer::IServer() NetworkServer::NetworkServer()
{ {
privateData = new PrivateData(); privateData = new PrivateData();
} }
IServer::~IServer() NetworkServer::~NetworkServer()
{ {
if(privateData) if(privateData)
{ {
@ -163,35 +168,35 @@ IServer::~IServer()
} }
} }
bool IServer::Init(INIT_DESC& initDesc) bool NetworkServer::Init(INIT_DESC& initDesc)
{ {
privateData->Init(initDesc); privateData->Init(initDesc);
return true; return true;
} }
bool IServer::Start() bool NetworkServer::Start()
{ {
privateData->Start(); privateData->Start();
return true; return true;
} }
bool IServer::Stop() bool NetworkServer::Stop()
{ {
privateData->Stop(); privateData->Stop();
return true; return true;
} }
bool IServer::Shutdown() bool NetworkServer::Shutdown()
{ {
privateData->Shutdown(); privateData->Shutdown();
return true; return true;
} }
bool IServer::IsStarted() const bool NetworkServer::IsStarted() const
{ {
return privateData->started; return privateData->started;
} }

View File

@ -0,0 +1,56 @@
#ifndef NETWORK_API_NETWORK_SERVER_H
#define NETWORK_API_NETWORK_SERVER_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#ifdef CUSTOM_NET_PROTOCOL_EXPORT
#define NET_PROTOCOL_EXPORT __declspec(dllexport)
#else
#define NET_PROTOCOL_EXPORT __declspec(dllimport)
#endif
#pragma comment(lib, "ws2_32.lib")
//#include "NetworkClient.h"
namespace Oyster
{
namespace Network
{
namespace Server
{
extern "C"
{
class NET_PROTOCOL_EXPORT NetworkServer
{
public:
class NetworkClient;
struct INIT_DESC
{
unsigned short port; //Port the server should be accepting clients on.
void (*proc)(NetworkClient*);
};
NetworkServer();
virtual ~NetworkServer();
virtual bool Init(INIT_DESC& initDesc);
virtual bool Start();
virtual bool Stop();
virtual bool Shutdown();
virtual bool IsStarted() const;
private:
struct PrivateData;
PrivateData* privateData;
};
}
}
}
}
#endif

View File

@ -135,9 +135,9 @@ int Connection::Recieve(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
int Connection::Listen() int Connection::Listen()
{ {
int clientSocket; int clientSocket;
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) if((clientSocket = (int)accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
{ {
return INVALID_SOCKET;//WSAGetLastError(); return (int)INVALID_SOCKET;//WSAGetLastError();
} }
return clientSocket; return clientSocket;
@ -148,7 +148,7 @@ int Connection::Listen()
/////////////////////////////////////// ///////////////////////////////////////
int Connection::InitiateSocket() int Connection::InitiateSocket()
{ {
this->socket = ::socket(AF_INET, SOCK_STREAM, 0); this->socket = (int)::socket(AF_INET, SOCK_STREAM, 0);
if(this->socket == SOCKET_ERROR) if(this->socket == SOCKET_ERROR)
{ {
return WSAGetLastError(); return WSAGetLastError();

View File

@ -130,7 +130,7 @@ void MessageHeader::PackDouble(double i, OysterByte& bytes)
void MessageHeader::PackStr(char str[], OysterByte& bytes) void MessageHeader::PackStr(char str[], OysterByte& bytes)
{ {
int totalSize = 2 + strlen(str); int totalSize = 2 + (int)strlen(str);
bytes.AddSize(totalSize); bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str); Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize; size += totalSize;
@ -138,7 +138,7 @@ void MessageHeader::PackStr(char str[], OysterByte& bytes)
void MessageHeader::PackStr(std::string str, OysterByte& bytes) void MessageHeader::PackStr(std::string str, OysterByte& bytes)
{ {
int totalSize = 2 + str.length(); int totalSize = 2 + (int)str.length();
bytes.AddSize(totalSize); bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str); Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize; size += totalSize;
@ -243,7 +243,7 @@ double MessageHeader::UnpackDouble(OysterByte& bytes)
std::string MessageHeader::UnpackStr(OysterByte& bytes) std::string MessageHeader::UnpackStr(OysterByte& bytes)
{ {
std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]); std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]);
size += 2 + str.length(); size += 2 + (int)str.length();
return str; return str;
} }

View File

@ -21,7 +21,7 @@ OysterByte::OysterByte(const OysterByte& obj)
//delete[] this->byteArray; //delete[] this->byteArray;
this->byteArray = new unsigned char[obj.capacity]; this->byteArray = new unsigned char[obj.capacity];
for(int i = 0; i < obj.size; i++) for(int i = 0; i < (int)obj.size; i++)
{ {
this->byteArray[i] = obj.byteArray[i]; this->byteArray[i] = obj.byteArray[i];
} }
@ -85,7 +85,7 @@ OysterByte& OysterByte::operator =(const OysterByte& obj)
delete[] this->byteArray; delete[] this->byteArray;
this->byteArray = new unsigned char[obj.capacity]; this->byteArray = new unsigned char[obj.capacity];
for(int i = 0; i < obj.size; i++) for(int i = 0; i < (int)obj.size; i++)
{ {
this->byteArray[i] = obj.byteArray[i]; this->byteArray[i] = obj.byteArray[i];
} }

View File

@ -100,7 +100,7 @@ namespace Oyster
//string //string
void Pack(unsigned char buffer[], char str[]) void Pack(unsigned char buffer[], char str[])
{ {
short len = strlen(str); short len = (short)strlen(str);
Pack(buffer, len); Pack(buffer, len);
buffer += 2; buffer += 2;
memcpy(buffer, str, len); memcpy(buffer, str, len);
@ -108,7 +108,7 @@ namespace Oyster
void Pack(unsigned char buffer[], std::string& str) void Pack(unsigned char buffer[], std::string& str)
{ {
short len = str.length(); short len = (short)str.length();
Pack(buffer, len); Pack(buffer, len);
buffer += 2; buffer += 2;
memcpy(buffer, str.c_str(), len); memcpy(buffer, str.c_str(), len);

View File

@ -1,23 +0,0 @@
#ifndef OYSTER_NETWORK_SERVER_I_CLIENT_H
#define OYSTER_NETWORK_SERVER_I_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
class IClient
{
public:
IClient() {}
virtual ~IClient() {}
virtual void Disconnect() {}
virtual bool IsConnected() {return true;}
virtual void Send() {}
private:
};
#endif

View File

@ -1,44 +0,0 @@
#ifndef OYSTER_NETWORK_SERVER_I_SERVER_H
#define OYSTER_NETWORK_SERVER_I_SERVER_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "IClient.h"
namespace Oyster
{
namespace Network
{
namespace Server
{
class IServer
{
public:
struct INIT_DESC
{
unsigned short port; //Port the server should be accepting clients on.
void (*proc)(IClient*);
};
IServer();
virtual ~IServer();
virtual bool Init(INIT_DESC& initDesc);
virtual bool Start();
virtual bool Stop();
virtual bool Shutdown();
virtual bool IsStarted() const;
private:
struct PrivateData;
PrivateData* privateData;
};
}
}
}
#endif

View File

@ -149,18 +149,18 @@
<ProjectReference Include="..\..\Misc\Misc.vcxproj"> <ProjectReference Include="..\..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project> <Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\NetworkAPI\NetworkAPI.vcxproj">
<Project>{460d625f-2ac9-4559-b809-0ba89ceaedf4}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj"> <ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project> <Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="IServer.cpp" />
<ClCompile Include="ServerMain.cpp" /> <ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" /> <ClCompile Include="TestClass.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="IClient.h" />
<ClInclude Include="IServer.h" />
<ClInclude Include="RecieverObject.h" /> <ClInclude Include="RecieverObject.h" />
<ClInclude Include="TestClass.h" /> <ClInclude Include="TestClass.h" />
</ItemGroup> </ItemGroup>

View File

@ -18,20 +18,11 @@
<ClCompile Include="ServerMain.cpp"> <ClCompile Include="ServerMain.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="IServer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="TestClass.cpp"> <ClCompile Include="TestClass.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="IServer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IClient.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RecieverObject.h"> <ClInclude Include="RecieverObject.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

View File

@ -3,71 +3,24 @@
#include <vector> #include <vector>
#include <vld.h> #include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h"
#include "../NetworkDependencies/ThreadedClient.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/WinTimer.h"
#include "../../Misc/Utilities.h"
#include "../../Misc/Utilities-Impl.h"
#include "TestClass.h" #include "TestClass.h"
#include "IServer.h"
#include "IClient.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std; using namespace std;
using namespace Oyster::Network::Server;
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace Utility;
using namespace ::Utility::DynamicMemory;
void clientProc(IClient* client); void clientProc(Oyster::Network::NetworkClient* client);
int main() int main()
{ {
Test tests;
SmartPointer<OysterByte> sendBuffer = new OysterByte;
SmartPointer<OysterByte> recvBuffer = new OysterByte;
ProtocolSet* set = new ProtocolSet;
cout << "Server" << endl;
Translator t;
int errorCode = 0;
if(!InitWinSock()) if(!InitWinSock())
{ {
cout << "errorMessage: unable to start winsock" << endl; cout << "errorMessage: unable to start winsock" << endl;
} }
IServer server; Test test;
IServer::INIT_DESC initDesc;
initDesc.port = 9876;
initDesc.proc = clientProc;
server.Init(initDesc);
server.Start();
Sleep(1000); cout << "Server" << endl;
//Create a test protocol test.mainLoop();
ProtocolPlayerPos test;
test.clientID = 0;
test.ID = 5;
test.nrOfFloats = 10;
test.matrix = new float[test.nrOfFloats];
for(int i = 0; i < (int)test.nrOfFloats; i++)
{
test.matrix[i] = (float)i;
}
t.Pack(test, sendBuffer);
WinTimer timer;
while(1) while(1)
{ {
@ -92,21 +45,11 @@ int main()
//PrintOutMessage(set); //PrintOutMessage(set);
set->Release(); set->Release();
}*/ }*/
Sleep(1);
} }
server.Stop();
server.Shutdown();
//listener.Shutdown();
Sleep(1000); Sleep(1000);
system("pause"); system("pause");
return 0; return 0;
} }
void clientProc(IClient* client)
{
cout << "Proc" << endl;
clients.push_back(client);
}

View File

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
using namespace Oyster::Network; using namespace Oyster::Network;
using namespace ::Server;
using namespace ::Protocols; using namespace ::Protocols;
using namespace Utility; using namespace Utility;
using namespace ::DynamicMemory; using namespace ::DynamicMemory;
@ -11,12 +12,35 @@ using namespace std;
Test::Test() Test::Test()
{ {
recvPostBox = new PostBox<SmartPointer<OysterByte>>; recvPostBox = new PostBox<SmartPointer<OysterByte>>;
sendBuffer = new OysterByte;
recvBuffer = new OysterByte;
NetworkServer::INIT_DESC initDesc;
initDesc.port = 9876;
initDesc.proc = NULL;
server.Init(initDesc);
server.Start();
test.clientID = 0;
test.ID = 5;
test.nrOfFloats = 10;
test.matrix = new float[test.nrOfFloats];
for(int i = 0; i < (int)test.nrOfFloats; i++)
{
test.matrix[i] = (float)i;
}
t.Pack(test, sendBuffer);
} }
Test::~Test() Test::~Test()
{ {
for(int i = 0; i < (int)clients.size(); i++) for(int i = 0; i < (int)clients.size(); i++)
delete clients.at(i); delete clients.at(i);
server.Stop();
} }
void Test::ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg) void Test::ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg)

View File

@ -5,7 +5,8 @@
#include "../../Misc/Utilities.h" #include "../../Misc/Utilities.h"
#include "../NetworkDependencies/OysterByte.h" #include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h" #include "../NetworkDependencies/PostBox.h"
#include "IClient.h" #include "../NetworkAPI/NetworkClient.h"
#include "../NetworkAPI/NetworkServer.h"
#include "../NetworkDependencies/Translator.h" #include "../NetworkDependencies/Translator.h"
#include <vector> #include <vector>
@ -21,13 +22,15 @@ public:
void PrintOutMessage(Oyster::Network::Protocols::ProtocolSet* set); void PrintOutMessage(Oyster::Network::Protocols::ProtocolSet* set);
private: private:
std::vector<IClient*> clients; std::vector<Oyster::Network::NetworkClient*> clients;
Oyster::Network::IPostBox<Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte>> *recvPostBox; Oyster::Network::IPostBox<Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte>> *recvPostBox;
Oyster::Network::Translator t; Oyster::Network::Translator t;
Oyster::Network::Protocols::ProtocolPlayerPos test; Oyster::Network::Protocols::ProtocolPlayerPos test;
Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> sendBuffer;
Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> recvBuffer;
Oyster::Network::Server::NetworkServer server;
}; };