#include "gosGML.hpp"
//===========================================================================//
// File:	 Remote.cpp														 //
// Project:  gosGml															 //
// Contents:																 //
//---------------------------------------------------------------------------//
// Date      Who    Modification                                             //
// 9/15/00   DP     Created													 //
//---------------------------------------------------------------------------//
// Copyright (C) 2000 Microsoft Corp.						                 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//
#include "Remote.h"

RemoteM* RMCA=0; //Remote Master Class Address

//int af(int,int){
//	return 0;
//}

DWORD WINAPI RemoteMTS(LPVOID lpParameter)
{
	lpParameter;
	RMCA->NTE();
	return 1;
}
/*
INTERNET_STATUS_CLOSING_CONNECTION 
	Closing the connection to the server. The lpvStatusInformation parameter is NULL. 
INTERNET_STATUS_CONNECTED_TO_SERVER 
	Successfully connected to the socket address (SOCKADDR) pointed to by lpvStatusInformation. 
INTERNET_STATUS_CONNECTING_TO_SERVER 
	Connecting to the socket address (SOCKADDR) pointed to by lpvStatusInformation. 
INTERNET_STATUS_CONNECTION_CLOSED 
	Successfully closed the connection to the server. The lpvStatusInformation parameter is NULL. 
INTERNET_STATUS_CTL_RESPONSE_RECEIVED 
	Not currently implemented. 
INTERNET_STATUS_DETECTING_PROXY 
	Notifies the client application that a proxy has been detected. 
INTERNET_STATUS_HANDLE_CLOSING 
	This handle value has been terminated. 
INTERNET_STATUS_HANDLE_CREATED 
	Used by InternetConnect to indicate it has created the new handle. This lets the application call InternetCloseHandle from another thread, if the connect is taking too long. The lpvStatusInformation parameter contains the address of an INTERNET_ASYNC_RESULT structure. 
INTERNET_STATUS_INTERMEDIATE_RESPONSE 
	Received an intermediate (100 level) status code message from the server. 
INTERNET_STATUS_NAME_RESOLVED 
	Successfully found the IP address of the name contained in lpvStatusInformation. 
INTERNET_STATUS_PREFETCH 
	Not currently implemented. 
INTERNET_STATUS_RECEIVING_RESPONSE 
	Waiting for the server to respond to a request. The lpvStatusInformation parameter is NULL. 
INTERNET_STATUS_REDIRECT 
	An HTTP request is about to automatically redirect the request. The lpvStatusInformation parameter points to the new URL. At this point, the application can read any data returned by the server with the redirect response and can query the response headers. It can also cancel the operation by closing the handle. This callback is not made if the original request specified INTERNET_FLAG_NO_AUTO_REDIRECT. 
INTERNET_STATUS_REQUEST_COMPLETE 
	An asynchronous operation has been completed. The lpvStatusInformation parameter contains the address of an INTERNET_ASYNC_RESULT structure. 
INTERNET_STATUS_REQUEST_SENT 
	Successfully sent the information request to the server. The lpvStatusInformation parameter points to a DWORD containing the number of bytes sent. 
INTERNET_STATUS_RESOLVING_NAME 
	Looking up the IP address of the name contained in lpvStatusInformation. 
INTERNET_STATUS_RESPONSE_RECEIVED 
	Successfully received a response from the server. The lpvStatusInformation parameter points to a DWORD containing the number of bytes received. 
INTERNET_STATUS_SENDING_REQUEST 
	Sending the information request to the server. The lpvStatusInformation parameter is NULL. 
INTERNET_STATUS_STATE_CHANGE 
	Moved between a secure (HTTPS) and a nonsecure (HTTP) site. This can be one of the following values: 
	INTERNET_STATE_CONNECTED 
		Connected state (mutually exclusive with disconnected state). 
	INTERNET_STATE_DISCONNECTED 
		Disconnected state. No network connection could be established. 
	INTERNET_STATE_DISCONNECTED_BY_USER 
		Disconnected by user request. 
	INTERNET_STATE_IDLE 
		No network requests are being made by the Win32® Internet functions. 
	INTERNET_STATE_BUSY 
		Network requests are being made by the Win32 Internet functions. 
	INTERNET_STATUS_USER_INPUT_REQUIRED 
		The request requires user input to be completed.



//
// status manifests for Internet status callback
//

#define INTERNET_STATUS_RESOLVING_NAME          10
#define INTERNET_STATUS_NAME_RESOLVED           11
#define INTERNET_STATUS_CONNECTING_TO_SERVER    20
#define INTERNET_STATUS_CONNECTED_TO_SERVER     21
#define INTERNET_STATUS_SENDING_REQUEST         30
#define INTERNET_STATUS_REQUEST_SENT            31
#define INTERNET_STATUS_RECEIVING_RESPONSE      40
#define INTERNET_STATUS_RESPONSE_RECEIVED       41
#define INTERNET_STATUS_CTL_RESPONSE_RECEIVED   42
#define INTERNET_STATUS_PREFETCH                43
#define INTERNET_STATUS_CLOSING_CONNECTION      50
#define INTERNET_STATUS_CONNECTION_CLOSED       51
#define INTERNET_STATUS_HANDLE_CREATED          60
#define INTERNET_STATUS_HANDLE_CLOSING          70
#define INTERNET_STATUS_REQUEST_COMPLETE        100
#define INTERNET_STATUS_REDIRECT                110
#define INTERNET_STATUS_INTERMEDIATE_RESPONSE   120
#define INTERNET_STATUS_STATE_CHANGE            200

//
// the following can be indicated in a state change notification:
//

#define INTERNET_STATE_CONNECTED                0x00000001  // connected state (mutually exclusive with disconnected)
#define INTERNET_STATE_DISCONNECTED             0x00000002  // disconnected from network
#define INTERNET_STATE_DISCONNECTED_BY_USER     0x00000010  // disconnected by user request
#define INTERNET_STATE_IDLE                     0x00000100  // no network requests being made (by Wininet)
#define INTERNET_STATE_BUSY                     0x00000200  // network requests being made (by Wininet)
*/
void CBFW(HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
{
	((Remote*)dwContext)->CBF(hInternet, dwContext, dwInternetStatus, lpvStatusInformation, dwStatusInformationLength);	//I really hope this never gets called once the connection is thought to be over!
}

RemoteM::RemoteM(bool Nothing, int maxSimCon, int maxConPerServer, bool reUseRemoteCon)
:MaxSimCon(maxSimCon),MaxConPerServer(maxConPerServer),ReUseRemoteCon(reUseRemoteCon),InitState(0)
{
	Nothing;
	if(RMCA)
		STOP(("Remote master already exists"));
	RMCA=this;
	Icon = InternetOpen("anonymous", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); //Internet Settings
	InternetSetStatusCallback(Icon, CBFW);
	RemoteHash = new Hash<DWORD, DWORD>(59);
	ThreadH = CreateThread(NULL, 0, &RemoteMTS, NULL, NULL, &ThreadID);
	//GetFile(NULL,&af);
}

RemoteM::~RemoteM()
{
	RMCA=NULL;
	delete RemoteHash;
}

void RemoteM::OpLoop()
{

}

int RemoteM::GetFile(char* URL, RemoteNF* x, int NotifyLevel)
{
	URL;x;NotifyLevel;
	return 0;
}

void RemoteM::NTE() //New Thread Entry
{
	if(InitState)
		return;
	InitState=1; //now initialized and ready to go
	RMCA=NULL;
	delete new Remote("http://gameos", 0, this);
	OpLoop();
}

Remote::Remote(char* uRL, int notifyLevel, RemoteM* master)
:URL(uRL),NotifyLevel(notifyLevel),Master(master)
{
	static int UIDc=1;
	if(!URL || strlen(URL) < 7)
	{
		Status = -1;	//honestly, fuck off
		return;
	}
	UID=UIDc;
	UIDc++;
	//BOOL InetCon = InternetCheckConnection(NULL,NULL,NULL);  //this doesn't work worth crap
	HINTERNET bob2 = InternetConnect(Master->Icon,URL,INTERNET_DEFAULT_HTTP_PORT,"anonymous","password",INTERNET_SERVICE_HTTP, NULL,(DWORD)this);
	InternetSetStatusCallback(bob2, CBFW);
	HINTERNET bob3 = HttpOpenRequest(bob2,"GET","/","HTTP/1.1",NULL,NULL,NULL,NULL);
	InternetSetStatusCallback(bob3, CBFW);
	BOOL SReq = HttpSendRequest(bob3,NULL,0,NULL,0);
	BOOL DA = InternetQueryDataAvailable( bob3,&NumBytes,0,0);
	buff=new char[NumBytes+1];
	*(buff+NumBytes)=0;
	BOOL RF = InternetReadFile(bob3,buff,NumBytes,&NumBytes2);
	UID+=0;
	DA;RF;SReq;
}

void Remote::CBF(HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
{
	hInternet;dwContext;dwInternetStatus;lpvStatusInformation;dwStatusInformationLength;
}

Remote::~Remote()
{
//	if(URL)
//		delete[] URL;
}