From 640ac2172a90d47ea868907ffadb849028da1a70 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Mon, 25 Nov 2013 11:39:38 +0100 Subject: [PATCH 1/3] checked the coding standard and changed it accordinly --- .../NetworkDependencies/Connection.cpp | 37 +++++++++---------- Code/Network/NetworkDependencies/Connection.h | 15 ++++---- .../Network/NetworkDependencies/IConnection.h | 7 ++-- Code/Network/NetworkDependencies/ITranslate.h | 4 ++ Code/Network/NetworkDependencies/Protocols.h | 4 ++ Code/Network/NetworkDependencies/Translator.h | 4 ++ 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index cac38e71..08700b8c 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -3,21 +3,17 @@ #include #include -using namespace Oyster::Network; +using namespace Oyster::Network; Connection::~Connection() { - if(mySocket != NULL) - { - closesocket( mySocket ); - mySocket = NULL; - } + closesocket( this->socket ); } bool Connection::Connect(unsigned short port , const char serverName[]) { - mySocket = socket(AF_INET, SOCK_STREAM, 0); - if(mySocket == SOCKET_ERROR) + this->socket = ::socket(AF_INET, SOCK_STREAM, 0); + if(this->socket == SOCKET_ERROR) { //error opening socket return false; @@ -30,6 +26,7 @@ bool Connection::Connect(unsigned short port , const char serverName[]) //couldn't find host return false; } + struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(port); @@ -37,7 +34,7 @@ bool Connection::Connect(unsigned short port , const char serverName[]) while(1) { - if(connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) + if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { //Error connecting to server return false; @@ -56,8 +53,8 @@ bool Connection::Connect(unsigned short port , const char serverName[]) bool Connection::InitiateServer(unsigned short port) { - mySocket = socket(AF_INET, SOCK_STREAM, 0); - if(mySocket == SOCKET_ERROR) + this->socket = ::socket(AF_INET, SOCK_STREAM, 0); + if(this->socket == SOCKET_ERROR) { //Error opening socket! return false; @@ -68,28 +65,28 @@ bool Connection::InitiateServer(unsigned short port) server.sin_port = htons(port); server.sin_addr.s_addr = INADDR_ANY; - if(bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) + if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { //Bind failed!; - closesocket(mySocket); + closesocket(this->socket); return false; } //not our Listen function! its trying to keep our socket open for connections - if(listen(mySocket, 5) == SOCKET_ERROR) + if(listen(this->socket, 5) == SOCKET_ERROR) { //"Listen failed! - closesocket(mySocket); + closesocket(this->socket); return -1; } //Server started! - return mySocket; + return this->socket; } void Connection::Disconnect() { - closesocket(mySocket); + closesocket(this->socket); } bool Connection::Send(const unsigned char message[]) @@ -98,7 +95,7 @@ bool Connection::Send(const unsigned char message[]) unsigned long messageSize = strlen((char*)message); messageSize = 255; - nBytes = send(mySocket, (char*)message , messageSize, 0); + nBytes = send(this->socket, (char*)message , messageSize, 0); if(nBytes == SOCKET_ERROR) { //Send failed! @@ -111,7 +108,7 @@ bool Connection::Send(const unsigned char message[]) int Connection::Recieve(unsigned char message[]) { int nBytes; - nBytes = recv(mySocket, (char*)message , 255, 0); + nBytes = recv(this->socket, (char*)message , 255, 0); if(nBytes == SOCKET_ERROR) { //Recv failed @@ -126,7 +123,7 @@ int Connection::Recieve(unsigned char message[]) int Connection::Listen() { int clientSocket; - if((clientSocket = accept(mySocket, NULL, NULL)) == INVALID_SOCKET) + if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) { //failed return -1; diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 109d761c..34fd7ff4 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -1,10 +1,10 @@ +#ifndef NETWORK_DEPENDENCIES_CONNECTION_H +#define NETWORK_DEPENDENCIES_CONNECTION_H + ////////////////////////////////// // Created by Sam Svensson 2013 // ////////////////////////////////// -#ifndef NETWORK_DEPENDENCIES_CONNECTION_H -#define NETWORK_DEPENDENCIES_CONNECTION_H - #include "IConnection.h" namespace Oyster @@ -15,12 +15,13 @@ namespace Oyster { public: - Connection() { mySocket = 0; }; - Connection(int socket) { mySocket = socket; }; - ~Connection(); + Connection() { this->socket = 0; }; + Connection(int socket) { this->socket = socket; }; + virtual ~Connection(); virtual bool Connect( unsigned short port , const char serverName[] ); virtual bool InitiateServer( unsigned short port ); + //virutal bool initiateClient(); virtual void Disconnect(); @@ -30,7 +31,7 @@ namespace Oyster virtual int Listen(); private: - int mySocket; + int socket; }; } diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 91b7c7b4..8b934a20 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -1,10 +1,10 @@ +#ifndef NETWORK_DEPENDENCIES_I_CONNECTION_H +#define NETWORK_DEPENDENCIES_I_CONNECTION_H + ////////////////////////////////// // Created by Sam Svensson 2013 // ////////////////////////////////// -#ifndef NETWORK_DEPENDENCIES_I_CONNECTION_H -#define NETWORK_DEPENDENCIES_I_CONNECTION_H - namespace Oyster { namespace Network @@ -17,6 +17,7 @@ namespace Oyster virtual bool Send( const unsigned char message[] ) = 0; virtual int Recieve(unsigned char message[]) = 0; virtual bool InitiateServer( unsigned short port ) { return false; }; + virtual bool initiateClient( unsigned short port ) { return false; }; virtual int Listen() { return -1; }; virtual bool Connect( unsigned short port, const char serverName[] ) { return false; }; }; diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index 63f18e68..4fddb643 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -1,6 +1,10 @@ #ifndef NETWORK_DEPENDENCIES_I_TRANSLATE #define NETWORK_DEPENDENCIES_I_TRANSLATE +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + namespace Oyster { namespace Network diff --git a/Code/Network/NetworkDependencies/Protocols.h b/Code/Network/NetworkDependencies/Protocols.h index dcddf797..cb5ad8d1 100644 --- a/Code/Network/NetworkDependencies/Protocols.h +++ b/Code/Network/NetworkDependencies/Protocols.h @@ -1,6 +1,10 @@ #ifndef NETWORK_DEPENDENCIES_PROTOCOLS_H #define NETWORK_DEPENDENCIES_PROTOCOLS_H +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + #include namespace Oyster diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 26b5a56c..0e496429 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -1,6 +1,10 @@ #ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H #define NETWORK_DEPENDENCIES_TRANSLATOR_H +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + #include "Messages/MessagesInclude.h" #include "Protocols.h" #include "ITranslate.h" From 82b8ef7b05b50c3fe292e577cb17215e9154cae7 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Mon, 25 Nov 2013 12:14:01 +0100 Subject: [PATCH 2/3] Fixed connection split up connect function and added initiateClient() --- .../NetworkDependencies/Connection.cpp | 39 ++++++++----------- Code/Network/NetworkDependencies/Connection.h | 7 ++-- .../Network/NetworkDependencies/IConnection.h | 2 +- Code/Network/OysterNetworkClient/Client.cpp | 1 + 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 08700b8c..26db50f8 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -12,14 +12,6 @@ Connection::~Connection() bool Connection::Connect(unsigned short port , const char serverName[]) { - this->socket = ::socket(AF_INET, SOCK_STREAM, 0); - if(this->socket == SOCKET_ERROR) - { - //error opening socket - return false; - } - - struct hostent *hostEnt; if((hostEnt = gethostbyname(serverName)) == NULL) { @@ -32,19 +24,10 @@ bool Connection::Connect(unsigned short port , const char serverName[]) server.sin_port = htons(port); server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr; - while(1) + 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 false; - } - - else - { - break; - } - Sleep(10); + //Error connecting to server + return false; } //connection succesfull! @@ -77,11 +60,23 @@ bool Connection::InitiateServer(unsigned short port) { //"Listen failed! closesocket(this->socket); - return -1; + return false; } //Server started! - return this->socket; + return true; +} + +bool Connection::InitiateClient() +{ + this->socket = ::socket(AF_INET, SOCK_STREAM, 0); + if(this->socket == SOCKET_ERROR) + { + //error opening socket + return false; + } + + return true; } void Connection::Disconnect() diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 34fd7ff4..6c679371 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -19,15 +19,14 @@ namespace Oyster Connection(int socket) { this->socket = socket; }; virtual ~Connection(); - virtual bool Connect( unsigned short port , const char serverName[] ); virtual bool InitiateServer( unsigned short port ); - //virutal bool initiateClient(); - - virtual void Disconnect(); + virtual bool InitiateClient(); virtual bool Send(const unsigned char message[]); virtual int Recieve(unsigned char message[]); + virtual void Disconnect(); + virtual bool Connect( unsigned short port , const char serverName[] ); virtual int Listen(); private: diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 8b934a20..51b7631d 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -17,7 +17,7 @@ namespace Oyster virtual bool Send( const unsigned char message[] ) = 0; virtual int Recieve(unsigned char message[]) = 0; virtual bool InitiateServer( unsigned short port ) { return false; }; - virtual bool initiateClient( unsigned short port ) { return false; }; + virtual bool InitiateClient() { return false; }; virtual int Listen() { return -1; }; virtual bool Connect( unsigned short port, const char serverName[] ) { return false; }; }; diff --git a/Code/Network/OysterNetworkClient/Client.cpp b/Code/Network/OysterNetworkClient/Client.cpp index d77b1bf7..abe36d71 100644 --- a/Code/Network/OysterNetworkClient/Client.cpp +++ b/Code/Network/OysterNetworkClient/Client.cpp @@ -15,6 +15,7 @@ Client::~Client() bool Client::Connect(unsigned int port, char filename[]) { + connection->InitiateClient(); connection->Connect(port, filename); return true; From ca5e578af5c79e00aaf8162e7adf98efb5020d79 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Mon, 25 Nov 2013 14:03:32 +0100 Subject: [PATCH 3/3] reconstruction in connection. --- .../NetworkDependencies/Connection.cpp | 22 ++++++++++++++----- Code/Network/NetworkDependencies/Connection.h | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 26db50f8..792654f8 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -36,8 +36,7 @@ bool Connection::Connect(unsigned short port , const char serverName[]) bool Connection::InitiateServer(unsigned short port) { - this->socket = ::socket(AF_INET, SOCK_STREAM, 0); - if(this->socket == SOCKET_ERROR) + if(!initiateSocket()) { //Error opening socket! return false; @@ -69,10 +68,8 @@ bool Connection::InitiateServer(unsigned short port) bool Connection::InitiateClient() { - this->socket = ::socket(AF_INET, SOCK_STREAM, 0); - if(this->socket == SOCKET_ERROR) + if(!initiateSocket()) { - //error opening socket return false; } @@ -126,3 +123,18 @@ int Connection::Listen() return clientSocket; } + +/////////////////////////////////////// +//Private functions +/////////////////////////////////////// +bool Connection::initiateSocket() +{ + this->socket = ::socket(AF_INET, SOCK_STREAM, 0); + if(this->socket == SOCKET_ERROR) + { + //error opening socket + return false; + } + + return true; +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 6c679371..a0019a26 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -30,6 +30,8 @@ namespace Oyster virtual int Listen(); private: + bool initiateSocket(); + int socket; };