aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/gatscon/proxythread.cpp
blob: 733af7f5a2b09c08b042490db5992a5707d4f9bd (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
109
110
111
112
113
114
115
/*
 * Copyright (C) 2007-2012 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 <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 );
    }
}