summaryrefslogtreecommitdiff
path: root/protocols/Xfire/src/Xfire_proxy.cpp
blob: f2fe0659a685dc77c0e40caee77c3fd48ccdbf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "stdafx.h"

#include "Xfire_proxy.h"

extern HANDLE hNetlib;
HANDLE hBindPort=NULL;
HANDLE netlibcon=NULL;


void FromServerToClient(LPVOID lParam) {
	char buf[1024]={0};

	HANDLE hConnection=(HANDLE)lParam;
	do {
		if (!hBindPort) return;

		int cbRead = Netlib_Recv(netlibcon, buf, sizeof(buf), 0);
		if ( cbRead == SOCKET_ERROR)
			break;

		if (cbRead) {
			Netlib_Send(hConnection, buf, cbRead, 0);
		}
	}
	while(1);
}

//xfireclient baut verbindung auf
void XfireclientConnecting(HANDLE hConnection, DWORD, void* extra )
{
	char buf[1024]={0};

	//verbindung zum richtigen xfire server aufbauen
	NETLIBOPENCONNECTION ncon = { 0 };
	ncon.cbSize = sizeof(ncon);
	ncon.szHost = "206.220.42.147";
	ncon.wPort = (WORD)atol("25999");
	ncon.timeout=5;
	netlibcon = (HANDLE) CallService(MS_NETLIB_OPENCONNECTION, (WPARAM) hNetlib, (LPARAM) & ncon);

	if (!netlibcon) {
		Netlib_CloseHandle(hConnection);
	}

	mir_forkthread(FromServerToClient,(LPVOID)hConnection);

	//schleife behandelt empfangende daten
	do {
		int cbRead = Netlib_Recv(hConnection, buf, sizeof(buf), 0);
		if ( cbRead == SOCKET_ERROR)
		{
			Netlib_CloseHandle(hConnection);
			Netlib_CloseHandle(netlibcon);
			break;
		}

		if (cbRead) {
			if (!Netlib_Send(netlibcon, buf, cbRead, 0))
			{
				Netlib_CloseHandle(hConnection);
				Netlib_CloseHandle(netlibcon);
				break;
			}
		}
	}
	while(1);
}

//inits nachdem alle module geladen wurden
int AfterSystemModulesLoaded(WPARAM wParam,LPARAM lParam)
{
	//init netlib handle
	NETLIBUSER nlu = {0};
	nlu.cbSize = sizeof(nlu);
	nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_INCOMING;
	nlu.szSettingsModule = protocolname;
	nlu.szDescriptiveName = "XFire server connection";
	hNetlib = (HANDLE) CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM) & nlu);

	//init socet server
	NETLIBBIND nb = {0};
	nb.cbSize = NETLIBBIND_SIZEOF_V2;
	nb.pfnNewConnectionV2 = XfireclientConnecting;
	nb.pExtra = NULL;
	nb.wPort = 25999;
	hBindPort = (HANDLE)CallService(MS_NETLIB_BINDPORT, (WPARAM)hNetlib,(LPARAM) &nb);
	return 0;
}

int initXfireProxy()
{
	//inits nach dem alle module geladen wurden
	HookEvent(ME_SYSTEM_MODULESLOADED, AfterSystemModulesLoaded);
	return 0;
}