final app
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#include <iomanip>
|
||||
#include "Socket.h"
|
||||
#include "Log.h"
|
||||
#include <assert.h>
|
||||
@@ -14,7 +15,11 @@ namespace SocketLibrary
|
||||
Socket::Socket(Protocol protocol)
|
||||
:_ipversion(IPVersion::IPv4), _protocol(protocol), _socket(INVALID_SOCKET)
|
||||
{
|
||||
assert(_ipversion == IPVersion::IPv4);
|
||||
}
|
||||
|
||||
Socket::Socket(SOCKET socket)
|
||||
:_ipversion(IPVersion::IPv4), _protocol(Protocol::TCP), _socket(socket)
|
||||
{
|
||||
}
|
||||
|
||||
Result Socket::Create()
|
||||
@@ -40,7 +45,8 @@ namespace SocketLibrary
|
||||
case SocketLibrary::Protocol::TCP:
|
||||
_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
break;
|
||||
case SocketLibrary::Protocol::UDP:
|
||||
case SocketLibrary::Protocol::UDP_Sender:
|
||||
case SocketLibrary::Protocol::UDP_Reciver:
|
||||
_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
break;
|
||||
default:
|
||||
@@ -55,7 +61,19 @@ namespace SocketLibrary
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::Print("Gniazdo zostalo utworzone. [SOCKET:" + std::to_string(_socket) + "]");
|
||||
std::string msg = "Gniazdo zostalo utworzone ";
|
||||
switch (_protocol)
|
||||
{
|
||||
case SocketLibrary::Protocol::TCP:
|
||||
msg += "[TCP] [SOCKET:";
|
||||
break;
|
||||
case SocketLibrary::Protocol::UDP_Sender:
|
||||
msg += "[UDP] [SOCKET:";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Log::Print(msg + std::to_string(_socket) + "]");
|
||||
}
|
||||
|
||||
switch (_protocol)
|
||||
@@ -66,12 +84,18 @@ namespace SocketLibrary
|
||||
Log::Print("NoDelay zostalo wlaczone na gniezdzie TCP.");
|
||||
}
|
||||
break;
|
||||
case SocketLibrary::Protocol::UDP:
|
||||
case SocketLibrary::Protocol::UDP_Sender:
|
||||
if (SetSocketOption(SocketOption::UDP_Broadcast, TRUE) == Result::Success)
|
||||
{
|
||||
Log::Print("UDP_Broadcast zostalo wlaczone na gniezdzie UDP.");
|
||||
}
|
||||
break;
|
||||
case SocketLibrary::Protocol::UDP_Reciver:
|
||||
if (SetSocketOption(SocketOption::UDP_Reuseaddr, TRUE) == Result::Success)
|
||||
{
|
||||
Log::Print("UDP_Reuseaddr zostalo wlaczone na gniezdzie UDP.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -108,6 +132,12 @@ namespace SocketLibrary
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::AssignEndpoint(Endpoint endpoint)
|
||||
{
|
||||
_endpoint = endpoint;
|
||||
return Result();
|
||||
}
|
||||
|
||||
Result Socket::Bind(Endpoint endpoint)
|
||||
{
|
||||
sockaddr_in addr = endpoint.GetSocketaddrIP();
|
||||
@@ -123,7 +153,7 @@ namespace SocketLibrary
|
||||
Log::Print("Udalo sie zwiazac gniazdo z wybranym portem. [PORT]:" + std::to_string(endpoint.GetPort())
|
||||
+ " [SOCKET]:" + std::to_string(_socket));
|
||||
}
|
||||
|
||||
_endpoint = endpoint;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
@@ -145,11 +175,11 @@ namespace SocketLibrary
|
||||
{
|
||||
Log::Print("Gniazdo jest od teraz w trybie nasluchu.");
|
||||
}
|
||||
|
||||
// _endpoint = endpoint;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::Accpet(Socket& outSocket)
|
||||
Result Socket::Accept(Socket& outSocket)
|
||||
{
|
||||
sockaddr_in addr = {};
|
||||
int len = sizeof(sockaddr_in);
|
||||
@@ -169,6 +199,31 @@ namespace SocketLibrary
|
||||
newConnectionEndpoint.Print();
|
||||
|
||||
outSocket = Socket(IPVersion::IPv4, Protocol::TCP, acceptedConnection);
|
||||
outSocket._endpoint = newConnectionEndpoint;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::Connect()
|
||||
{
|
||||
//if (_endpoint.GetSocketaddrIP() == ?)
|
||||
//{
|
||||
// Log::PrintError("Blad wewnetrny. Endpoint nie instnieje.",_socket);
|
||||
// return Result::Fail;
|
||||
//}
|
||||
|
||||
sockaddr_in addr = _endpoint.GetSocketaddrIP();
|
||||
int result = connect(_socket, (sockaddr*)(&addr), sizeof(sockaddr_in));
|
||||
if (result != 0)
|
||||
{
|
||||
int error = WSAGetLastError();
|
||||
Log::PrintError("Nie mozna nawiazac polaczenia z " + _endpoint.GetIpString(), error);
|
||||
return Result::Fail;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::Print("Nawiazano nowe polaczenie z " + _endpoint.GetIpString());
|
||||
}
|
||||
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
@@ -186,19 +241,68 @@ namespace SocketLibrary
|
||||
{
|
||||
Log::Print("Nawiazano nowe polaczenie z " + endpoint.GetIpString());
|
||||
}
|
||||
_endpoint = endpoint;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::SendUDP(std::string& listOfUsers, Endpoint server)
|
||||
{
|
||||
sockaddr_in reciverAddr = _endpoint.GetSocketaddrIP();
|
||||
int result = sendto(_socket, listOfUsers.c_str(), listOfUsers.size(), 0, (sockaddr*)&reciverAddr, sizeof(reciverAddr));
|
||||
if (result == SOCKET_ERROR)
|
||||
{
|
||||
int error = WSAGetLastError();
|
||||
Log::PrintError("Problem z wyslaniem danych przez UDP.", error);
|
||||
return Result::Fail;
|
||||
}
|
||||
if (result != listOfUsers.size())
|
||||
{
|
||||
Log::PrintError("Nie udalo sie wyslac wszystkich danych przez UDP.", -1);
|
||||
return Result::Fail;
|
||||
}
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::ReciveUDP(std::string& output, int& numberOfBytes)
|
||||
{
|
||||
char reciveData[256] = {};
|
||||
sockaddr_in addr = {};
|
||||
int len = sizeof(addr);
|
||||
int bytesRecived = recvfrom(_socket, reciveData, 256, 0, (sockaddr*)&addr, &len);
|
||||
if (bytesRecived == SOCKET_ERROR)
|
||||
{
|
||||
int error = WSAGetLastError();
|
||||
Log::PrintError("Problem z odebraniem danych przez UDP.", error);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
std::stringstream msg (reciveData);
|
||||
msg >> len;
|
||||
msg.seekg(1, std::ios_base::cur);
|
||||
std::getline(msg, output);
|
||||
if (len != output.size())
|
||||
{
|
||||
Log::PrintError("Dlugosc odebranych danych sie nie zgadza [UDP]", -1);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
numberOfBytes = len;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::SendAll(const void* data, int numberOfBytes)
|
||||
{
|
||||
int totalBytesSent = 0;
|
||||
uint32_t numberBytesToSend = numberOfBytes;
|
||||
int numberBytesToSend = numberOfBytes;
|
||||
numberBytesToSend = htonl(numberBytesToSend);
|
||||
Result result = Send(&numberBytesToSend, sizeof(uint32_t), totalBytesSent);
|
||||
if (totalBytesSent != sizeof(uint32_t))
|
||||
Result result = Send(&numberBytesToSend, sizeof(int), totalBytesSent);
|
||||
if (result != Result::Success)
|
||||
{
|
||||
Log::PrintError("Nie mozna wyslac dlugosci strumienia danych.", -1);
|
||||
return result;
|
||||
}
|
||||
if (totalBytesSent != sizeof(int))
|
||||
{
|
||||
Log::PrintError("Nie mozna wyslac dlugosci pakietu.", -1);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
@@ -224,17 +328,22 @@ namespace SocketLibrary
|
||||
Result Socket::ReciveAll(std::string& output, int& numberOfBytes)
|
||||
{
|
||||
int totalBytesRecive = 0;
|
||||
uint32_t numberBytesToRecive = 0;
|
||||
Result result = Recive(&numberBytesToRecive, sizeof(uint32_t), totalBytesRecive);
|
||||
if (totalBytesRecive != sizeof(uint32_t))
|
||||
int numberBytesToRecive = 0;
|
||||
Result result = Recive(&numberBytesToRecive, sizeof(int), totalBytesRecive);
|
||||
if (result != Result::Success)
|
||||
{
|
||||
Log::PrintError("Nie mozna odebrac dlugosci strumienia dancyh.", -1);
|
||||
Result::Fail;
|
||||
return result;
|
||||
}
|
||||
if (totalBytesRecive != sizeof(int))
|
||||
{
|
||||
Log::PrintError("Nie mozna odebrac dlugosci pakietu", -1);
|
||||
return Result::Fail;
|
||||
|
||||
}
|
||||
|
||||
totalBytesRecive = 0;
|
||||
numberBytesToRecive = ntohl(numberBytesToRecive);
|
||||
output.resize(numberBytesToRecive + 1);
|
||||
output.resize(numberBytesToRecive );//+1
|
||||
|
||||
while (totalBytesRecive < numberBytesToRecive)
|
||||
{
|
||||
@@ -244,7 +353,7 @@ namespace SocketLibrary
|
||||
Result result = Recive(bufferOffset, bytesLeft, bytesRecive);
|
||||
if (result == Result::Fail)
|
||||
{
|
||||
Log::PrintError("Odebralem " + std::to_string(totalBytesRecive) + " z " + std::to_string(numberOfBytes) + " bajtow.", -1);
|
||||
Log::PrintError("Odebralem " + std::to_string(totalBytesRecive) + " z " + std::to_string(numberBytesToRecive) + " bajtow.", -1);
|
||||
return Result::Fail;
|
||||
}
|
||||
totalBytesRecive += bytesRecive;
|
||||
@@ -264,6 +373,11 @@ namespace SocketLibrary
|
||||
return _ipversion;
|
||||
}
|
||||
|
||||
Endpoint Socket::GetEndpoint()
|
||||
{
|
||||
return _endpoint;
|
||||
}
|
||||
|
||||
Result Socket::Send(const void* data, int size, int& bytesSent)
|
||||
{
|
||||
bytesSent = send(_socket, (const char*)data, size, NULL);
|
||||
@@ -285,7 +399,7 @@ namespace SocketLibrary
|
||||
{
|
||||
Log::PrintError("Polaczenie zostalo zamkniete.", -1);
|
||||
// TODO - zamknac gniazdo
|
||||
return Result::Fail;
|
||||
return Result::ConnectionLost;
|
||||
}
|
||||
if (bytesRecived == SOCKET_ERROR)
|
||||
{
|
||||
@@ -308,6 +422,9 @@ namespace SocketLibrary
|
||||
case SocketOption::UDP_Broadcast:
|
||||
result = setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (const char*)&value, sizeof(value));
|
||||
break;
|
||||
case SocketOption::UDP_Reuseaddr:
|
||||
result = setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&value, sizeof(value));
|
||||
break;
|
||||
default:
|
||||
return Result::Fail;
|
||||
}
|
||||
@@ -321,4 +438,4 @@ namespace SocketLibrary
|
||||
|
||||
return Result::Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user