본격적으로 라이브러리를 쌓아올리는 과정이다.
먼저 소켓을 초기화하고 인자를 설정하는 등 소켓에 대한 내용이다.
SocketUtils.h
#pragma once
#include "NetAddress.h"
// 매핑을 해줘서 편하게 사용하기 위함
//----------------
// Socket Utils
//----------------
class SocketUtils
{
public:
static LPFN_CONNECTEX ConnectEx;
static LPFN_DISCONNECTEX DisconnectEx;
static LPFN_ACCEPTEX AcceptEx;
public:
static void Init();
static void Clear();
static bool BindWindowsFunction(SOCKET socket, GUID guid, LPVOID* fn);
static SOCKET CreateSocket();
// 소캣 옵션 설정
static bool SetLinger(SOCKET socket, uint16 onoff, uint16 linger);
static bool SetReuseAddress(SOCKET socket, bool flag);
static bool SetRecvBufferSize(SOCKET socket, int32 size);
static bool SetSendBufferSize(SOCKET socket, int32 size);
static bool SetTcpNoDelay(SOCKET socket, bool flag);
static bool SetUpdateAcceptSocket(SOCKET socket, SOCKET listenSocket);
static bool Bind(SOCKET socket, NetAddress netAddr);
static bool BindAnyAddress(SOCKET socket, uint64 port);
static bool Listen(SOCKET socket, int32 backlog = SOMAXCONN);
static void Close(SOCKET& socket);
};
template<typename T>
static inline bool SetSocketOpt(SOCKET socket, int32 level, int32 optName, T optVal)
{
return SOCKET_ERROR != ::setsockopt(socket, level, optName, reinterpret_cast<char*>(&optVal), sizeof(T));
}
SocketUtils.cpp
#include "pch.h"
#include "SocketUtils.h"
//----------------
// Socket Utils
//----------------
LPFN_CONNECTEX SocketUtils::ConnectEx = nullptr;
LPFN_DISCONNECTEX SocketUtils::DisconnectEx = nullptr;
LPFN_ACCEPTEX SocketUtils::AcceptEx = nullptr;
void SocketUtils::Init()
{
WSADATA wsaData;
ASSERT_CRASH(::WSAStartup(MAKEWORD(2, 2), OUT & wsaData) == 0);
// 런타임에 주소 얻어오는 API
SOCKET dummySocket = CreateSocket();
ASSERT_CRASH(BindWindowsFunction(dummySocket, WSAID_CONNECTEX, reinterpret_cast<LPVOID*>(&ConnectEx)));
ASSERT_CRASH(BindWindowsFunction(dummySocket, WSAID_DISCONNECTEX, reinterpret_cast<LPVOID*>(&DisconnectEx)));
ASSERT_CRASH(BindWindowsFunction(dummySocket, WSAID_ACCEPTEX, reinterpret_cast<LPVOID*>(&AcceptEx)));
Close(dummySocket);
}
void SocketUtils::Clear()
{
::WSACleanup();
}
// 런타임에 각 함수들의 주소를 얻어오는 함수
bool SocketUtils::BindWindowsFunction(SOCKET socket, GUID guid, LPVOID* fn)
{
DWORD bytes = 0;
return SOCKET_ERROR != ::WSAIoctl(socket, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), fn, sizeof(*fn), OUT & bytes, NULL, NULL);
}
SOCKET SocketUtils::CreateSocket()
{
return ::WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED/*비동기 함수 사용 가능*/);
}
bool SocketUtils::SetLinger(SOCKET socket, uint16 onoff, uint16 linger)
{
LINGER option;
option.l_onoff = onoff;
option.l_linger = linger;
return SetSocketOpt(socket, SOL_SOCKET, SO_LINGER, option);
}
bool SocketUtils::SetReuseAddress(SOCKET socket, bool flag)
{
return SetSocketOpt(socket, SOL_SOCKET, SO_REUSEADDR, flag);
}
bool SocketUtils::SetRecvBufferSize(SOCKET socket, int32 size)
{
return SetSocketOpt(socket, SOL_SOCKET, SO_RCVBUF, size);
}
bool SocketUtils::SetSendBufferSize(SOCKET socket, int32 size)
{
return SetSocketOpt(socket, SOL_SOCKET, SO_SNDBUF, size);
}
bool SocketUtils::SetTcpNoDelay(SOCKET socket, bool flag)
{
return SetSocketOpt(socket, SOL_SOCKET, TCP_NODELAY, flag);
}
// ListenSocket의 특성을 ClientSocket에 그대로 적용
bool SocketUtils::SetUpdateAcceptSocket(SOCKET socket, SOCKET listenSocket)
{
return SetSocketOpt(socket, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, listenSocket);
}
bool SocketUtils::Bind(SOCKET socket, NetAddress netAddr)
{
return SOCKET_ERROR != ::bind(socket, reinterpret_cast<const SOCKADDR*>(&netAddr.GetSockAddr()), sizeof(SOCKADDR_IN));
}
bool SocketUtils::BindAnyAddress(SOCKET socket, uint64 port)
{
SOCKADDR_IN myAddress;
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = ::htonl(INADDR_ANY);
myAddress.sin_port = ::htons(port);
return SOCKET_ERROR != ::bind(socket, reinterpret_cast<const SOCKADDR*>(&myAddress), sizeof(myAddress));
}
bool SocketUtils::Listen(SOCKET socket, int32 backlog)
{
return SOCKET_ERROR != ::listen(socket, backlog);
}
void SocketUtils::Close(SOCKET& socket)
{
if(socket != INVALID_SOCKET)
::closesocket(socket);
socket = INVALID_SOCKET;
}
NetAddress.h
#pragma once
// 클라이언트의 주소를 추출할 일이 있음
// 매번 함수를 부르기 보다 이 클래스로 매핑해서
// 편하게 사용하도록 함
//---------------
// NetAddress
//---------------
class NetAddress
{
public:
NetAddress() = default;
NetAddress(SOCKADDR_IN sockAddr);
NetAddress(wstring ip, uint64 port);
SOCKADDR_IN& GetSockAddr() { return _sockAddr; };
wstring GetIpAddr();
uint64 Getport() { return ::ntohs(_sockAddr.sin_port); };
public:
static IN_ADDR Ip2Address(const WCHAR* ip);
// DNS 주소를 ip주소로 바꾸는 등 기능 확장
private:
SOCKADDR_IN _sockAddr = {};
};
NetAddress.cpp
#include "pch.h"
#include "NetAddress.h"
NetAddress::NetAddress(SOCKADDR_IN sockAddr) : _sockAddr(sockAddr)
{
}
NetAddress::NetAddress(wstring ip, uint64 port)
{
::memset(&_sockAddr, 0, sizeof(_sockAddr));
_sockAddr.sin_family = AF_INET;
_sockAddr.sin_addr = Ip2Address(ip.c_str());
_sockAddr.sin_port = ::htons(port);
}
wstring NetAddress::GetIpAddr()
{
WCHAR buffer[100];
::InetNtopW(AF_INET, &_sockAddr.sin_addr, buffer, len32(buffer));
return wstring(buffer);
}
IN_ADDR NetAddress::Ip2Address(const WCHAR* ip)
{
IN_ADDR address;
::InetPtonW(AF_INET, ip, &address);
return address;
}
Main.cpp
#include "pch.h"
#include "CorePch.h"
#include <Windows.h>
#include "ThreadManager.h"
// 소켓 유틸 사용 예시
// 자주 사용하지는 않지만 일일이 인자를 외우는 것보다
// 꼭 설정해야하는 인자만 두고 매핑하면 사용하기 편하다!
#include "SocketUtils.h"
int main()
{
// 소켓 init과 clear는 CoreGlobal에서 수행
SOCKET socket = SocketUtils::CreateSocket();
SocketUtils::BindAnyAddress(socket, 7777);
SocketUtils::Listen(socket);
// 이후 IOCP와 연동해서 사용
SOCKET clientSocket = ::accept(socket, nullptr, nullptr);
cout << "Client Connected!" << endl;
}
'서버 > 네트워크 라이브러리' 카테고리의 다른 글
5. Session#2 (0) | 2025.01.04 |
---|---|
4. Session#1 (0) | 2025.01.01 |
3. Service (0) | 2024.11.29 |
2. IocpCore (0) | 2024.10.05 |