From d1fb6a7718dfac6a6513c598ebbdb33a69d47126 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Thu, 20 Feb 2014 11:01:25 +0100 Subject: [PATCH] Added udp connection if we want to implement broadcast. --- .../NetworkDependencies/ConnectionUDP.cpp | 218 ++++++++++++++++++ .../NetworkDependencies/ConnectionUDP.h | 57 +++++ .../NetworkDependencies.vcxproj | 2 + .../NetworkDependencies.vcxproj.filters | 2 + 4 files changed, 279 insertions(+) create mode 100644 Code/Network/NetworkDependencies/ConnectionUDP.cpp create mode 100644 Code/Network/NetworkDependencies/ConnectionUDP.h diff --git a/Code/Network/NetworkDependencies/ConnectionUDP.cpp b/Code/Network/NetworkDependencies/ConnectionUDP.cpp new file mode 100644 index 00000000..e7d065a6 --- /dev/null +++ b/Code/Network/NetworkDependencies/ConnectionUDP.cpp @@ -0,0 +1,218 @@ +#include "ConnectionUDP.h" + +#include + +using namespace Oyster::Network; + +ConnectionUDP::ConnectionUDP() +{ + this->ipSize = 16; + this->socket = -1; + this->stillSending = false; + this->closed = true; + this->Address = NULL; +} + +ConnectionUDP::ConnectionUDP( int socket ) +{ + this->ipSize = 16; + this->socket = socket; + this->stillSending = false; + this->closed = true; + this->Address = NULL; +} + +ConnectionUDP::~ConnectionUDP() +{ + closesocket( this->socket ); +} + +int ConnectionUDP::Connect(unsigned short port, const char serverName[]) +{ + this->port = port; + + closed = false; + stillSending = true; + + struct hostent *hostEnt; + if((hostEnt = gethostbyname(serverName)) == NULL) + { + return SOCKET_ERROR; + } + + + this->Address = *(unsigned long*)hostEnt->h_addr; + this->port = htons(this->port); + return 0; +} + +int ConnectionUDP::Disconnect() +{ + int result = closesocket(this->socket); + + if(result == SOCKET_ERROR) + { + return result; + } + + return 0; +} + +int ConnectionUDP::InitiateServer(unsigned short port) +{ + int errorCode = 0; + + if((errorCode = InitiateSocket()) != 0) + { + return errorCode; + } + + struct sockaddr_in server; + server.sin_family = AF_INET; + server.sin_addr.s_addr = INADDR_ANY; + server.sin_port = htons(port); + + if(bind(this->socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) + { + errorCode = SOCKET_ERROR; + closesocket(this->socket); + return errorCode; + } + + closed = false; + stillSending = true; + return 0; +} + +int ConnectionUDP::InitiateClient() +{ + return InitiateSocket(); +} + +int ConnectionUDP::InitiateBroadcast(unsigned short port) +{ + int result = InitiateSocket(); + + + int flag = 1; + result = setsockopt(this->socket, /* socket affected */ + SOL_SOCKET, /* set option at TCP level */ + SO_BROADCAST, /* name of option */ + (char *) &flag, /* the cast is historical cruft */ + sizeof(flag)); /* length of option value */ + if (result < 0) + return -1; +} + +int ConnectionUDP::Send(OysterByte &bytes) +{ + int nBytes; + struct sockaddr_in reciever; + reciever.sin_family = AF_INET; + reciever.sin_addr.s_addr = this->Address; + reciever.sin_port = port; + + //printf("Send: %d\n", bytes.GetSize()); + nBytes = sendto(this->socket, + bytes, + bytes.GetSize(), + 0, + (sockaddr*)&reciever, + sizeof(sockaddr_in) + ); + + if ( nBytes != bytes.GetSize() ) + { + return nBytes; + } + + return 0; +} + +int ConnectionUDP::Recieve(OysterByte &bytes) +{ + int nBytes; + sockaddr_in from; + int fromLength = sizeof( from ); + + bytes.Resize(1000); + nBytes = recvfrom(this->socket, + bytes, + 1000, + 0, + (sockaddr*)&from, + &fromLength + ); + + + if(nBytes <= 0) + { + bytes.SetSize(0); + return WSAGetLastError(); + } + else + { + bytes.SetSize(nBytes); + } + + + //address and port of the client who sent the message + unsigned int from_address = ntohl( from.sin_addr.s_addr ); + unsigned int from_port = ntohs( from.sin_port ); + + + return 0; +} + +bool ConnectionUDP::IsSending() +{ + return stillSending; +} + +bool ConnectionUDP::IsConnected() +{ + if(closed) + { + return false; + } + + return true; +} + +int ConnectionUDP::SetBlockingMode( bool blocking ) +{ + DWORD nonBlocking; + + if(blocking) + { + nonBlocking = 0; + } + else + { + nonBlocking = 1; + } + + int result = ioctlsocket(this->socket, FIONBIO, &nonBlocking); + if(result != 0) + { + return result; + } + + //Success + return 0; +} +////////////////////////////////////// +// Private Methods +////////////////////////////////////// + +int ConnectionUDP::InitiateSocket() +{ + this->socket = (int)::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if(this->socket == SOCKET_ERROR) + { + return socket; + } + + return 0; +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ConnectionUDP.h b/Code/Network/NetworkDependencies/ConnectionUDP.h new file mode 100644 index 00000000..163ad741 --- /dev/null +++ b/Code/Network/NetworkDependencies/ConnectionUDP.h @@ -0,0 +1,57 @@ +#ifndef NETWORK_DEPENDENCIES_CONNECTIONUDP_H +#define NETWORK_DEPENDENCIES_CONNECTIONUDP_H + +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "IConnection.h" +#include "../NetworkDependencies/OysterByte.h" + +namespace Oyster +{ + namespace Network + { + class ConnectionUDP : public IConnection + { + public: + ConnectionUDP(); + ConnectionUDP( int socket); + virtual ~ConnectionUDP(); + + virtual int InitiateServer( unsigned short port ); + virtual int InitiateClient(); + + virtual int InitiateBroadcast(unsigned short port); + + virtual int Send ( OysterByte &byte ); + virtual int Recieve( OysterByte &byte ); + + virtual int Connect( unsigned short port, const char serverName[] ); + + virtual int Disconnect(); + + bool IsSending(); + bool IsConnected(); + int GetIpSize(); + + int SetBlockingMode( bool blocking ); + + private: + int InitiateSocket(); + + int socket; + //char ipAddress[16]; + //unsigned short port; + int ipSize; + + unsigned long Address; + unsigned short port; + + bool stillSending; + bool closed; + }; + } +} + +#endif diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 79521d28..9f508f0c 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -147,6 +147,7 @@ + @@ -157,6 +158,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 6a695e88..f24743d1 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -9,6 +9,7 @@ + @@ -25,5 +26,6 @@ + \ No newline at end of file