2013-11-19 12:38:13 +01:00
|
|
|
#include "Connection.h"
|
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iostream>
|
2013-11-26 13:45:03 +01:00
|
|
|
#include <string>
|
2013-11-19 13:42:50 +01:00
|
|
|
|
2013-11-25 11:39:38 +01:00
|
|
|
using namespace Oyster::Network;
|
2013-11-19 12:38:13 +01:00
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket( this->socket );
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
int Connection::Connect(unsigned short port , const char serverName[])
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-22 09:17:07 +01:00
|
|
|
struct hostent *hostEnt;
|
|
|
|
if((hostEnt = gethostbyname(serverName)) == NULL)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return WSAGetLastError();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
2013-11-25 11:39:38 +01:00
|
|
|
|
2013-11-19 12:38:13 +01:00
|
|
|
struct sockaddr_in server;
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(port);
|
2013-11-22 09:17:07 +01:00
|
|
|
server.sin_addr.s_addr = *(unsigned long*) hostEnt->h_addr;
|
2013-11-19 12:38:13 +01:00
|
|
|
|
2013-11-25 12:14:01 +01:00
|
|
|
if(connect(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return WSAGetLastError();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//connection succesfull!
|
2013-11-26 13:45:03 +01:00
|
|
|
return 0;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
int Connection::InitiateServer(unsigned short port)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
int errorCode = 0;
|
|
|
|
|
2013-11-27 11:28:11 +01:00
|
|
|
if((errorCode = InitiateSocket()) != 0)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return errorCode;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in server;
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(port);
|
|
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2013-11-25 11:39:38 +01:00
|
|
|
if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
errorCode = WSAGetLastError();
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket(this->socket);
|
2013-11-26 13:45:03 +01:00
|
|
|
return errorCode;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 09:17:07 +01:00
|
|
|
//not our Listen function! its trying to keep our socket open for connections
|
2013-11-25 11:39:38 +01:00
|
|
|
if(listen(this->socket, 5) == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
errorCode = WSAGetLastError();
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket(this->socket);
|
2013-11-26 13:45:03 +01:00
|
|
|
return errorCode;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Server started!
|
2013-11-26 13:45:03 +01:00
|
|
|
return 0;
|
2013-11-25 12:14:01 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
int Connection::InitiateClient()
|
2013-11-25 12:14:01 +01:00
|
|
|
{
|
2013-11-27 11:28:11 +01:00
|
|
|
return InitiateSocket();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
int Connection::Disconnect()
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-25 11:39:38 +01:00
|
|
|
closesocket(this->socket);
|
2013-11-26 13:45:03 +01:00
|
|
|
|
|
|
|
return WSAGetLastError();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:28:11 +01:00
|
|
|
int Connection::Send(OysterByte& bytes)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
int nBytes;
|
2013-11-22 09:24:44 +01:00
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
nBytes = send(this->socket, bytes, bytes.GetSize(), 0);
|
2013-11-22 09:17:07 +01:00
|
|
|
if(nBytes == SOCKET_ERROR)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return WSAGetLastError();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
return 0;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
int Connection::Recieve(OysterByte& bytes)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
|
|
|
int nBytes;
|
2013-11-25 19:05:28 +01:00
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
bytes.Clear(1000);
|
|
|
|
nBytes = recv(this->socket, bytes, 500, 0);
|
2013-11-19 12:38:13 +01:00
|
|
|
if(nBytes == SOCKET_ERROR)
|
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return WSAGetLastError();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
2013-11-27 11:01:22 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes.SetSize(nBytes);
|
|
|
|
}
|
2013-11-19 12:38:13 +01:00
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl;
|
|
|
|
|
|
|
|
//bytes.byteArray[nBytes] = '\0';
|
2013-11-19 14:18:34 +01:00
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
return 0;
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int Connection::Listen()
|
|
|
|
{
|
|
|
|
int clientSocket;
|
2013-11-25 11:39:38 +01:00
|
|
|
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
|
2013-11-19 12:38:13 +01:00
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return WSAGetLastError();
|
2013-11-19 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return clientSocket;
|
|
|
|
}
|
2013-11-25 14:03:32 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
//Private functions
|
|
|
|
///////////////////////////////////////
|
2013-11-27 11:28:11 +01:00
|
|
|
int Connection::InitiateSocket()
|
2013-11-25 14:03:32 +01:00
|
|
|
{
|
|
|
|
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(this->socket == SOCKET_ERROR)
|
|
|
|
{
|
2013-11-26 13:45:03 +01:00
|
|
|
return WSAGetLastError();
|
2013-11-25 14:03:32 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:03 +01:00
|
|
|
return 0;
|
2013-11-25 19:05:28 +01:00
|
|
|
}
|