<commit_before>/*
Bind socket to port 8888 on localhost
	*/
#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#include "main.h"
#include <utility>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

struct handle_traits
{
	static HANDLE invalid() throw()
	{
		return nullptr;
	}
	static void close(HANDLE value) throw()
	{
		CloseHandle(value);
	}
};

template <typename Type, typename Traits>
class unique_handle
{
	unique_handle(unique_handle const &) = delete;
	unique_handle & operator=(unique_handle const &) = delete;
	void close() throw()
	{
		if (*this)
		{
			Traits::close(m_value);
		}
	}
	Type m_value;
public:
	explicit unique_handle(Type value = Traits::invalid()) throw() :
		m_value(value)
	{
	}
	~unique_handle() throw()
	{
		close();
	}

private:
	struct boolean_struct { int member; };
	typedef int boolean_struct::* boolean_type;
	bool operator==(unique_handle const &) = delete;
	bool operator!=(unique_handle const &) = delete;
public:
	operator boolean_type() const throw()
	{
		return Traits::invalid() != m_value ? &boolean_struct::member : nullptr;
	}
	Type get() const throw()
	{
		return m_value;
	}
	bool reset(Type value = Traits::invalid()) throw()
	{
		if (m_value != value)
		{
			close();
			m_value = value;
		}
		return *this;
	}
	Type release() throw()
	{
		auto value = m_value;
		m_value = Traits::invalid();
		return value;
	}
	unique_handle(unique_handle && other) throw() :
		m_value(other.release())
	{
	}
	unique_handle & operator=(unique_handle && other) throw()
	{
		reset(other.release());
		return *this;
	}
};

struct socket_trait
{
	static auto invalid() noexcept
	{
		return 0;
	}

	static auto close(SOCKET value) noexcept
	{
		closesocket(value);
	}
};

using socket_handle = unique_handle<SOCKET, socket_trait>;

struct socket_client
{
	socket_handle handle;

	//TODO is this right?
	socket_client(socket_handle &handle) : handle(std::move(handle))
	{			
	}

	void send(const char * buffer, int len)
	{
		::send(handle.get(), buffer, len, 0);
	}
};

struct socket_server
{
	socket_handle handle;

	auto open(unsigned int port, unsigned int backlog)
	{
		auto const result = socket(AF_INET, SOCK_STREAM, 0);

		if (result == INVALID_SOCKET) {
			throw 1;
		}

		struct sockaddr_in server;
		server.sin_family = AF_INET;
		server.sin_addr.s_addr = INADDR_ANY;
		server.sin_port = htons(port);

		bind(result, (struct sockaddr *)&server, sizeof(server));

		listen(result, backlog);

		auto localHandle = socket_handle(result);
		handle = std::move(localHandle);
	}

	auto accept()
	{
		struct sockaddr_in client;
		int c = sizeof(struct sockaddr_in);
		auto const result = ::accept(handle.get(), (struct sockaddr *)&client, &c);

		auto localHandle = socket_handle(result);
		return socket_client(localHandle);
	}
};



int main(int argc, char *argv[])
{
	WSADATA wsa;
	printf("\nInitialising Winsock...");
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
		printf("Failed. Error Code : %d", WSAGetLastError());
		return 1;
	}
	printf("Initialised.\n");

	socket_server ss;
	ss.open(8889, 1);
	auto sc  = ss.accept();
	auto response = "HTTP/1.1 200 OK\nContent-Length: 2\n\nOK";
	sc.send(response, strlen(response));
			
	getchar();
	WSACleanup();
<commit_msg>very simple cpp http server using handle pattern<commit_after>/*
Bind socket to port 8888 on localhost
	*/
#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#include "main.h"
#include <utility>

#pragma comment(lib,"ws2_32.lib") //Winsock Library


struct handle_traits
{
	static HANDLE invalid() throw()
	{
		return nullptr;
	}
	static void close(HANDLE value) throw()
	{
		CloseHandle(value);
	}
};

template <typename Type, typename Traits>
class unique_handle
{
	unique_handle(unique_handle const &) = delete;
	unique_handle & operator=(unique_handle const &) = delete;
	void close() throw()
	{
		if (*this)
		{
			Traits::close(m_value);
		}
	}
	Type m_value;
public:
	explicit unique_handle(Type value = Traits::invalid()) throw() :
		m_value(value)
	{
	}
	~unique_handle() throw()
	{
		close();
	}

private:
	struct boolean_struct { int member; };
	typedef int boolean_struct::* boolean_type;
	bool operator==(unique_handle const &) = delete;
	bool operator!=(unique_handle const &) = delete;
public:
	operator boolean_type() const throw()
	{
		return Traits::invalid() != m_value ? &boolean_struct::member : nullptr;
	}
	Type get() const throw()
	{
		return m_value;
	}
	bool reset(Type value = Traits::invalid()) throw()
	{
		if (m_value != value)
		{
			close();
			m_value = value;
		}
		return *this;
	}
	Type release() throw()
	{
		auto value = m_value;
		m_value = Traits::invalid();
		return value;
	}
	unique_handle(unique_handle && other) throw() :
		m_value(other.release())
	{
	}
	unique_handle & operator=(unique_handle && other) throw()
	{
		reset(other.release());
		return *this;
	}
};

struct wsa_trait
{
	static auto invalid() noexcept
	{
		return 0;
	}

	static auto close(int value) noexcept
	{
		WSACleanup();
	}
};

struct wsa
{
	unique_handle<int, wsa_trait> handle;

	wsa()
	{
		WSADATA wsa;
		::WSAStartup(MAKEWORD(2, 2), &wsa);
	}
};

struct socket_trait
{
	static auto invalid() noexcept
	{
		return INVALID_SOCKET;
	}

	static auto close(SOCKET value) noexcept
	{
		//WSAEVENT e = WSACreateEvent();
		//WSAEventSelect(value, e, FD_CLOSE);
		::shutdown(value, SD_BOTH);
		//WSAWaitForMultipleEvents(1, &e, FALSE, WSA_INFINITE, FALSE);

		::closesocket(value);
	}
};

using socket_handle = unique_handle<SOCKET, socket_trait>;

struct socket_client
{
	socket_handle handle;

	//TODO is this right?
	socket_client(socket_handle &handle) : handle(std::move(handle))
	{
	}

	void receive()
	{
		char recvbuf[1024];
		int recvbuflen = 1024;
		auto result = recv(handle.get(), recvbuf, recvbuflen, 0);

		recvbuf[result] = 0;
		printf(recvbuf);
	}

	void send(const char * buffer, int len)
	{
		auto result = ::send(handle.get(), buffer, len, 0);
		if (result == SOCKET_ERROR) {
			printf("send failed with error: %d\n", WSAGetLastError());
		}
	}
};

struct socket_server
{
	socket_handle handle;

	auto open(unsigned int port)
	{
		auto const result = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

		if (result == INVALID_SOCKET) {
			throw 1;
		}

		struct sockaddr_in server;
		server.sin_family = AF_INET;
		server.sin_addr.s_addr = INADDR_ANY;
		server.sin_port = htons(port);

		::bind(result, (struct sockaddr *)&server, sizeof(server));
		::listen(result, SOMAXCONN);

		auto localHandle = socket_handle(result);
		handle = std::move(localHandle);
	}

	auto accept()
	{
		auto const result = ::accept(handle.get(), NULL, NULL);

		if (result == INVALID_SOCKET) {
			printf("accept failed with error: %d\n", WSAGetLastError());
		}

		auto localHandle = socket_handle(result);
		return socket_client(localHandle);
	}
};



int main(int argc, char *argv[])
{
	wsa wsa;

	socket_server ss;
	ss.open(8889);

	while (true) {
		auto sc = ss.accept();
		auto response = "HTTP/1.1 200 OK\nContent-Length: 2\n\nOK";

		sc.receive();
		sc.send(response, strlen(response));
	}

	WSACleanup();

{'fraction_non_alphanumeric': 0.14452181327270144, 'fraction_numerical': 0.0086684666761404, 'mean_word_length': 4.267964071856287, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3135109', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2587, 'n_words': 605}