Created
August 30, 2022 04:24
-
-
Save kklouzal/d77b56ae6fe6b29dc7a181555704c35b to your computer and use it in GitHub Desktop.
Was trying to example an error...but no error...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <iostream> | |
#include <ioapiset.h> | |
#include <string> | |
#include <thread> | |
#include <deque> | |
class SomeData : public OVERLAPPED | |
{ | |
public: | |
std::string _IP_ADDR; | |
int _PORT_RECV; | |
int _PORT_SEND; | |
SomeData() : | |
_IP_ADDR("127.0.0.1"), _PORT_RECV(1234), _PORT_SEND(5678) | |
{ | |
// | |
// OVERLAPPED::Pointer | |
Pointer = this; | |
} | |
}; | |
class Maintainer | |
{ | |
public: | |
HANDLE IOCP; | |
std::atomic<bool> bRunning; | |
std::thread* _Thread; | |
enum class CompletionKey : ULONG_PTR { | |
Key1 | |
}; | |
Maintainer() : | |
bRunning(true) | |
{ | |
IOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 0); | |
if (IOCP == nullptr) { | |
printf("Create IO Completion Port - Send Error: (%lu)\n", GetLastError()); | |
} | |
_Thread = new std::thread(&Maintainer::Thread, this); | |
} | |
void Thread() | |
{ | |
printf("Recv Thread Started\n"); | |
DWORD numberOfBytes = 0; | |
ULONG_PTR completionKey = 0; | |
ULONG numResults = 0; | |
while (bRunning.load()) { | |
OVERLAPPED* pOverlapped = nullptr; | |
// | |
// Wait until we have a receive event | |
GetQueuedCompletionStatus(IOCP, &numberOfBytes, &completionKey, &pOverlapped, INFINITE); | |
// | |
// Received New Packet Operation | |
if (completionKey == static_cast<ULONG_PTR>(CompletionKey::Key1)) { | |
// | |
// Formulate the client ID | |
// | |
// TODO: THIS SHIT RIGHT HERE DONT WORK FOR SOME FRIGGEN REASON. | |
SomeData* _Client1 = static_cast<SomeData*>(pOverlapped->Pointer); | |
const std::string ID1(_Client1->_IP_ADDR + ":" + std::to_string(_Client1->_PORT_RECV)); | |
printf("ID1 %s\n", ID1.c_str()); | |
std::this_thread::sleep_for(std::chrono::seconds(1)); | |
SomeData* _Client3 = static_cast<SomeData*>(pOverlapped->Pointer); | |
const std::string ID2(_Client3->_IP_ADDR + ":" + std::to_string(_Client3->_PORT_RECV)); | |
printf("ID2 %s\n", ID2.c_str()); | |
// | |
const std::string ID3(static_cast<SomeData*>(pOverlapped->Pointer)->_IP_ADDR + ":" + std::to_string(static_cast<SomeData*>(pOverlapped->Pointer)->_PORT_SEND)); | |
printf("ID %s\n", ID3.c_str()); | |
printf("BREAKPOINT LOCATION\n"); | |
} | |
// | |
// Shutdown Thread Operation | |
else { | |
printf("Recv Thread Shutdown Operation\n"); | |
} | |
} | |
printf("Thread Ended\n"); | |
} | |
void PostIOCP(SomeData* _Data) | |
{ | |
PostQueuedCompletionStatus(IOCP, NULL, static_cast<ULONG_PTR>(Maintainer::CompletionKey::Key1), _Data); | |
} | |
}; | |
void main() | |
{ | |
std::deque<SomeData*> _Deque1; | |
std::deque<SomeData*> _Deque2; | |
// | |
// Create the 'maintainer' | |
Maintainer _Main; | |
// | |
// Create some data | |
SomeData* _Data = new SomeData(); | |
_Deque1.push_back(_Data); | |
_Deque2.push_back(_Data); | |
for (auto& _Itm : _Deque2) | |
{ | |
for (std::deque<SomeData*>::iterator it = _Deque1.begin(); it != _Deque1.end();) | |
{ | |
if (*it == _Itm) | |
{ | |
_Deque1.erase(it); | |
// | |
// Pass the data into the maintainer | |
_Main.PostIOCP(_Itm); | |
break; | |
} | |
} | |
} | |
std::system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment