#pragma warning (disable : 4786)
#include "stdafx.h"
#include "data server.hpp"
#include "data server.h"
#include <vector>

using namespace std;
CRITICAL_SECTION g_CritSection;

void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
    return(malloc(len));
}

void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
    free(ptr);
}

int StartRunID (void)
{
	EnterCriticalSection (&g_CritSection);
	int cur;
	char string[255];

	cur = -1;
	do
	{
		cur++;
		sprintf (string,"mw4run%d.data",cur);
	} while (FileExists (string)  );

	HANDLE file;
	file = NewFile (string);
	CloseFile (file);

	LeaveCriticalSection (&g_CritSection);
	return cur;
}

void RecordData (int id,int version,double time,int type,int objectid,
					 float posx,float posy,float posz,
					 int int1,int int2,int int3,int int4,
					 float float1,float float2,float float3,float float4,
					 const unsigned char __RPC_FAR *str)
{
	g_DataList[id].push_back (CDataEntry (id,version,type,time,objectid,posx,posy,posz,int1,int2,int3,int4,float1,float2,float3,float4,(const char *) str));
}

void FinishRun (int id)
{
	HANDLE file;
	char string[255];
	std::map<int, std::vector<CDataEntry> >::iterator iter;
	
	sprintf (string,"mw4run%d.data",id);
	iter = g_DataList.find (id);
	if (iter == g_DataList.end ())
	{
		DeleteFile (string);
		return;
	}


	file = NewFile (string);
	if (file == INVALID_HANDLE_VALUE)
		return;
	DumpData (file,g_DataList[id]);
	CloseFile (file);
}

bool InitRPC (void)
{
	InitializeCriticalSection (&g_CritSection);

  RPC_STATUS status;
  unsigned char * pszProtocolSequence1 = (unsigned char *) "ncacn_ip_tcp";	
  unsigned char * pszEndpoint1         = (unsigned char *) "1405";

  unsigned char * pszProtocolSequence2 = (unsigned char *) "ncalrpc";
  unsigned char * pszEndpoint2         = (unsigned char *) "MW4DataServer";
  unsigned char * pszSecurity         = (unsigned char *) NULL;
  unsigned int    cMinCalls           = 1;
  unsigned int    cMaxCalls           = 50;
  unsigned int    fDontWait           = 1;


//	status = RpcServerUseAllProtseqs (cMaxCalls,pszSecurity);
  status = RpcServerUseProtseqEp(pszProtocolSequence1,cMaxCalls,pszEndpoint1,pszSecurity);  // Security descriptor
  if (status)
		return false;

  status = RpcServerUseProtseqEp(pszProtocolSequence2,cMaxCalls,pszEndpoint2,pszSecurity);  // Security descriptor
  if (status)
		return false;

	RPC_BINDING_VECTOR *inq;
	status = RpcServerInqBindings (&inq);

  status = RpcServerRegisterIf(MW4DataServer_ServerIfHandle,  // interface to register
                               NULL,   // MgrTypeUuid
                               NULL);  // MgrEpv; null means use default
  if (status)
		return false;

  status = RpcServerListen(cMinCalls,cMaxCalls,fDontWait);
  if (status)
		return false;
	return true;
}

bool CloseRPC (void)
{
	RPC_STATUS status;
  status = RpcMgmtStopServerListening(NULL);
  if (!status)
	{
	  status = RpcServerUnregisterIf(NULL, NULL, FALSE);
  }

	DeleteCriticalSection (&g_CritSection);
	return true;
}
