aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/gatscon/proxythread.cpp
blob: 574b56b37aa553e6b1487b8e45b5b8c20b1fb2f2 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "proxythread.h"

#include <gats/types.h>

#include <bu/tcpsocket.h>
#include <bu/tcpserversocket.h>
#include <bu/sio.h>
#include <bu/membuf.h>

using namespace Bu;

ProxyThread::ProxyThread( QObject *pParent, int iPortIn,
		const QByteArray &baHostOut, int iPortOut ) :
	QThread( pParent ),
	pHost( NULL ),
	iPortIn( iPortIn ),
	baHostOut( baHostOut ),
	iPortOut( iPortOut ),
	gsCli( ssCli )
{
	pHost = new ProxyHostThread( pParent, this );
}

ProxyThread::~ProxyThread()
{
}

void ProxyThread::send( Gats::Object *pObj )
{
	MemBuf bg;
	Gats::GatsStream gs( bg );
	gs.writeObject( pObj );
	ssCli.write( bg.getString().getStr(), bg.getString().getSize() );
}

void ProxyThread::run()
{
	int iSockIn;

	{
		TcpServerSocket tsIn( iPortIn );
		do
		{
			iSockIn = tsIn.accept( 5 );
		} while( iSockIn < 0 );
	}

	emit gotConnection();

	ssCli.setStream( new TcpSocket( iSockIn ) );
	ssCli.setBlocking( true );

	pHost->setStream(
		new TcpSocket( baHostOut.constData(), iPortOut )
		);

	pHost->start();

	while( !ssCli.isEos() )
	{
		Gats::Object *pObj = gsCli.readObject();
		if( pObj == NULL )
			continue;

		pHost->send( pObj );
		emit recv( pObj );
	}

}

ProxyHostThread::ProxyHostThread( QObject *pParent, ProxyThread *pClient ) :
	QThread( pParent ),
	pClient( pClient ),
	ssHst(),
	gsHst( ssHst )
{
}

ProxyHostThread::~ProxyHostThread()
{
}

void ProxyHostThread::send( Gats::Object *pObj )
{
	MemBuf bg;
	Gats::GatsStream gs( bg );
	gs.writeObject( pObj );
	ssHst.write( bg.getString().getStr(), bg.getString().getSize() );
}

void ProxyHostThread::setStream( Bu::Stream *pStr )
{
	ssHst.setStream( pStr );
}

void ProxyHostThread::run()
{
	while( !ssHst.isEos() )
	{
		Gats::Object *pObj = gsHst.readObject();
		if( pObj == NULL )
			continue;

		pClient->send( pObj );
		emit recv( pObj );
	}
}