#include "ServerClass.h"
int main()
{
unique_ptr<class FServerClass> ServerClass(new FServerClass());
if (ServerClass->Init())
{
int32 tInput = 0;
cin >> tInput;
}
else
{
cout << "Server Failure Terminate" << endl;
}
ServerClass.reset();
return 0;
}
ServerClass.h
#pragma once
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include <iostream>
#include <cstdint>
#include <thread>
#include <atomic>
#include <chrono>
using namespace std;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
#define _IOOperationBufferSize 1024
class FServerClass
{
public:
bool Init();
FServerClass();
~FServerClass();
private:
atomic<bool> bWantsToStop;
HANDLE IOCPHandle;
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKADDR_IN HostAddress;
thread* ServerThread;
void MemFree();
void ThreadFunc();
char recvBuffer[1000];
};
ServerClass.cpp
#include "stdafx.h"
#include "ServerClass.h"
bool FServerClass::Init()
{
int32 tRecInt = 0;
tRecInt = WSAStartup(MAKEWORD(2, 2), &wsaData);;
if (tRecInt != 0)
{
cout << "WSA Failure" << endl;
return false;
}
ListeningSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (ListeningSocket == INVALID_SOCKET)
{
cout << "Socket Creation Failure" << endl;
return false;
}
HostAddress.sin_family = AF_INET;
HostAddress.sin_port = htons(7777);
HostAddress.sin_addr.s_addr = htonl(INADDR_ANY);
tRecInt = bind(ListeningSocket, (struct sockaddr*)&HostAddress, sizeof(SOCKADDR_IN));
if (tRecInt == 0)
{
cout << "Bind Success" << endl;
ServerThread = new thread(&FServerClass::ThreadFunc, this);
return true;
}
else
{
cout << "Bind Failure" << endl;
return false;
}
}
FServerClass::FServerClass()
{
IOCPHandle = NULL;
bWantsToStop = false;
ServerThread = nullptr;
ListeningSocket = INVALID_SOCKET;
}
FServerClass::~FServerClass()
{
MemFree();
}
void FServerClass::MemFree()
{
bWantsToStop = true;
if (ServerThread != nullptr)
{
ServerThread = nullptr;
}
CloseHandle(IOCPHandle);
closesocket(ListeningSocket);
WSACleanup();
}
void FServerClass::ThreadFunc()
{
struct Session
{
SOCKET socket = INVALID_SOCKET;
char recvBuffer[_IOOperationBufferSize] = {};
int32 recvBytes = 0;
};
struct OverlappedEx
{
WSAOVERLAPPED overlapped = {};
int32 type = 0;
};
HANDLE g_hCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 3);
if (!g_hCompletionPort)
{
cout << "IOCP HANDLE Init FAILURE" << endl;
}
CreateIoCompletionPort((HANDLE)ListeningSocket, g_hCompletionPort, (DWORD)ListeningSocket, 3);
SOCKET client_sock;
DWORD bytesTransferred = 0;
Session* session = nullptr;
OverlappedEx* overlappedEx = nullptr;
while (!bWantsToStop.load())
{
bool tbResult = GetQueuedCompletionStatus(g_hCompletionPort, &bytesTransferred, (ULONG_PTR*)&session, (LPOVERLAPPED*)&overlappedEx, INFINITE);
if (tbResult)
{
cout << "NewConnection: %dBytes" << static_cast<int32>(bytesTransferred) << endl;
}
}
delete this;
}
The GetQueuedCompletionStatus function is not triggering for incoming UDP packets, even though the recvfrom function confirms packet reception and the ThreadFunc is reached. No errors are reported during compilation or execution.
I wanna see GetQueuedCompletionStatus returns true.
New contributor
Handrix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.