Add socket library
This commit is contained in:
@@ -1 +1,2 @@
|
||||
Client.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\Client.exe
|
||||
client.cpp
|
||||
Client.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\Client.exe
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Debug/Client.exe
BIN
Debug/Client.exe
Binary file not shown.
BIN
Debug/Client.ilk
BIN
Debug/Client.ilk
Binary file not shown.
BIN
Debug/Client.pdb
BIN
Debug/Client.pdb
Binary file not shown.
BIN
Debug/Server.exe
BIN
Debug/Server.exe
Binary file not shown.
BIN
Debug/Server.ilk
BIN
Debug/Server.ilk
Binary file not shown.
BIN
Debug/Server.pdb
BIN
Debug/Server.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -8,6 +8,16 @@ int main()
|
||||
if (Network::Initialize() == true)
|
||||
{
|
||||
Log::Print("[SERVER] Winsok zostal zainicjolizowany. Mozemy dzialac.");
|
||||
Socket socket;
|
||||
if (socket.Create() == Result::Success)
|
||||
{
|
||||
Log::Print("[SERVER] Utworzylem gniazdo.");
|
||||
socket.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::PrintError("[SERVER] Nie udalo mi sie utworzyc gniazda.", -1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,2 +1,2 @@
|
||||
Network.cpp
|
||||
Socket.cpp
|
||||
SocketLibrary.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\SocketLibrary.lib
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,12 +1,14 @@
|
||||
#include <iostream>
|
||||
#include "Log.h"
|
||||
#include "Result.h"
|
||||
#include "ipVersion.h"
|
||||
|
||||
void SocketLibrary::Log::Print(std::string message)
|
||||
{
|
||||
std::cout << message << std::endl;
|
||||
std::cout << "[LOG] " << message << std::endl;
|
||||
}
|
||||
|
||||
void SocketLibrary::Log::PrintError(std::string message, int error)
|
||||
{
|
||||
std::cerr << "[Error Code: " << error << "] "<< message << std::endl;
|
||||
std::cerr << "[ERROR] " << "[Error Code: " << error << "] "<< message << std::endl;
|
||||
}
|
||||
|
||||
10
SocketLibrary/Result.h
Normal file
10
SocketLibrary/Result.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace SocketLibrary
|
||||
{
|
||||
enum class Result
|
||||
{
|
||||
Success,
|
||||
Fail
|
||||
};
|
||||
}
|
||||
92
SocketLibrary/Socket.cpp
Normal file
92
SocketLibrary/Socket.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "Socket.h"
|
||||
#include "Log.h"
|
||||
#include <assert.h>
|
||||
|
||||
namespace SocketLibrary
|
||||
{
|
||||
Socket::Socket(IPVersion ipversion, SOCKET socket)
|
||||
:_ipversion(ipversion),_socket(socket)
|
||||
{
|
||||
assert(_ipversion == IPVersion::IPv4);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Result Socket::Create()
|
||||
{
|
||||
assert(_ipversion == IPVersion::IPv4);
|
||||
|
||||
if (_socket != INVALID_SOCKET)
|
||||
{
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (_socket == INVALID_SOCKET)
|
||||
{
|
||||
int error = WSAGetLastError();
|
||||
Log::PrintError("Nie udalo sie utworzyc gniazda", error);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
Log::Print("Gniazdo zostalo utworzone.");
|
||||
|
||||
if (SetSocketOption(SocketOption::TCP_NoDelay, TRUE) == Result::Success)
|
||||
{
|
||||
Log::Print("NoDelay zostalo wlaczone na gniezdzie.");
|
||||
}
|
||||
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Result Socket::Close()
|
||||
{
|
||||
if (_socket == INVALID_SOCKET)
|
||||
{
|
||||
Log::PrintError("Niemozna zamknac nieistniejacego gniazda.", -1);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
int result = closesocket(_socket);
|
||||
if (result != 0)
|
||||
{
|
||||
int error = WSAGetLastError();
|
||||
Log::PrintError("Nie udalo sie zamknac gniazda.", error);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
_socket = INVALID_SOCKET;
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
SOCKET Socket::GetSocket()
|
||||
{
|
||||
return _socket;
|
||||
}
|
||||
|
||||
IPVersion Socket::GetIpVersion()
|
||||
{
|
||||
return _ipversion;
|
||||
}
|
||||
Result Socket::SetSocketOption(SocketOption option, BOOL value)
|
||||
{
|
||||
int result = 0;
|
||||
switch (option)
|
||||
{
|
||||
case SocketOption::TCP_NoDelay:
|
||||
result = setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&value, sizeof(value));
|
||||
break;
|
||||
default:
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
if (result != 0)
|
||||
{
|
||||
int error = WSAGetLastError();
|
||||
Log::PrintError("Nie mozna ustawic opcji dla gniazda.", error);
|
||||
return Result::Fail;
|
||||
}
|
||||
|
||||
return Result::Success;
|
||||
}
|
||||
}
|
||||
24
SocketLibrary/Socket.h
Normal file
24
SocketLibrary/Socket.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <WinSock2.h>
|
||||
|
||||
#include "ipVersion.h"
|
||||
#include "Result.h"
|
||||
#include "SocketOption.h"
|
||||
|
||||
namespace SocketLibrary
|
||||
{
|
||||
class Socket
|
||||
{
|
||||
public:
|
||||
Socket(IPVersion ipversion = IPVersion::IPv4, SOCKET socket = INVALID_SOCKET); //(..., SocketHandle handle)
|
||||
Result Create();
|
||||
Result Close();
|
||||
SOCKET GetSocket(); //SocketHandle GetHandle()
|
||||
IPVersion GetIpVersion();
|
||||
private:
|
||||
Result SetSocketOption(SocketOption option, BOOL value);
|
||||
IPVersion _ipversion = IPVersion::IPv4;
|
||||
SOCKET _socket = INVALID_SOCKET;
|
||||
|
||||
};
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
#include "Network.h"
|
||||
#include "Socket.h"
|
||||
|
||||
#include "Log.h"
|
||||
@@ -142,13 +142,18 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ipVersion.h" />
|
||||
<ClInclude Include="Log.h" />
|
||||
<ClInclude Include="Network.h" />
|
||||
<ClInclude Include="Result.h" />
|
||||
<ClInclude Include="Socket.h" />
|
||||
<ClInclude Include="SocketLibrary.h" />
|
||||
<ClInclude Include="SocketOption.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Log.cpp" />
|
||||
<ClCompile Include="Network.cpp" />
|
||||
<ClCompile Include="Socket.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -24,6 +24,18 @@
|
||||
<ClInclude Include="Log.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Result.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ipVersion.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Socket.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SocketOption.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Network.cpp">
|
||||
@@ -32,5 +44,8 @@
|
||||
<ClCompile Include="Log.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Socket.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
9
SocketLibrary/SocketOption.h
Normal file
9
SocketLibrary/SocketOption.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace SocketLibrary
|
||||
{
|
||||
enum class SocketOption
|
||||
{
|
||||
TCP_NoDelay,
|
||||
};
|
||||
}
|
||||
12
SocketLibrary/ipVersion.h
Normal file
12
SocketLibrary/ipVersion.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
namespace SocketLibrary
|
||||
{
|
||||
|
||||
enum class IPVersion
|
||||
{
|
||||
Unknown,
|
||||
IPv4,
|
||||
IPv6
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user