Client interface and class for both server and client. now using postbox system
This commit is contained in:
parent
10c786b745
commit
aa4cf634d3
|
@ -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
|
|
@ -158,11 +158,13 @@
|
|||
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||
<ClCompile Include="OysterByte.cpp" />
|
||||
<ClCompile Include="Packing.cpp" />
|
||||
<ClCompile Include="ThreadedClient.cpp" />
|
||||
<ClCompile Include="Translator.cpp" />
|
||||
<ClCompile Include="WinsockFunctions.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
<ClInclude Include="IClient.h" />
|
||||
<ClInclude Include="IConnection.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="IPostBox.h" />
|
||||
|
@ -175,6 +177,7 @@
|
|||
<ClInclude Include="ITranslate.h" />
|
||||
<ClInclude Include="PostBox.h" />
|
||||
<ClInclude Include="Protocols.h" />
|
||||
<ClInclude Include="ThreadedClient.h" />
|
||||
<ClInclude Include="Translator.h" />
|
||||
<ClInclude Include="WinsockFunctions.h" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
<ClCompile Include="Listener.cpp" />
|
||||
<ClCompile Include="WinsockFunctions.cpp" />
|
||||
<ClCompile Include="OysterByte.cpp" />
|
||||
<ClCompile Include="ThreadedClient.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
|
@ -27,5 +28,7 @@
|
|||
<ClInclude Include="OysterByte.h" />
|
||||
<ClInclude Include="IPostBox.h" />
|
||||
<ClInclude Include="PostBox.h" />
|
||||
<ClInclude Include="IClient.h" />
|
||||
<ClInclude Include="ThreadedClient.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -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();
|
||||
}
|
|
@ -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
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -6,15 +6,15 @@
|
|||
#include "..\NetworkDependencies\Protocols.h"
|
||||
#include "../NetworkDependencies/OysterByte.h"
|
||||
#include "../../Misc/ThreadSafeQueue.h"
|
||||
#include "Client.h"
|
||||
#include "../NetworkDependencies/ThreadedClient.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
using namespace std;
|
||||
using namespace Oyster::Network::Protocols;
|
||||
using namespace Oyster::Network::Client;
|
||||
using namespace Oyster::Network;
|
||||
|
||||
void chat(Client &client);
|
||||
void chat(ThreadedClient &client);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -27,7 +27,7 @@ int main()
|
|||
cout << "Client" << endl;
|
||||
|
||||
//Create Client
|
||||
Client client;
|
||||
ThreadedClient client;
|
||||
|
||||
//Connect to server
|
||||
errorCode = client.Connect(9876, "localhost");
|
||||
|
@ -38,7 +38,7 @@ int main()
|
|||
wcout << "errorMessage: " << errorTest << endl;
|
||||
}
|
||||
|
||||
chat(client);
|
||||
//chat(client);
|
||||
|
||||
ShutdownWinSock();
|
||||
|
||||
|
@ -46,7 +46,7 @@ int main()
|
|||
return 0;
|
||||
}
|
||||
|
||||
void chat(Client &client)
|
||||
void chat(ThreadedClient &client)
|
||||
{
|
||||
Oyster::Network::Translator *t = new Oyster::Network::Translator();
|
||||
|
||||
|
@ -65,7 +65,7 @@ void chat(Client &client)
|
|||
}
|
||||
|
||||
bool chatDone = false;
|
||||
|
||||
/*
|
||||
while(!chatDone)
|
||||
{
|
||||
client.Recv(msgRecv);
|
||||
|
@ -113,9 +113,10 @@ void chat(Client &client)
|
|||
}
|
||||
|
||||
cin.clear();*/
|
||||
|
||||
/*
|
||||
}
|
||||
|
||||
delete t;
|
||||
delete set;
|
||||
*/
|
||||
}
|
|
@ -154,12 +154,8 @@
|
|||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Client.cpp" />
|
||||
<ClCompile Include="ClientMain.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Client.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
|
|
@ -18,13 +18,5 @@
|
|||
<ClCompile Include="ClientMain.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Client.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Client.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -154,12 +154,8 @@
|
|||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Client.cpp" />
|
||||
<ClCompile Include="ServerMain.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Client.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
|
|
@ -18,13 +18,5 @@
|
|||
<ClCompile Include="ServerMain.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Client.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Client.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -5,7 +5,7 @@
|
|||
#include "../NetworkDependencies/WinsockFunctions.h"
|
||||
#include "../NetworkDependencies/Listener.h"
|
||||
#include "../NetworkDependencies/Translator.h"
|
||||
#include "Client.h"
|
||||
#include "../NetworkDependencies/ThreadedClient.h"
|
||||
#include "../NetworkDependencies/OysterByte.h"
|
||||
#include "../NetworkDependencies/PostBox.h"
|
||||
#include "../../Misc/WinTimer.h"
|
||||
|
@ -57,7 +57,7 @@ int main()
|
|||
|
||||
WinTimer timer;
|
||||
|
||||
vector<Client*> clients;
|
||||
vector<ThreadedClient*> clients;
|
||||
int client = -1;
|
||||
while(1)
|
||||
{
|
||||
|
@ -66,9 +66,9 @@ int main()
|
|||
if(client != -1)
|
||||
{
|
||||
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.
|
||||
|
@ -78,7 +78,7 @@ int main()
|
|||
timer.reset();
|
||||
for(int i = 0; i < (int)clients.size(); i++)
|
||||
{
|
||||
clients.at(i)->Send(recvBuffer);
|
||||
clients.at(i)->Send(&recvBuffer);
|
||||
}
|
||||
}
|
||||
Sleep(100);
|
||||
|
|
Loading…
Reference in New Issue