/* * Copyright (C) 2007-2013 Xagasoft, All rights reserved. * * This file is part of the libgats library and is released under the * terms of the license contained in the file LICENSE. */ #include "proxythread.h" #include #include #include #include #include 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 ); } }