#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

//81920 is the default buffer size used by .NET 4.5.1
// with nonblocking sockets autotuning did not work regardless of application buffer size
#define SEND_BUFFER_SIZE 81920
//Output with 80KB buffer:
/*
8192<-current
65536<-ideal
131072<-ideal
262144<-ideal
524288<-ideal
*/
// code taken from https://docs.microsoft.com/en-us/windows/desktop/winsock/complete-client-code
// adapted to showcase automatic buffer tuning
int main(int argc, char* argv[])
{
	static int prev = 0, prevideal=0;
	int curval=0;
	int curlen=sizeof(curval);
	DWORD ideallen;
	ULONG curideal=0;

	char tmp[11];
	WSADATA wsaData;
	SOCKET ConnectSocket = INVALID_SOCKET;
	struct addrinfo *result = NULL,
		*ptr = NULL,
		hints;
	char sendbuf[SEND_BUFFER_SIZE] = "PUT /research_w/en/1 HTTP/1.1\nHost: 10.51.4.147:9200\nAccept: */*\nContent-Length: 185829363\n\n";

	int iResult;    

	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	if (iResult != 0) {
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}

	ZeroMemory( &hints, sizeof(hints) );
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	// Resolve the server address and port
	iResult = getaddrinfo("10.51.4.147", "9200", &hints, &result);
	if ( iResult != 0 ) {
		printf("getaddrinfo failed with error: %d\n", iResult);
		WSACleanup();
		return 1;
	}

	// Attempt to connect to an address until one succeeds
	for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

		// Create a SOCKET for connecting to server
		ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
			ptr->ai_protocol);
		if (ConnectSocket == INVALID_SOCKET) {
			printf("socket failed with error: %ld\n", WSAGetLastError());
			WSACleanup();
			return 1;
		}

		// Connect to server.
		iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
		if (iResult == SOCKET_ERROR) {
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
			continue;
		}
		break;
	}

	freeaddrinfo(result);

	if (ConnectSocket == INVALID_SOCKET) {
		printf("Unable to connect to server!\n");
		WSACleanup();
		return 1;
	}
	unsigned long flags = 1UL;
	iResult = ioctlsocket(ConnectSocket, FIONBIO, &flags);
	if (iResult == SOCKET_ERROR) {
		printf("failed to set nonblocking mode: %ld\n", WSAGetLastError());
		WSACleanup();
		return 1;
	}

	while(true) {
		iResult = send( ConnectSocket, sendbuf, (int)sizeof(sendbuf), 0 );
		//ignore send failures; they don't matter for the experiment
		if(getsockopt(ConnectSocket, SOL_SOCKET, SO_SNDBUF, (char *)&curval, &curlen) == 0) {
			if(curval != prev) {
				OutputDebugStringA(itoa(curval,tmp,10));
				OutputDebugStringA("<-current\n");
				prev = curval;
			}
		} else
			OutputDebugStringA("getsockopt failed \n");
		//with nonblocking socket it doesn't matter if we query - autotuning is disabled even if we don't
		iResult = WSAIoctl(ConnectSocket,SIO_IDEAL_SEND_BACKLOG_QUERY,0,0,&curideal,sizeof(curideal),&ideallen,0,0);
		if(iResult == 0) {
			if(curideal != prevideal) {
				OutputDebugStringA(itoa(curideal,tmp,10));
				OutputDebugStringA("<-ideal\n");
				prevideal = curideal;
			}
		} else {
			OutputDebugStringA(itoa(iResult,tmp,10));
			OutputDebugStringA("WsaIOctl failed \n");
		}
	}
}