Merge branch 'Network' of https://github.com/dean11/Danbias into Network
Conflicts: Code/Network/NetworkDependencies/Connection.cpp Code/Network/NetworkDependencies/Connection.h Code/Network/NetworkDependencies/IConnection.h Code/Network/OysterNetworkClient/Client.cpp Code/Network/OysterNetworkClient/Client.h Code/Network/OysterNetworkClient/ClientMain.cpp Code/Network/OysterNetworkServer/Client.cpp Code/Network/OysterNetworkServer/Client.h Code/Network/OysterNetworkServer/ServerMain.cpp
This commit is contained in:
commit
aeddc032d9
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace Oyster::Network;
|
using namespace Oyster::Network;
|
||||||
|
|
||||||
|
@ -10,13 +11,12 @@ Connection::~Connection()
|
||||||
closesocket( this->socket );
|
closesocket( this->socket );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Connection::Connect(unsigned short port , const char serverName[])
|
int Connection::Connect(unsigned short port , const char serverName[])
|
||||||
{
|
{
|
||||||
struct hostent *hostEnt;
|
struct hostent *hostEnt;
|
||||||
if((hostEnt = gethostbyname(serverName)) == NULL)
|
if((hostEnt = gethostbyname(serverName)) == NULL)
|
||||||
{
|
{
|
||||||
//couldn't find host
|
return WSAGetLastError();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
|
@ -26,20 +26,20 @@ bool Connection::Connect(unsigned short port , const char serverName[])
|
||||||
|
|
||||||
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//Error connecting to server
|
return WSAGetLastError();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//connection succesfull!
|
//connection succesfull!
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Connection::InitiateServer(unsigned short port)
|
int Connection::InitiateServer(unsigned short port)
|
||||||
{
|
{
|
||||||
if(!initiateSocket())
|
int errorCode = 0;
|
||||||
|
|
||||||
|
if((errorCode = initiateSocket()) != 0)
|
||||||
{
|
{
|
||||||
//Error opening socket!
|
return errorCode;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
|
@ -49,36 +49,35 @@ bool Connection::InitiateServer(unsigned short port)
|
||||||
|
|
||||||
if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//Bind failed!;
|
errorCode = WSAGetLastError();
|
||||||
closesocket(this->socket);
|
closesocket(this->socket);
|
||||||
return false;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
//not our Listen function! its trying to keep our socket open for connections
|
//not our Listen function! its trying to keep our socket open for connections
|
||||||
if(listen(this->socket, 5) == SOCKET_ERROR)
|
if(listen(this->socket, 5) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//"Listen failed!
|
errorCode = WSAGetLastError();
|
||||||
closesocket(this->socket);
|
closesocket(this->socket);
|
||||||
return false;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Server started!
|
//Server started!
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Connection::InitiateClient()
|
int Connection::InitiateClient()
|
||||||
{
|
{
|
||||||
if(!initiateSocket())
|
int errorCode;
|
||||||
{
|
return initiateSocket();
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::Disconnect()
|
int Connection::Disconnect()
|
||||||
{
|
{
|
||||||
closesocket(this->socket);
|
closesocket(this->socket);
|
||||||
|
|
||||||
|
return WSAGetLastError();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Connection::Send(OysterByte& bytes)
|
bool Connection::Send(OysterByte& bytes)
|
||||||
|
@ -88,13 +87,10 @@ bool Connection::Send(OysterByte& bytes)
|
||||||
nBytes = send(this->socket, bytes, bytes.GetSize(), 0);
|
nBytes = send(this->socket, bytes, bytes.GetSize(), 0);
|
||||||
if(nBytes == SOCKET_ERROR)
|
if(nBytes == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//Send failed!
|
return WSAGetLastError();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Size of the sent data: " << nBytes << " bytes" << std::endl;
|
return 0;
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Connection::Recieve(OysterByte& bytes)
|
int Connection::Recieve(OysterByte& bytes)
|
||||||
|
@ -105,8 +101,7 @@ int Connection::Recieve(OysterByte& bytes)
|
||||||
nBytes = recv(this->socket, bytes, 500, 0);
|
nBytes = recv(this->socket, bytes, 500, 0);
|
||||||
if(nBytes == SOCKET_ERROR)
|
if(nBytes == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//Recv failed
|
return WSAGetLastError();
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -117,7 +112,7 @@ int Connection::Recieve(OysterByte& bytes)
|
||||||
|
|
||||||
//bytes.byteArray[nBytes] = '\0';
|
//bytes.byteArray[nBytes] = '\0';
|
||||||
|
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Connection::Listen()
|
int Connection::Listen()
|
||||||
|
@ -125,8 +120,7 @@ int Connection::Listen()
|
||||||
int clientSocket;
|
int clientSocket;
|
||||||
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
|
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
//failed
|
return WSAGetLastError();
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientSocket;
|
return clientSocket;
|
||||||
|
@ -135,14 +129,13 @@ int Connection::Listen()
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
//Private functions
|
//Private functions
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
bool Connection::initiateSocket()
|
int Connection::initiateSocket()
|
||||||
{
|
{
|
||||||
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if(this->socket == SOCKET_ERROR)
|
if(this->socket == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
//error opening socket
|
return WSAGetLastError();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,18 +20,18 @@ namespace Oyster
|
||||||
Connection(int socket) { this->socket = socket; };
|
Connection(int socket) { this->socket = socket; };
|
||||||
virtual ~Connection();
|
virtual ~Connection();
|
||||||
|
|
||||||
virtual bool InitiateServer( unsigned short port );
|
virtual int InitiateServer( unsigned short port );
|
||||||
virtual bool InitiateClient();
|
virtual int InitiateClient();
|
||||||
|
|
||||||
virtual bool Send( OysterByte& bytes );
|
virtual bool Send( OysterByte& bytes );
|
||||||
virtual int Recieve( OysterByte& bytes );
|
virtual int Recieve( OysterByte& bytes );
|
||||||
|
|
||||||
virtual void Disconnect();
|
virtual int Disconnect();
|
||||||
virtual bool Connect( unsigned short port , const char serverName[] );
|
virtual int Connect( unsigned short port , const char serverName[] );
|
||||||
virtual int Listen();
|
virtual int Listen();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool initiateSocket();
|
int initiateSocket();
|
||||||
|
|
||||||
int socket;
|
int socket;
|
||||||
|
|
||||||
|
|
|
@ -14,13 +14,13 @@ namespace Oyster
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void Disconnect() = 0;
|
virtual int Disconnect() = 0;
|
||||||
virtual bool Send( OysterByte& bytes ) = 0;
|
virtual bool Send( OysterByte& bytes ) = 0;
|
||||||
virtual int Recieve( OysterByte& bytes) = 0;
|
virtual int Recieve( OysterByte& bytes) = 0;
|
||||||
virtual bool InitiateServer( unsigned short port ) { return false; };
|
virtual int InitiateServer( unsigned short port ) { return false; };
|
||||||
virtual bool InitiateClient() { return false; };
|
virtual int InitiateClient() { return false; };
|
||||||
virtual int Listen() { return -1; };
|
virtual int Listen() { return -1; };
|
||||||
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
|
virtual int Connect( unsigned short port, const char serverName[] ) { return false; };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,37 @@
|
||||||
#include "WinsockFunctions.h"
|
#include "WinsockFunctions.h"
|
||||||
#include <WinSock2.h>
|
#include <WinSock2.h>
|
||||||
|
|
||||||
bool InitSockets()
|
bool InitWinSock()
|
||||||
{
|
{
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
|
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShutdownSockets()
|
void ShutdownWinSock()
|
||||||
{
|
{
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::wstring GetErrorMessage(int errorCode)
|
||||||
|
{
|
||||||
|
LPWSTR lpMessage;
|
||||||
|
std::wstring retVal(L"Succesful");
|
||||||
|
|
||||||
|
DWORD bufLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,
|
||||||
|
NULL,
|
||||||
|
errorCode ,
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ,
|
||||||
|
(LPWSTR)&lpMessage,
|
||||||
|
0 ,
|
||||||
|
NULL );
|
||||||
|
|
||||||
|
if(bufLen)
|
||||||
|
{
|
||||||
|
retVal = lpMessage;
|
||||||
|
|
||||||
|
LocalFree(lpMessage);
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,8 +4,12 @@
|
||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
// Created by Pontus Fransson 2013 //
|
// Created by Pontus Fransson 2013 //
|
||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
|
#include <string>
|
||||||
|
|
||||||
void ShutdownSockets();
|
|
||||||
bool InitSockets();
|
void ShutdownWinSock();
|
||||||
|
bool InitWinSock();
|
||||||
|
|
||||||
|
std::wstring GetErrorMessage(int errorCode);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -13,12 +13,21 @@ Client::~Client()
|
||||||
connection = 0;
|
connection = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Client::Connect(unsigned int port, char filename[])
|
int Client::Connect(unsigned int port, char filename[])
|
||||||
{
|
{
|
||||||
connection->InitiateClient();
|
int errorCode;
|
||||||
connection->Connect(port, filename);
|
|
||||||
|
|
||||||
return true;
|
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)
|
void Client::Send(Oyster::Network::OysterByte& bytes)
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace Oyster
|
||||||
Client();
|
Client();
|
||||||
~Client();
|
~Client();
|
||||||
|
|
||||||
bool Connect(unsigned int port, char filename[]);
|
int Connect(unsigned int port, char filename[]);
|
||||||
|
|
||||||
void Send(OysterByte& bytes);
|
void Send(OysterByte& bytes);
|
||||||
void Recv(OysterByte& bytes);
|
void Recv(OysterByte& bytes);
|
||||||
|
|
|
@ -17,9 +17,11 @@ void chat(Client &client);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
int errorCode;
|
||||||
|
|
||||||
char msgRecv[255] = "\0";
|
char msgRecv[255] = "\0";
|
||||||
|
|
||||||
InitSockets();
|
InitWinSock();
|
||||||
|
|
||||||
cout << "Client" << endl;
|
cout << "Client" << endl;
|
||||||
|
|
||||||
|
@ -27,11 +29,17 @@ int main()
|
||||||
Client client;
|
Client client;
|
||||||
|
|
||||||
//Connect to server
|
//Connect to server
|
||||||
client.Connect(9876, "localhost");
|
errorCode = client.Connect(9876, "localhost");
|
||||||
|
|
||||||
|
if(errorCode != 0)
|
||||||
|
{
|
||||||
|
wstring errorTest = GetErrorMessage(errorCode);
|
||||||
|
wcout << "errorMessage: " << errorTest << endl;
|
||||||
|
}
|
||||||
|
|
||||||
chat(client);
|
chat(client);
|
||||||
|
|
||||||
ShutdownSockets();
|
ShutdownWinSock();
|
||||||
|
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -20,10 +20,11 @@ int main()
|
||||||
|
|
||||||
cout << "Server" << endl;
|
cout << "Server" << endl;
|
||||||
Translator t;
|
Translator t;
|
||||||
|
int errorCode;
|
||||||
|
|
||||||
if(!InitSockets())
|
if(!InitWinSock())
|
||||||
{
|
{
|
||||||
cout << "Sockets failed to initialize" << endl;
|
cout << "errorMessage: unable to start winsock" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create socket
|
//Create socket
|
||||||
|
@ -88,7 +89,8 @@ int main()
|
||||||
client1.Send(recvBuffer);
|
client1.Send(recvBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
ShutdownSockets();
|
|
||||||
|
ShutdownWinSock();
|
||||||
delete set;
|
delete set;
|
||||||
|
|
||||||
system("pause");
|
system("pause");
|
||||||
|
|
Loading…
Reference in New Issue