Client interface and class for both server and client. now using postbox system

This commit is contained in:
Sam Mario Svensson 2013-12-03 13:04:53 +01:00
parent 10c786b745
commit aa4cf634d3
15 changed files with 223 additions and 171 deletions

View File

@ -0,0 +1,28 @@
#ifndef NETWORK_DEPENDENCIES_I_CLIENT_H
#define NETWORK_DEPENDENCIES_I_CLIENT_H
//////////////////////////////////
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster
{
namespace Network
{
class IClient
{
public:
virtual ~IClient() {};
virtual int Send() = 0;
virtual int Recv() = 0;
private:
};
}
}
#endif

View File

@ -158,11 +158,13 @@
<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" />
<ClCompile Include="ThreadedClient.cpp" />
<ClCompile Include="Translator.cpp" /> <ClCompile Include="Translator.cpp" />
<ClCompile Include="WinsockFunctions.cpp" /> <ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
<ClInclude Include="IClient.h" />
<ClInclude Include="IConnection.h" /> <ClInclude Include="IConnection.h" />
<ClInclude Include="IListener.h" /> <ClInclude Include="IListener.h" />
<ClInclude Include="IPostBox.h" /> <ClInclude Include="IPostBox.h" />
@ -175,6 +177,7 @@
<ClInclude Include="ITranslate.h" /> <ClInclude Include="ITranslate.h" />
<ClInclude Include="PostBox.h" /> <ClInclude Include="PostBox.h" />
<ClInclude Include="Protocols.h" /> <ClInclude Include="Protocols.h" />
<ClInclude Include="ThreadedClient.h" />
<ClInclude Include="Translator.h" /> <ClInclude Include="Translator.h" />
<ClInclude Include="WinsockFunctions.h" /> <ClInclude Include="WinsockFunctions.h" />
</ItemGroup> </ItemGroup>

View File

@ -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="ThreadedClient.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
@ -27,5 +28,7 @@
<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="IClient.h" />
<ClInclude Include="ThreadedClient.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,123 @@
#include "ThreadedClient.h"
#include <iostream>
using namespace Oyster::Network;
using namespace Oyster::Thread;
ThreadedClient::ThreadedClient()
{
this->connection = new Connection();
this->sendPostBox = new PostBox<OysterByte*>();
this->recvPostBox = NULL;
}
ThreadedClient::ThreadedClient(unsigned int socket)
{
this->connection = new Connection(socket);
this->sendPostBox = new PostBox<OysterByte*>();
this->recvPostBox = NULL;
}
ThreadedClient::~ThreadedClient()
{
thread.Terminate();
delete this->connection;
this->connection = NULL;
this->recvPostBox = NULL;
if(sendPostBox != NULL)
{
delete sendPostBox;
this->sendPostBox = NULL;
}
}
int ThreadedClient::Send(OysterByte* byte)
{
this->sendPostBox->PostMessage(byte);
return 0;
}
int ThreadedClient::Send()
{
int errorCode = 0;
mutex.LockMutex();
if(!sendPostBox->IsFull())
{
OysterByte *temp = NULL;
sendPostBox->FetchMessage(temp);
errorCode = this->connection->Send(*temp);
mutex.UnlockMutex();
}
return errorCode;
}
int ThreadedClient::Recv()
{
int errorCode = 0;
mutex.LockMutex();
if(!recvPostBox->IsFull())
{
OysterByte *temp = NULL;
errorCode = this->connection->Recieve(*temp);
recvPostBox->PostMessage(temp);
mutex.UnlockMutex();
}
return errorCode;
}
void ThreadedClient::ThreadEntry()
{
std::cout<< "Thread started" << std::endl;
}
void ThreadedClient::ThreadExit()
{
std::cout << "Thread exit" << std::endl;
}
bool ThreadedClient::DoWork()
{
int errorCode;
errorCode = Send();
if(errorCode != 0)
{
return false;
}
errorCode = Recv();
if(errorCode != 0)
{
return false;
}
return true;
}
int ThreadedClient::Connect(unsigned short port, const char serverName[])
{
int errorCode;
if((errorCode = connection->InitiateClient()) != 0)
{
return errorCode;
}
else if((errorCode = connection->Connect(port, serverName)) != 0)
{
return errorCode;
}
thread.Create(this, true);
return 0;
}
void ThreadedClient::setRecvPostBox(IPostBox<OysterByte*>* postBox)
{
this->mutex.LockMutex();
this->recvPostBox = postBox;
this->mutex.UnlockMutex();
}

View File

@ -0,0 +1,52 @@
#ifndef NETWORK_DEPENDENCIES_THREADED_CLIENT_H
#define NETWORK_DEPENDENCIES_THREADED_CLIENT_H
//////////////////////////////////
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../NetworkDependencies/IClient.h"
#include "../../Misc/Thread/IThreadObject.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h"
namespace Oyster
{
namespace Network
{
class ThreadedClient : public IClient, public Thread::IThreadObject
{
public:
ThreadedClient();
ThreadedClient(unsigned int socket);
virtual ~ThreadedClient();
int Send(OysterByte* byte);
int Connect(unsigned short port, const char serverName[]);
void setRecvPostBox(IPostBox<OysterByte*>* postBox);
private:
virtual int Send();
virtual int Recv();
virtual void ThreadEntry();
virtual void ThreadExit();
virtual bool DoWork();
Connection* connection;
IPostBox<OysterByte*>* sendPostBox;
IPostBox<OysterByte*>* recvPostBox;
Oyster::Thread::OysterThread thread;
OysterMutex mutex;
};
}
}
#endif

View File

@ -1,41 +0,0 @@
#include "Client.h"
using namespace Oyster::Network::Client;
Client::Client()
{
connection = new Connection();
}
Client::~Client()
{
delete this->connection;
connection = 0;
}
int Client::Connect(unsigned int port, char filename[])
{
int errorCode;
if((errorCode = connection->InitiateClient()) != 0)
{
return errorCode;
}
if((errorCode = connection->Connect(port, filename)) != 0)
{
return errorCode;
}
return 0;
}
void Client::Send(Oyster::Network::OysterByte& bytes)
{
connection->Send(bytes);
}
void Client::Recv(Oyster::Network::OysterByte& bytes)
{
connection->Recieve(bytes);
}

View File

@ -1,35 +0,0 @@
#ifndef NETWORK_CLIENT_CLIENT_H
#define NETWORK_CLIENT_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster
{
namespace Network
{
namespace Client
{
class Client
{
public:
Client();
~Client();
int Connect(unsigned int port, char filename[]);
void Send(OysterByte& bytes);
void Recv(OysterByte& bytes);
private:
::Oyster::Network::Connection* connection;
};
}
}
}
#endif

View File

@ -6,15 +6,15 @@
#include "..\NetworkDependencies\Protocols.h" #include "..\NetworkDependencies\Protocols.h"
#include "../NetworkDependencies/OysterByte.h" #include "../NetworkDependencies/OysterByte.h"
#include "../../Misc/ThreadSafeQueue.h" #include "../../Misc/ThreadSafeQueue.h"
#include "Client.h" #include "../NetworkDependencies/ThreadedClient.h"
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
using namespace std; using namespace std;
using namespace Oyster::Network::Protocols; using namespace Oyster::Network::Protocols;
using namespace Oyster::Network::Client; using namespace Oyster::Network;
void chat(Client &client); void chat(ThreadedClient &client);
int main() int main()
{ {
@ -27,7 +27,7 @@ int main()
cout << "Client" << endl; cout << "Client" << endl;
//Create Client //Create Client
Client client; ThreadedClient client;
//Connect to server //Connect to server
errorCode = client.Connect(9876, "localhost"); errorCode = client.Connect(9876, "localhost");
@ -38,7 +38,7 @@ int main()
wcout << "errorMessage: " << errorTest << endl; wcout << "errorMessage: " << errorTest << endl;
} }
chat(client); //chat(client);
ShutdownWinSock(); ShutdownWinSock();
@ -46,7 +46,7 @@ int main()
return 0; return 0;
} }
void chat(Client &client) void chat(ThreadedClient &client)
{ {
Oyster::Network::Translator *t = new Oyster::Network::Translator(); Oyster::Network::Translator *t = new Oyster::Network::Translator();
@ -65,7 +65,7 @@ void chat(Client &client)
} }
bool chatDone = false; bool chatDone = false;
/*
while(!chatDone) while(!chatDone)
{ {
client.Recv(msgRecv); client.Recv(msgRecv);
@ -113,9 +113,10 @@ void chat(Client &client)
} }
cin.clear();*/ cin.clear();*/
/*
} }
delete t; delete t;
delete set; delete set;
*/
} }

View File

@ -154,12 +154,8 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="ClientMain.cpp" /> <ClCompile Include="ClientMain.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -18,13 +18,5 @@
<ClCompile Include="ClientMain.cpp"> <ClCompile Include="ClientMain.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,24 +0,0 @@
#include "Client.h"
using namespace Oyster::Network;
using namespace Oyster::Network::Server;
Client::Client(unsigned int socket)
{
connection = new Connection(socket);
}
Client::~Client()
{
delete connection;
}
void Client::Send(OysterByte& bytes)
{
connection->Send(bytes);
}
void Client::Recv(OysterByte& bytes)
{
connection->Recieve(bytes);
}

View File

@ -1,34 +0,0 @@
#ifndef NETWORK_SERVER_CLIENT_H
#define NETWORK_SERVER_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster
{
namespace Network
{
namespace Server
{
class Client
{
public:
Client(unsigned int socket);
~Client();
void Send(OysterByte& bytes);
void Recv(OysterByte& bytes);
private:
::Oyster::Network::Connection* connection;
};
}
}
};
#endif

View File

@ -154,12 +154,8 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="ServerMain.cpp" /> <ClCompile Include="ServerMain.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -18,13 +18,5 @@
<ClCompile Include="ServerMain.cpp"> <ClCompile Include="ServerMain.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -5,7 +5,7 @@
#include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h" #include "../NetworkDependencies/Translator.h"
#include "Client.h" #include "../NetworkDependencies/ThreadedClient.h"
#include "../NetworkDependencies/OysterByte.h" #include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h" #include "../NetworkDependencies/PostBox.h"
#include "../../Misc/WinTimer.h" #include "../../Misc/WinTimer.h"
@ -57,7 +57,7 @@ int main()
WinTimer timer; WinTimer timer;
vector<Client*> clients; vector<ThreadedClient*> clients;
int client = -1; int client = -1;
while(1) while(1)
{ {
@ -66,9 +66,9 @@ int main()
if(client != -1) if(client != -1)
{ {
cout << "Client connected: " << client << endl; cout << "Client connected: " << client << endl;
clients.push_back(new Client(client)); clients.push_back(new ThreadedClient(client));
clients.at(clients.size()-1)->Send(recvBuffer); clients.at(clients.size()-1)->Send(&recvBuffer);
} }
//Send a message every 1 secounds to all clients. //Send a message every 1 secounds to all clients.
@ -78,7 +78,7 @@ int main()
timer.reset(); timer.reset();
for(int i = 0; i < (int)clients.size(); i++) for(int i = 0; i < (int)clients.size(); i++)
{ {
clients.at(i)->Send(recvBuffer); clients.at(i)->Send(&recvBuffer);
} }
} }
Sleep(100); Sleep(100);