Merge branch 'Network' of https://github.com/dean11/Danbias into Network

Conflicts:
	Code/Network/NetworkDependencies/Connection.cpp
This commit is contained in:
Pontus Fransson 2013-11-25 19:05:28 +01:00
commit 6b57815bb8
7 changed files with 74 additions and 53 deletions

View File

@ -3,51 +3,31 @@
#include <winsock2.h> #include <winsock2.h>
#include <iostream> #include <iostream>
using namespace Oyster::Network; using namespace Oyster::Network;
Connection::~Connection() Connection::~Connection()
{ {
if(mySocket != NULL) closesocket( this->socket );
{
closesocket( mySocket );
mySocket = NULL;
}
} }
bool Connection::Connect(unsigned short port , const char serverName[]) bool Connection::Connect(unsigned short port , const char serverName[])
{ {
mySocket = socket(AF_INET, SOCK_STREAM, 0);
if(mySocket == SOCKET_ERROR)
{
//error opening socket
return false;
}
struct hostent *hostEnt; struct hostent *hostEnt;
if((hostEnt = gethostbyname(serverName)) == NULL) if((hostEnt = gethostbyname(serverName)) == NULL)
{ {
//couldn't find host //couldn't find host
return false; return false;
} }
struct sockaddr_in server; struct sockaddr_in server;
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons(port); server.sin_port = htons(port);
server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr; server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr;
while(1) if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{ {
if(connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) //Error connecting to server
{ return false;
//Error connecting to server
return false;
}
else
{
break;
}
Sleep(10);
} }
//connection succesfull! //connection succesfull!
@ -56,8 +36,7 @@ bool Connection::Connect(unsigned short port , const char serverName[])
bool Connection::InitiateServer(unsigned short port) bool Connection::InitiateServer(unsigned short port)
{ {
mySocket = socket(AF_INET, SOCK_STREAM, 0); if(!initiateSocket())
if(mySocket == SOCKET_ERROR)
{ {
//Error opening socket! //Error opening socket!
return false; return false;
@ -68,28 +47,38 @@ bool Connection::InitiateServer(unsigned short port)
server.sin_port = htons(port); server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY; 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!; //Bind failed!;
closesocket(mySocket); closesocket(this->socket);
return false; return false;
} }
//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(mySocket, 5) == SOCKET_ERROR) if(listen(this->socket, 5) == SOCKET_ERROR)
{ {
//"Listen failed! //"Listen failed!
closesocket(mySocket); closesocket(this->socket);
return -1; return false;
} }
//Server started! //Server started!
return mySocket; return true;
}
bool Connection::InitiateClient()
{
if(!initiateSocket())
{
return false;
}
return true;
} }
void Connection::Disconnect() void Connection::Disconnect()
{ {
closesocket(mySocket); closesocket(this->socket);
} }
bool Connection::Send(const unsigned char message[]) bool Connection::Send(const unsigned char message[])
@ -98,10 +87,10 @@ bool Connection::Send(const unsigned char message[])
unsigned long messageSize = strlen((char*)message); unsigned long messageSize = strlen((char*)message);
int optlen = sizeof(int); int optlen = sizeof(int);
int optval = 0; int optval = 0;
getsockopt(mySocket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
messageSize = 1000; messageSize = 255;
nBytes = send(mySocket, (char*)message , messageSize, 0); nBytes = send(this->socket, (char*)message , messageSize, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Send failed! //Send failed!
@ -114,7 +103,8 @@ bool Connection::Send(const unsigned char message[])
int Connection::Recieve(unsigned char message[]) int Connection::Recieve(unsigned char message[])
{ {
int nBytes; int nBytes;
nBytes = recv(mySocket, (char*)message , 10000, 0);
nBytes = recv(this->socket, (char*)message , 255, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Recv failed //Recv failed
@ -129,7 +119,7 @@ int Connection::Recieve(unsigned char message[])
int Connection::Listen() int Connection::Listen()
{ {
int clientSocket; int clientSocket;
if((clientSocket = accept(mySocket, NULL, NULL)) == INVALID_SOCKET) if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
{ {
//failed //failed
return -1; return -1;
@ -137,3 +127,18 @@ int Connection::Listen()
return clientSocket; 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;
}

View File

@ -1,10 +1,10 @@
#ifndef NETWORK_DEPENDENCIES_CONNECTION_H
#define NETWORK_DEPENDENCIES_CONNECTION_H
////////////////////////////////// //////////////////////////////////
// Created by Sam Svensson 2013 // // Created by Sam Svensson 2013 //
////////////////////////////////// //////////////////////////////////
#ifndef NETWORK_DEPENDENCIES_CONNECTION_H
#define NETWORK_DEPENDENCIES_CONNECTION_H
#include "IConnection.h" #include "IConnection.h"
namespace Oyster namespace Oyster
@ -15,22 +15,24 @@ namespace Oyster
{ {
public: public:
Connection() { mySocket = 0; }; Connection() { this->socket = 0; };
Connection(int socket) { mySocket = socket; }; Connection(int socket) { this->socket = socket; };
~Connection(); virtual ~Connection();
virtual bool Connect( unsigned short port , const char serverName[] );
virtual bool InitiateServer( unsigned short port ); virtual bool InitiateServer( unsigned short port );
virtual bool InitiateClient();
virtual void Disconnect();
virtual bool Send(const unsigned char message[]); virtual bool Send(const unsigned char message[]);
virtual int Recieve(unsigned char message[]); virtual int Recieve(unsigned char message[]);
virtual void Disconnect();
virtual bool Connect( unsigned short port , const char serverName[] );
virtual int Listen(); virtual int Listen();
private: private:
int mySocket; bool initiateSocket();
int socket;
}; };
} }

View File

@ -1,10 +1,10 @@
#ifndef NETWORK_DEPENDENCIES_I_CONNECTION_H
#define NETWORK_DEPENDENCIES_I_CONNECTION_H
////////////////////////////////// //////////////////////////////////
// Created by Sam Svensson 2013 // // Created by Sam Svensson 2013 //
////////////////////////////////// //////////////////////////////////
#ifndef NETWORK_DEPENDENCIES_I_CONNECTION_H
#define NETWORK_DEPENDENCIES_I_CONNECTION_H
namespace Oyster namespace Oyster
{ {
namespace Network namespace Network
@ -17,6 +17,7 @@ namespace Oyster
virtual bool Send( const unsigned char message[] ) = 0; virtual bool Send( const unsigned char message[] ) = 0;
virtual int Recieve(unsigned char message[]) = 0; virtual int Recieve(unsigned char message[]) = 0;
virtual bool InitiateServer( unsigned short port ) { return false; }; virtual bool InitiateServer( unsigned short port ) { return false; };
virtual bool 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 bool Connect( unsigned short port, const char serverName[] ) { return false; };
}; };

View File

@ -1,6 +1,10 @@
#ifndef NETWORK_DEPENDENCIES_I_TRANSLATE #ifndef NETWORK_DEPENDENCIES_I_TRANSLATE
#define NETWORK_DEPENDENCIES_I_TRANSLATE #define NETWORK_DEPENDENCIES_I_TRANSLATE
//////////////////////////////////
// Created by Sam Svensson 2013 //
//////////////////////////////////
namespace Oyster namespace Oyster
{ {
namespace Network namespace Network

View File

@ -1,6 +1,10 @@
#ifndef NETWORK_DEPENDENCIES_PROTOCOLS_H #ifndef NETWORK_DEPENDENCIES_PROTOCOLS_H
#define NETWORK_DEPENDENCIES_PROTOCOLS_H #define NETWORK_DEPENDENCIES_PROTOCOLS_H
//////////////////////////////////
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include <string> #include <string>
namespace Oyster namespace Oyster

View File

@ -1,6 +1,10 @@
#ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H #ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H
#define NETWORK_DEPENDENCIES_TRANSLATOR_H #define NETWORK_DEPENDENCIES_TRANSLATOR_H
//////////////////////////////////
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "Messages/MessagesInclude.h" #include "Messages/MessagesInclude.h"
#include "Protocols.h" #include "Protocols.h"
#include "ITranslate.h" #include "ITranslate.h"

View File

@ -15,6 +15,7 @@ Client::~Client()
bool Client::Connect(unsigned int port, char filename[]) bool Client::Connect(unsigned int port, char filename[])
{ {
connection->InitiateClient();
connection->Connect(port, filename); connection->Connect(port, filename);
return true; return true;