summaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/proxy/proxy.cpp
blob: 153b2499b93f50697442cca3b972e644b6926b5f (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fnet/transport.h>
#include <vespa/fnet/transport_thread.h>
#include <vespa/fnet/connection.h>
#include <vespa/fnet/signalshutdown.h>
#include <vespa/fnet/packet.h>
#include <vespa/fnet/iserveradapter.h>
#include <vespa/fnet/ipacketstreamer.h>
#include <vespa/fnet/channel.h>
#include <vespa/fnet/connector.h>
#include <vespa/fastos/app.h>

#include <vespa/log/log.h>
LOG_SETUP("proxy");

class RawPacket : public FNET_Packet
{
private:
    FNET_DataBuffer _data;

public:
    RawPacket() : _data() {}
    uint32_t GetPCODE() override;
    uint32_t GetLength() override;
    void Encode(FNET_DataBuffer *) override;
    bool Decode(FNET_DataBuffer *src, uint32_t len) override;
};

uint32_t
RawPacket::GetPCODE()
{
    return 0;
}

uint32_t
RawPacket::GetLength()
{
    return _data.GetDataLen();
}

void
RawPacket::Encode(FNET_DataBuffer *dst)
{
    dst->WriteBytes(_data.GetData(), _data.GetDataLen());
}

bool
RawPacket::Decode(FNET_DataBuffer *src, uint32_t len)
{
    _data.WriteBytes(src->GetData(), len);
    src->DataToDead(len);
    return true;
}


class Bridge : public FNET_IPacketHandler
{
private:
    FNET_Channel    *_client;
    FNET_Connection *_server;

    Bridge(const Bridge &);
    Bridge &operator=(const Bridge &);

public:
    Bridge() : _client(nullptr), _server(nullptr) {}

    enum packet_source {
        CLIENT = 0,
        SERVER = 1
    };

    void SetConns(FNET_Channel *client,
                  FNET_Connection *server)
    {
        _client = client;
        _server = server;
    }

    HP_RetCode HandlePacket(FNET_Packet *packet, FNET_Context context) override;
};


FNET_IPacketHandler::HP_RetCode
Bridge::HandlePacket(FNET_Packet *packet, FNET_Context context)
{
    HP_RetCode ret = FNET_KEEP_CHANNEL;

    if (packet->IsChannelLostCMD()) {

        if (context._value.INT == CLIENT) {

            if (_server != nullptr) {
                LOG(info, "client connection lost");
                _server->Owner()->Close(_server);
            }
            ret = FNET_FREE_CHANNEL;
            _client = nullptr;

        } else if (context._value.INT == SERVER) {

            if (_client != nullptr) {
                LOG(info, "server connection lost");
                _client->GetConnection()->Owner()->Close(_client->GetConnection());
            }
            _server->SubRef();
            _server = nullptr;

        }

        if (_client == nullptr && _server == nullptr)
            delete this;

    } else {

        if (context._value.INT == CLIENT) {
            if (_server != nullptr)
                _server->PostPacket(packet, FNET_NOID);
            else
                packet->Free();

        } else if (context._value.INT == SERVER) {
            if (_client != nullptr)
                _client->Send(packet);
            else
                packet->Free();

        }
    }

    // The admin channel on a client connection (in this case, the
    // connection with the server) are freed when the connection
    // object is destructed. The admin channel on a server connection
    // however (in this case the channel connecting us with the
    // client) must be treated as a normal channel.

    return ret;
}


class Proxy : public FNET_IServerAdapter,
              public FNET_IPacketStreamer,
              public FastOS_Application
{
private:
    FNET_Transport _transport;

public:
    Proxy() : _transport() {}
    ~Proxy() override { }
    bool GetPacketInfo(FNET_DataBuffer *src, uint32_t *plen, uint32_t *pcode, uint32_t *chid, bool *) override;
    FNET_Packet *Decode(FNET_DataBuffer *src, uint32_t plen, uint32_t pcode, FNET_Context) override;
    void Encode(FNET_Packet *packet, uint32_t chid, FNET_DataBuffer *dst) override;
    // ---------------------------------------------
    bool InitAdminChannel(FNET_Channel *channel) override;
    bool InitChannel(FNET_Channel *, uint32_t) override;
    // ---------------------------------------------
    int Main() override;
};


bool
Proxy::GetPacketInfo(FNET_DataBuffer *src, uint32_t *plen,
                     uint32_t *pcode, uint32_t *chid, bool *)
{
    if (src->GetDataLen() == 0) {
        return false;
    }
    *pcode = 0;
    *plen = src->GetDataLen();
    *chid = FNET_NOID;
    return true;
}

FNET_Packet *
Proxy::Decode(FNET_DataBuffer *src, uint32_t plen,
              uint32_t, FNET_Context)
{
    RawPacket *packet = new RawPacket();
    packet->Decode(src, plen);
    return packet;
}

void
Proxy::Encode(FNET_Packet *packet, uint32_t chid,
              FNET_DataBuffer *dst)
{
    uint32_t pcode = packet->GetPCODE();
    uint32_t len = packet->GetLength();
    (void) pcode;
    (void) chid;
    (void) len;
    packet->Encode(dst);
}

// ---------------------------------------------

bool
Proxy::InitAdminChannel(FNET_Channel *channel)
{
    Bridge *bridge = new Bridge();
    FNET_Connection *server = _transport.Connect(_argv[2], this, bridge,
                                                 FNET_Context(Bridge::SERVER));
    if (server == nullptr) {
        channel->GetConnection()->Owner()->Close(channel->GetConnection());
        delete bridge;
        return false;
    }
    bridge->SetConns(channel, server);
    channel->SetHandler(bridge);
    channel->SetContext(FNET_Context((uint32_t)Bridge::CLIENT));
    return true;
}

bool
Proxy::InitChannel(FNET_Channel *, uint32_t)
{
    return false;
}

// ---------------------------------------------

int
Proxy::Main()
{
    FNET_SignalShutDown::hookSignals();
    if (_argc != 3) {
        fprintf(stderr, "usage: %s <listen spec> <target spec>\n", _argv[0]);
        return 1;
    }

    FNET_Connector *listener =
        _transport.Listen(_argv[1], this, this);
    if (listener != nullptr)
        listener->SubRef();

    FNET_SignalShutDown ssd(_transport);
    _transport.Main();
    return 0;
}


int
main(int argc, char **argv)
{
    Proxy myapp;
    return myapp.Entry(argc, argv);
}