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

View File

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

View File

@ -21,7 +21,7 @@ OysterByte::OysterByte(const OysterByte& obj)
//delete[] this->byteArray;
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];
}
@ -85,7 +85,7 @@ OysterByte& OysterByte::operator =(const OysterByte& obj)
delete[] this->byteArray;
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];
}

View File

@ -100,7 +100,7 @@ namespace Oyster
//string
void Pack(unsigned char buffer[], char str[])
{
short len = strlen(str);
short len = (short)strlen(str);
Pack(buffer, len);
buffer += 2;
memcpy(buffer, str, len);
@ -108,7 +108,7 @@ namespace Oyster
void Pack(unsigned char buffer[], std::string& str)
{
short len = str.length();
short len = (short)str.length();
Pack(buffer, len);
buffer += 2;
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">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkAPI\NetworkAPI.vcxproj">
<Project>{460d625f-2ac9-4559-b809-0ba89ceaedf4}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="IServer.cpp" />
<ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="IClient.h" />
<ClInclude Include="IServer.h" />
<ClInclude Include="RecieverObject.h" />
<ClInclude Include="TestClass.h" />
</ItemGroup>

View File

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

View File

@ -3,71 +3,24 @@
#include <vector>
#include <vld.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 "IServer.h"
#include "IClient.h"
#pragma comment(lib, "ws2_32.lib")
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()
{
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())
{
cout << "errorMessage: unable to start winsock" << endl;
}
IServer server;
IServer::INIT_DESC initDesc;
initDesc.port = 9876;
initDesc.proc = clientProc;
server.Init(initDesc);
server.Start();
Test test;
Sleep(1000);
cout << "Server" << endl;
//Create a test protocol
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;
test.mainLoop();
while(1)
{
@ -92,21 +45,11 @@ int main()
//PrintOutMessage(set);
set->Release();
}*/
Sleep(1);
}
server.Stop();
server.Shutdown();
//listener.Shutdown();
Sleep(1000);
system("pause");
return 0;
}
void clientProc(IClient* client)
{
cout << "Proc" << endl;
clients.push_back(client);
}

View File

@ -3,6 +3,7 @@
#include <iostream>
using namespace Oyster::Network;
using namespace ::Server;
using namespace ::Protocols;
using namespace Utility;
using namespace ::DynamicMemory;
@ -11,12 +12,35 @@ using namespace std;
Test::Test()
{
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()
{
for(int i = 0; i < (int)clients.size(); i++)
delete clients.at(i);
server.Stop();
}
void Test::ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg)

View File

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