My goal is to have this be async, so it can be added to an existing pulse code base.
I'm writing this to get a working example that I can then merge into it. Using a windows console app for now.
I have it connecting using WSAConnect, but I can't figure out how to add events. My googling has left me with WSACreateEvent, and likely something with Overlapping I/O, but I either end up on Microsoft's website, where it explains them, but doesn't provide examples, or I find examples that appear to be above my understanding.
Worth mentioning, I also believe I need to be using WSAConnectEx to make that async, however until I can figure out how to add events, that doesn't help me just yet.
Note 2: At the moment, it requires the IP address, not a hostname. Something that needs changed, however, without events the little details are less important.
This has been hacked together by googling...
#pragma comment(lib,"Ws2_32.lib")
#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h> // Needed for _wtoi
int main()
{
// Declare and initialize variables
WSADATA wsaData = { 0 };
int iResult = 0;
SOCKET sock = INVALID_SOCKET;
int iFamily = AF_INET;
int iType = SOCK_STREAM;
int iProtocol = IPPROTO_TCP;
DWORD dwFlags = WSA_FLAG_OVERLAPPED;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
wprintf(L"WSAStartup failed: %d\n", iResult);
return 1;
}
sock = WSASocket(iFamily, iType, iProtocol, NULL, 0, dwFlags);
if (sock == INVALID_SOCKET)
wprintf(L"WSASocket function failed with error = %d\n", WSAGetLastError());
else
{
wprintf(L"WSASocket function succeeded\n");
// Find a way to make it non-blocking here.. I believe
// set up sockaddr
struct sockaddr_in saServer;
saServer.sin_family = AF_INET;
inet_pton(PF_INET, "###.###.###.###", &(saServer.sin_addr));
int portno = 6667;
saServer.sin_port = htons(portno);
iResult = WSAConnect(sock, (SOCKADDR*)&saServer, sizeof(saServer),NULL,NULL,NULL,NULL);
wprintf(L"WSAConnect return = %d\n", iResult);
if (iResult != 0)
{
wprintf(L"WSAConnect function failed with error = %d\n", WSAGetLastError());
return 1;
}
// EDIT - Below while statement has to be non-blocking. It will be executed every frame.
while (TRUE)
{
// my unrelated code here
}
iResult = closesocket(sock);
if (iResult == SOCKET_ERROR)
{
wprintf(L"closesocket function zfailed with error = %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
}
WSACleanup();
return 0;
}
// When data is received in full, this is triggered
void SomeEventFunction(args)
{
// parse the received data here
}
Output:
WSASocket function succeeded
WSAConnect return = 0