Commit 30fc7898 authored by Hobnob's avatar Hobnob

still figuring out how to get.. the async sauce

parent d0a3e634
#define _WINSOCKAPI_
#include <windows.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include "SocketServer.h"
#pragma comment(lib, "Ws2_32.lib")
SocketServer::SocketServer()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
this->ListenSocket = INVALID_SOCKET;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup Failed!");
return;
}
struct addrinfo* result = NULL, * ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(NULL, "27020", &hints, &result) != 0)
{
printf("getaddrinfo Failed!");
WSACleanup();
return;
}
this->ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (this->ListenSocket == INVALID_SOCKET)
{
printf("socket() Error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return;
}
if (bind(this->ListenSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR)
{
printf("Bind Failed, Error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(this->ListenSocket);
WSACleanup();
return;
}
freeaddrinfo(result);
if (listen(this->ListenSocket, SOMAXCONN) == SOCKET_ERROR)
{
printf("listen error: %ld\n", WSAGetLastError());
closesocket(this->ListenSocket);
WSACleanup();
return;
}
}
void SocketServer::SetOnConnectFunc(std::function<void(std::string)>)
{
}
void SocketServer::SetOnTrafficFunc(std::function<void(std::vector<char>, int)> f)
{
}
void SocketServer::Receive()
{
SOCKET ClientSocket;
while (true)
{
ClientSocket = INVALID_SOCKET;
ClientSocket = accept(this->ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
printf("accept Failed, Error: %d\n", WSAGetLastError());
closesocket(this->ListenSocket);
WSACleanup();
return;
}
char recvbuf[512];
int iResult, iSendResult;
int recvbuflen = 512;
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
//std::cout << recvbuf << std::endl;
//Messages.push_back(recvbuf);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return;
}
} while (iResult > 0);
}
}
void SocketServer::Shutdown(SOCKET ClientSocket)
{
if (shutdown(ClientSocket, SD_SEND) == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", WSAGetLastError());
}
closesocket(ClientSocket);
WSACleanup();
}
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <winsock2.h>
class SocketServer
{
public:
SocketServer();
/// <summary>
/// Define A Function to call on client connect
/// </summary>
/// <param name="f">func to call, f(string ClientIP)</param>
void SetOnConnectFunc(std::function<void(std::string)> f);
/// <summary>
/// Define A Function to call when traffic is received
/// </summary>
/// <param name="f">func to call, f(vector<char> Data, int DataLen)</param>
void SetOnTrafficFunc(std::function<void(std::vector<char>, int)> f);
private:
SOCKET ListenSocket;
void Receive();
void Shutdown(SOCKET ClientSocket);
};
#define _WINSOCKAPI_
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include "SocketServer.h"
#include <stdio.h>
#include <iostream>
#include <thread>
#include <vector>
#pragma comment(lib, "Ws2_32.lib")
std::vector<std::string> Messages;
......@@ -134,9 +132,14 @@ void DoSocket()
WSACleanup();
return;
}
void OnConnect(std::string IP)
{
printf("hi");
}
int main()
{
SocketServer s;
s.SetOnConnectFunc(OnConnect);
std::thread sockServer(DoSocket);
Example demo;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment