aboutsummaryrefslogtreecommitdiff
path: root/src/DirectConnection.cpp
blob: e41a4a543f54dbfae48430445c446f566fdff80e (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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "../include/sibs/DirectConnection.hpp"
#include "../include/Log.hpp"
#include <cstdio>
#include <cstring>
#include <random>
#include <sibs/SafeSerializer.hpp>
#include <sibs/SafeDeserializer.hpp>

#ifndef WIN32
   #include <arpa/inet.h>
   #include <netdb.h>
#else
   #include <winsock2.h>
   #include <ws2tcpip.h>
#endif
#include <udt/udt.h>

namespace sibs
{
    static std::random_device rd;
    static std::mt19937 gen(rd());

    static u32 generateRandomNumber(u32 start, u32 end)
    {
        std::uniform_int_distribution<> dis(start, end);
        return dis(gen);
    }

    // Max received data size allowed when receiving regular data, receive data as file to receive more data
    const int MAX_RECEIVED_DATA_SIZE = 1024 * 1024 * 1; // 1Mb

    bool DirectConnectionPeer::operator == (const DirectConnectionPeer &other) const
    {
        return socket->udtSocket == other.socket->udtSocket;
    }

    bool DirectConnectionPeer::operator != (const DirectConnectionPeer &other) const
    {
        return !(*this == other);
    }
    
    DirectConnections::DirectConnections(u16 _port) : 
        port(_port == 0 ? (u16)generateRandomNumber(2000, 32000) : _port),
        alive(true),
        removeDisconnectedPeerCallback(nullptr)
    {
        UDT::startup();
        eid = UDT::epoll_create();
        receiveDataThread = std::thread(&DirectConnections::receiveData, this);
    }
    
    DirectConnections::~DirectConnections()
    {
        alive = false;
        peers.clear();
        UDT::epoll_release(eid);
        UDT::cleanup();
        receiveDataThread.join();
    }
    
    std::unique_ptr<Socket> DirectConnections::createSocket(const Ipv4 &addressToBind, bool rendezvous, bool reuseAddr, bool bind)
    {
        Log::debug("UDT: Creating socket for ipv4 address %s, port: %d", addressToBind.getAddress().c_str(), addressToBind.getPort());
        UDTSOCKET udtSocket = UDT::socket(AF_INET, SOCK_STREAM, 0);
        if(udtSocket == UDT::INVALID_SOCK)
        {
            std::string errMsg = "UDT: Failed to create socket, error: ";
            errMsg += UDT::getlasterror_desc();
            throw SocketCreateException(errMsg);
        }
        
        auto socket = std::make_unique<Socket>(udtSocket);
        UDT::setsockopt(udtSocket, 0, UDT_RENDEZVOUS, &rendezvous, sizeof(bool));
        UDT::setsockopt(udtSocket, 0, UDT_REUSEADDR, &reuseAddr, sizeof(bool));
        
        // Windows UDP issue
        // For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
        #ifdef WIN32
        int mss = 1052;
        UDT::setsockopt(socket, 0, UDT_MSS, &mss, sizeof(mss));
        #endif

        if(rendezvous || bind)
        {
            if(reuseAddr)
            {
                /*
                if(UDT::bind(socket, (sockaddr*)&addressToBind.address, sizeof(addressToBind.address)) == UDT::ERROR)
                {
                    std::string errMsg = "UDT: Failed to bind, error: ";
                    errMsg += UDT::getlasterror_desc();
                    throw SocketCreateException(errMsg);
                }
                */
                Ipv4 myAddr = addressToBind;
                for(int i = 0; i < 2000; ++i)
                {
                    if(UDT::bind(udtSocket, (sockaddr*)&myAddr.address, sizeof(myAddr.address)) == UDT::ERROR)
                    {
                        u16 newPort = (u16)generateRandomNumber(2000, 32000);
                        Log::warn("DirectConnections: failed to bind socket to port %d, trying port %d. Fail reason: %s", port, newPort, UDT::getlasterror_desc());
                        port = newPort;
                        myAddr.address.sin_port = htons(port);
                    }
                    else
                        return socket;
                }
                throw SocketCreateException("UDT: Failed to bind after 2000 tries");
            }
            else
            {
                Ipv4 myAddr = addressToBind;
                for(int i = 0; i < 2000; ++i)
                {
                    if(UDT::bind(udtSocket, (sockaddr*)&myAddr.address, sizeof(myAddr.address)) == UDT::ERROR)
                    {
                        port = (u16)generateRandomNumber(2000, 32000);
                        myAddr.address.sin_port = htons(port);
                    }
                    else
                        return socket;
                }
                throw SocketCreateException("UDT: Failed to bind after 2000 tries");
            }
        }
        
        return socket;
    }
    
    void DirectConnections::connectServer(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc)
    {
        connect(address, false, true, connectCallbackFunc, receiveDataCallbackFunc, true);
    }
    
    void DirectConnections::connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc)
    {
        connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true);
    }
    
    void DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind)
    {
        std::thread([this, address, rendezvous, reuseAddr, connectCallbackFunc, receiveDataCallbackFunc, bind]()
        {
            std::shared_ptr<DirectConnectionPeer> peer = getPeerByAddress(address);
            if(peer)
            {
                // this doesn't really matter, we always call connect with same callback function
                peer->receiveDataCallbackFunc = receiveDataCallbackFunc;
                if(connectCallbackFunc)
                    connectCallbackFunc(peer, PubSubResult::OK, "");
                return;
            }
            else
            {
                peer = std::make_shared<DirectConnectionPeer>();
                peerByAddressMap[address] = peer;
            }
            
            std::unique_ptr<Socket> socket;
            try
            {
                socket = createSocket(Ipv4(nullptr, port), rendezvous, reuseAddr, bind);
            }
            catch(SocketCreateException &e)
            {
                if(connectCallbackFunc)
                    connectCallbackFunc(peer, PubSubResult::ERROR, e.what());
                return;
            }
            
            Log::debug("DirectConnections: Connecting to peer (ip: %s, port: %d, rendezvous: %s)", address.getAddress().c_str(), address.getPort(), rendezvous ? "yes" : "no");
            if(UDT::connect(socket->udtSocket, (sockaddr*)&address.address, sizeof(address.address)) == UDT::ERROR)
            {
                if(connectCallbackFunc)
                    connectCallbackFunc(peer, PubSubResult::ERROR, UDT::getlasterror_desc());
                return;
            }
            
            UDT::epoll_add_usock(eid, socket->udtSocket);
            socket->eid = eid;
            peersMutex.lock();
            peers[socket->udtSocket] = peer;
            peer->socket = std::move(socket);
            peer->address = address;
            peer->receiveDataCallbackFunc = receiveDataCallbackFunc;
            peersMutex.unlock();
            
            if(connectCallbackFunc)
                connectCallbackFunc(peer, PubSubResult::OK, "");
        }).detach();
    }
    
    bool DirectConnections::send(const std::shared_ptr<DirectConnectionPeer> peer, std::shared_ptr<Message> data, PubSubSendDataCallback sendDataCallbackFunc)
    {
        if(data->getDataSize() == 0)
            return true;

        if(data->getDataSize() > 819200) // 800kb
            return false;

        // TODO: Replace this with light-weight threads (fibers)?
        std::thread([peer, data, sendDataCallbackFunc]()
        {
            int sentSize = UDT::send(peer->socket->udtSocket, (char*)data->data(), data->getRawSize(), 0);
            if(sentSize == UDT::ERROR)
            {
                if(sendDataCallbackFunc)
                    sendDataCallbackFunc(PubSubResult::ERROR, UDT::getlasterror_desc());
            }
            else if(sendDataCallbackFunc)
                sendDataCallbackFunc(PubSubResult::OK, "");
        }).detach();
        return true;
    }

    void DirectConnections::onRemoveDisconnectedPeer(PubSubOnRemoveDisconnectedPeerCallback callbackFunc)
    {
        removeDisconnectedPeerCallback = callbackFunc;
    }
    
    bool DirectConnections::removePeer(int peerSocket)
    {
        bool wasRemoved = false;
        std::lock_guard<std::mutex> lock(peersMutex);
        auto peerIt = peers.find(peerSocket);
        if(peerIt != peers.end())
        {
            if(removeDisconnectedPeerCallback)
                removeDisconnectedPeerCallback(peerIt->second);
            peers.erase(peerIt);
            wasRemoved = true;
        }
        return wasRemoved;
    }

    std::vector<std::shared_ptr<DirectConnectionPeer>> DirectConnections::getPeers()
    {
        std::vector<std::shared_ptr<DirectConnectionPeer>> result;
        result.reserve(peers.size());
        std::lock_guard<std::mutex> lock(peersMutex);
        for(auto &it : peers)
        {
            result.push_back(it.second);
        }
        return result;
    }
    
    void DirectConnections::removeDisconnectedPeers()
    {
        peersMutex.lock();
        for(std::unordered_map<int, std::shared_ptr<DirectConnectionPeer>>::iterator it = peers.begin(); it != peers.end(); )
        {
            int socket = it->first;
            UDTSTATUS peerSocketStatus = UDT::getsockstate(socket);
            if(peerSocketStatus == UDTSTATUS::BROKEN || peerSocketStatus == UDTSTATUS::CLOSING || peerSocketStatus == UDTSTATUS::CLOSED || peerSocketStatus == UDTSTATUS::NONEXIST)
            {
                if(removeDisconnectedPeerCallback)
                    removeDisconnectedPeerCallback(it->second);

                Log::debug("UDT: Connection was broken to socket %d (peer most likely disconnected), removing peer", socket);
                it = peers.erase(it);
                Log::debug("UDT: Removed peer socket %d", socket);
            }
            else
                ++it;
        }
        peersMutex.unlock();
    }

    std::shared_ptr<DirectConnectionPeer> DirectConnections::getPeerByAddress(const Ipv4 &address) const
    {
        auto it = peerByAddressMap.find(address);
        if(it != peerByAddressMap.end())
            return it->second;
        return nullptr;
    }
    
    void DirectConnections::receiveData()
    {
        std::vector<char> data;
        data.resize(MAX_RECEIVED_DATA_SIZE);
        
        Log::debug("DirectConnections::receiveData(): waiting for events");
        std::set<UDTSOCKET> readfds;
        while(alive)
        {
            removeDisconnectedPeers();
            int numfsReady = UDT::epoll_wait(eid, &readfds, nullptr, 1000);
            if(numfsReady == 0)
            {
                continue;
            }
            else if(numfsReady == -1)
            {
                if(!alive)
                    continue;
                    
                if(UDT::getlasterror_code() == UDT::ERRORINFO::ETIMEOUT)
                {
                    continue;
                }
                else
                {
                    Log::error("UDT: Stop receiving data, got error: %s", UDT::getlasterror_desc());
                    return;
                }
            }
            
            for(UDTSOCKET receivedDataFromPeer : readfds)
            {
                bool peerDisconnected = false;
                usize receivedTotalSize = 0;
                int receivedDataStatus = receiveDataFromPeer(receivedDataFromPeer, data.data(), &receivedTotalSize);
                if(receivedDataStatus == 0)
                {
                    peersMutex.lock();
                    auto peer = peers[receivedDataFromPeer];
                    peersMutex.unlock();
                    try
                    {
                        Log::debug("DirectConnection: Received data from peer: (ip: %s, port: %d)", peer->address.getAddress().c_str(), peer->address.getPort());
                        if(peer->receiveDataCallbackFunc && receivedTotalSize > 0)
                        {
                            static_assert(sizeof(MessageType) == sizeof(u8), "");
                            MessageType messageType = (MessageType)data[0];
                            peer->receiveDataCallbackFunc(peer, messageType, data.data() + 1, receivedTotalSize - 1);
                        }
                    }
                    catch(std::exception &e)
                    {
                        Log::error("UDT: Receive callback function threw exception: %s, ignoring...", e.what());
                    }
                }
                else if(receivedDataStatus == CUDTException::EINVSOCK)
                {
                    Log::debug("UDT: Invalid socket %d, did remote peer disconnect?", receivedDataFromPeer);
                    peerDisconnected = true;
                }
                else if(receivedDataStatus == CUDTException::ECONNLOST)
                {
                    Log::debug("UDT: Connection was broken to socket %d (peer most likely disconnected), removing peer", receivedDataFromPeer);
                    peerDisconnected = true;
                }
                
                if(peerDisconnected)
                {
                    if(removePeer(receivedDataFromPeer))
                        Log::debug("UDT: Removed peer socket %d", receivedDataFromPeer);
                    else
                        Log::error("UDT: Failed to remove peer socket %d, system said we got data from it but we are not connected to it", receivedDataFromPeer);
                }
            }
            readfds.clear();
        }
    }
    
    int DirectConnections::receiveDataFromPeer(const int socket, char *output, usize *receivedTotalSize)
    {
        *receivedTotalSize = 0;
        while(*receivedTotalSize < MAX_RECEIVED_DATA_SIZE)
        {
            int dataAvailableSize;
            int receiveSizeDataTypeSize = sizeof(dataAvailableSize);
            if(UDT::getsockopt(socket, 0, UDT_RCVDATA, &dataAvailableSize, &receiveSizeDataTypeSize) == UDT::ERROR)
            {
                Log::error("UDT: Failed to receive data available size, error: %s (%d)", UDT::getlasterror_desc(), UDT::getlasterror_code());
                return UDT::getlasterror_code();
            }
            
            if(dataAvailableSize == 0)
                return 0;
            
            int receivedSize = UDT::recv(socket, &output[*receivedTotalSize], MAX_RECEIVED_DATA_SIZE - *receivedTotalSize, 0);
            if(receivedSize == UDT::ERROR)
            {
                Log::error("UDT: Failed to receive data, error: %s (%d)", UDT::getlasterror_desc(), UDT::getlasterror_code());
                return UDT::getlasterror_code();
            }
            
            if(receivedSize == 0)
            {
                return 0;
            }
            
            (*receivedTotalSize) += receivedSize;
        }
        
        Log::error("UDT: Received too much data, ignoring...");
        return 0;
    }

    std::vector<u8> DirectConnectionsUtils::serializePeers(const std::vector<std::shared_ptr<DirectConnectionPeer>> &peers)
    {
        sibs::SafeSerializer serializer;
        for(const auto &it : peers)
        {
            serializer.add((u32)it->address.address.sin_family);
            serializer.add((u32)it->address.address.sin_addr.s_addr);
            serializer.add((u16)it->address.address.sin_port);
        }
        return serializer.getBuffer();
    }

    std::vector<std::shared_ptr<DirectConnectionPeer>> DirectConnectionsUtils::deserializePeers(const u8 *data, const usize size)
    {
        std::vector<std::shared_ptr<DirectConnectionPeer>> result;
        sibs::SafeDeserializer deserializer(data, size);
        while(!deserializer.empty())
        {
            std::shared_ptr<DirectConnectionPeer> peer = std::make_shared<DirectConnectionPeer>();
            peer->address.address.sin_family = deserializer.extract<u32>();
            peer->address.address.sin_addr.s_addr = deserializer.extract<u32>();
            peer->address.address.sin_port = deserializer.extract<u32>();
            result.push_back(peer);
        }
        return result;
    }
}