| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
/***************************************************************************** |
| 12 |
Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. |
| 13 |
All rights reserved. |
| 14 |
|
| 15 |
Redistribution and use in source and binary forms, with or without |
| 16 |
modification, are permitted provided that the following conditions are |
| 17 |
met: |
| 18 |
|
| 19 |
* Redistributions of source code must retain the above |
| 20 |
copyright notice, this list of conditions and the |
| 21 |
following disclaimer. |
| 22 |
|
| 23 |
* Redistributions in binary form must reproduce the |
| 24 |
above copyright notice, this list of conditions |
| 25 |
and the following disclaimer in the documentation |
| 26 |
and/or other materials provided with the distribution. |
| 27 |
|
| 28 |
* Neither the name of the University of Illinois |
| 29 |
nor the names of its contributors may be used to |
| 30 |
endorse or promote products derived from this |
| 31 |
software without specific prior written permission. |
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 34 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 35 |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 36 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 37 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 38 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 39 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 40 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 41 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 42 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 43 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 |
*****************************************************************************/ |
| 45 |
|
| 46 |
/***************************************************************************** |
| 47 |
written by |
| 48 |
Yunhong Gu, last updated 07/09/2011 |
| 49 |
modified by |
| 50 |
Haivision Systems Inc. |
| 51 |
*****************************************************************************/ |
| 52 |
|
| 53 |
#include "platform_sys.h" |
| 54 |
|
| 55 |
#include <exception> |
| 56 |
#include <stdexcept> |
| 57 |
#include <typeinfo> |
| 58 |
#include <iterator> |
| 59 |
#include <vector> |
| 60 |
|
| 61 |
#include <cstring> |
| 62 |
#include "utilities.h" |
| 63 |
#include "netinet_any.h" |
| 64 |
#include "api.h" |
| 65 |
#include "core.h" |
| 66 |
#include "epoll.h" |
| 67 |
#include "logging.h" |
| 68 |
#include "threadname.h" |
| 69 |
#include "srt.h" |
| 70 |
#include "udt.h" |
| 71 |
|
| 72 |
#ifdef _WIN32 |
| 73 |
#include <win/wintime.h> |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifdef _MSC_VER |
| 77 |
#pragma warning(error : 4530) |
| 78 |
#endif |
| 79 |
|
| 80 |
using namespace std; |
| 81 |
using namespace srt_logging; |
| 82 |
using namespace srt::sync; |
| 83 |
|
| 84 |
void srt::CUDTSocket::construct() |
| 85 |
{ |
| 86 |
#if ENABLE_BONDING |
| 87 |
m_GroupOf = NULL; |
| 88 |
m_GroupMemberData = NULL; |
| 89 |
#endif |
| 90 |
setupMutex(m_AcceptLock, "Accept"); |
| 91 |
setupCond(m_AcceptCond, "Accept"); |
| 92 |
setupMutex(m_ControlLock, "Control"); |
| 93 |
} |
| 94 |
|
| 95 |
srt::CUDTSocket::~CUDTSocket() |
| 96 |
{ |
| 97 |
releaseMutex(m_AcceptLock); |
| 98 |
releaseCond(m_AcceptCond); |
| 99 |
releaseMutex(m_ControlLock); |
| 100 |
} |
| 101 |
|
| 102 |
void srt::CUDTSocket::resetAtFork() |
| 103 |
{ |
| 104 |
m_UDT.resetAtFork(); |
| 105 |
resetCond(m_AcceptCond); |
| 106 |
} |
| 107 |
|
| 108 |
SRT_SOCKSTATUS srt::CUDTSocket::getStatus() |
| 109 |
{ |
| 110 |
// TTL in CRendezvousQueue::updateConnStatus() will set m_bConnecting to false. |
| 111 |
// Although m_Status is still SRTS_CONNECTING, the connection is in fact to be closed due to TTL expiry. |
| 112 |
// In this case m_bConnected is also false. Both checks are required to avoid hitting |
| 113 |
// a regular state transition from CONNECTING to CONNECTED. |
| 114 |
|
| 115 |
if (m_UDT.m_bBroken) |
| 116 |
return SRTS_BROKEN; |
| 117 |
|
| 118 |
// Connecting timed out |
| 119 |
if ((m_Status == SRTS_CONNECTING) && !m_UDT.m_bConnecting && !m_UDT.m_bConnected) |
| 120 |
return SRTS_BROKEN; |
| 121 |
|
| 122 |
return m_Status; |
| 123 |
} |
| 124 |
|
| 125 |
// [[using locked(m_GlobControlLock)]] |
| 126 |
void srt::CUDTSocket::breakSocket_LOCKED() |
| 127 |
{ |
| 128 |
// This function is intended to be called from GC, |
| 129 |
// under a lock of m_GlobControlLock. |
| 130 |
m_UDT.m_bBroken = true; |
| 131 |
m_UDT.m_iBrokenCounter = 0; |
| 132 |
HLOGC(smlog.Debug, log << "@" << m_SocketID << " CLOSING AS SOCKET"); |
| 133 |
m_UDT.closeInternal(); |
| 134 |
setClosed(); |
| 135 |
} |
| 136 |
|
| 137 |
void srt::CUDTSocket::setClosed() |
| 138 |
{ |
| 139 |
m_Status = SRTS_CLOSED; |
| 140 |
|
| 141 |
// a socket will not be immediately removed when it is closed |
| 142 |
// in order to prevent other methods from accessing invalid address |
| 143 |
// a timer is started and the socket will be removed after approximately |
| 144 |
// 1 second |
| 145 |
m_tsClosureTimeStamp = steady_clock::now(); |
| 146 |
} |
| 147 |
|
| 148 |
void srt::CUDTSocket::setBrokenClosed() |
| 149 |
{ |
| 150 |
m_UDT.m_iBrokenCounter = 60; |
| 151 |
m_UDT.m_bBroken = true; |
| 152 |
setClosed(); |
| 153 |
} |
| 154 |
|
| 155 |
bool srt::CUDTSocket::readReady() |
| 156 |
{ |
| 157 |
if (m_UDT.m_bConnected && m_UDT.isRcvBufferReady()) |
| 158 |
return true; |
| 159 |
|
| 160 |
if (m_UDT.m_bListening) |
| 161 |
return !m_QueuedSockets.empty(); |
| 162 |
|
| 163 |
return broken(); |
| 164 |
} |
| 165 |
|
| 166 |
bool srt::CUDTSocket::writeReady() const |
| 167 |
{ |
| 168 |
return (m_UDT.m_bConnected && (m_UDT.m_pSndBuffer->getCurrBufSize() < m_UDT.m_config.iSndBufSize)) || broken(); |
| 169 |
} |
| 170 |
|
| 171 |
bool srt::CUDTSocket::broken() const |
| 172 |
{ |
| 173 |
return m_UDT.m_bBroken || !m_UDT.m_bConnected; |
| 174 |
} |
| 175 |
|
| 176 |
//////////////////////////////////////////////////////////////////////////////// |
| 177 |
|
| 178 |
srt::CUDTUnited::CUDTUnited() |
| 179 |
: m_Sockets() |
| 180 |
, m_GlobControlLock() |
| 181 |
, m_IDLock() |
| 182 |
, m_mMultiplexer() |
| 183 |
, m_pCache(new CCache<CInfoBlock>) |
| 184 |
, m_bClosing(false) |
| 185 |
, m_GCStopCond() |
| 186 |
, m_InitLock() |
| 187 |
, m_iInstanceCount(0) |
| 188 |
, m_bGCStatus(false) |
| 189 |
, m_ClosedSockets() |
| 190 |
{ |
| 191 |
// Socket ID MUST start from a random value |
| 192 |
m_SocketIDGenerator = genRandomInt(1, MAX_SOCKET_VAL); |
| 193 |
m_SocketIDGenerator_init = m_SocketIDGenerator; |
| 194 |
|
| 195 |
// XXX An unlikely exception thrown from the below calls |
| 196 |
// might destroy the application before `main`. This shouldn't |
| 197 |
// be a problem in general. |
| 198 |
setupMutex(m_GCStartLock, "GCStart"); |
| 199 |
setupMutex(m_GCStopLock, "GCStop"); |
| 200 |
setupCond(m_GCStopCond, "GCStop"); |
| 201 |
setupMutex(m_GlobControlLock, "GlobControl"); |
| 202 |
setupMutex(m_IDLock, "ID"); |
| 203 |
setupMutex(m_InitLock, "Init"); |
| 204 |
// Global initialization code |
| 205 |
#ifdef _WIN32 |
| 206 |
WORD wVersionRequested; |
| 207 |
WSADATA wsaData; |
| 208 |
wVersionRequested = MAKEWORD(2, 2); |
| 209 |
|
| 210 |
if (0 != WSAStartup(wVersionRequested, &wsaData)) |
| 211 |
throw CUDTException(MJ_SETUP, MN_NONE, WSAGetLastError()); |
| 212 |
#endif |
| 213 |
CCryptoControl::globalInit(); |
| 214 |
HLOGC(inlog.Debug, log << "SRT Clock Type: " << SRT_SYNC_CLOCK_STR); |
| 215 |
} |
| 216 |
|
| 217 |
srt::CUDTUnited::~CUDTUnited() |
| 218 |
{ |
| 219 |
// Call it if it wasn't called already. |
| 220 |
// This will happen at the end of main() of the application, |
| 221 |
// when the user didn't call srt_cleanup(). |
| 222 |
enterCS(m_InitLock); |
| 223 |
stopGarbageCollector(); |
| 224 |
leaveCS(m_InitLock); |
| 225 |
closeAllSockets(); |
| 226 |
releaseMutex(m_GlobControlLock); |
| 227 |
releaseMutex(m_IDLock); |
| 228 |
releaseMutex(m_InitLock); |
| 229 |
// XXX There's some weird bug here causing this |
| 230 |
// to hangup on Windows. This might be either something |
| 231 |
// bigger, or some problem in pthread-win32. As this is |
| 232 |
// the application cleanup section, this can be temporarily |
| 233 |
// tolerated with simply exit the application without cleanup, |
| 234 |
// counting on that the system will take care of it anyway. |
| 235 |
#ifndef _WIN32 |
| 236 |
releaseCond(m_GCStopCond); |
| 237 |
#endif |
| 238 |
releaseMutex(m_GCStopLock); |
| 239 |
releaseMutex(m_GCStartLock); |
| 240 |
delete m_pCache; |
| 241 |
#ifdef _WIN32 |
| 242 |
WSACleanup(); |
| 243 |
#endif |
| 244 |
} |
| 245 |
|
| 246 |
string srt::CUDTUnited::CONID(SRTSOCKET sock) |
| 247 |
{ |
| 248 |
if (sock == 0) |
| 249 |
return ""; |
| 250 |
|
| 251 |
std::ostringstream os; |
| 252 |
os << "@" << sock << ":"; |
| 253 |
return os.str(); |
| 254 |
} |
| 255 |
|
| 256 |
bool srt::CUDTUnited::startGarbageCollector() |
| 257 |
{ |
| 258 |
|
| 259 |
ScopedLock guard(m_GCStartLock); |
| 260 |
if (!m_bGCStatus) |
| 261 |
{ |
| 262 |
m_bClosing = false; |
| 263 |
m_bGCStatus = StartThread(m_GCThread, garbageCollect, this, "SRT:GC"); |
| 264 |
} |
| 265 |
return m_bGCStatus; |
| 266 |
} |
| 267 |
|
| 268 |
void srt::CUDTUnited::stopGarbageCollector() |
| 269 |
{ |
| 270 |
|
| 271 |
ScopedLock guard(m_GCStartLock); |
| 272 |
if (m_bGCStatus) |
| 273 |
{ |
| 274 |
m_bGCStatus = false; |
| 275 |
{ |
| 276 |
CUniqueSync gclock (m_GCStopLock, m_GCStopCond); |
| 277 |
m_bClosing = true; |
| 278 |
gclock.notify_all(); |
| 279 |
} |
| 280 |
m_GCThread.join(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void srt::CUDTUnited::cleanupAllSockets() |
| 285 |
{ |
| 286 |
for (sockets_t::iterator i = m_Sockets.begin(); i != m_Sockets.end(); ++i) |
| 287 |
{ |
| 288 |
CUDTSocket* s = i->second; |
| 289 |
|
| 290 |
#if ENABLE_BONDING |
| 291 |
if (s->m_GroupOf) |
| 292 |
{ |
| 293 |
s->removeFromGroup(false); |
| 294 |
} |
| 295 |
#endif |
| 296 |
|
| 297 |
// remove from listener's queue |
| 298 |
sockets_t::iterator ls = m_Sockets.find(s->m_ListenSocket); |
| 299 |
if (ls == m_Sockets.end()) |
| 300 |
{ |
| 301 |
ls = m_ClosedSockets.find(s->m_ListenSocket); |
| 302 |
} |
| 303 |
if (ls != m_ClosedSockets.end()) |
| 304 |
{ |
| 305 |
ls->second->m_QueuedSockets.erase(s->m_SocketID); |
| 306 |
} |
| 307 |
s->core().closeAtFork(); |
| 308 |
s->resetAtFork(); |
| 309 |
delete(s); |
| 310 |
} |
| 311 |
m_Sockets.clear(); |
| 312 |
|
| 313 |
#if ENABLE_BONDING |
| 314 |
for (groups_t::iterator j = m_Groups.begin(); j != m_Groups.end(); ++j) |
| 315 |
{ |
| 316 |
delete j->second; |
| 317 |
} |
| 318 |
m_Groups.clear(); |
| 319 |
#endif |
| 320 |
for (map<int, CMultiplexer>::iterator i = m_mMultiplexer.begin(); i != m_mMultiplexer.end(); ++i) |
| 321 |
{ |
| 322 |
CMultiplexer &multiplexer = i->second; |
| 323 |
multiplexer.resetAtFork(); |
| 324 |
} |
| 325 |
m_mMultiplexer.clear(); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
void srt::CUDTUnited::closeAllSockets() |
| 330 |
{ |
| 331 |
// remove all sockets and multiplexers |
| 332 |
HLOGC(inlog.Debug, log << "GC: GLOBAL EXIT - releasing all pending sockets. Acquiring control lock..."); |
| 333 |
|
| 334 |
{ |
| 335 |
// Pre-closing: run over all open sockets and close them. |
| 336 |
SharedLock glock(m_GlobControlLock); |
| 337 |
|
| 338 |
for (sockets_t::iterator i = m_Sockets.begin(); i != m_Sockets.end(); ++i) |
| 339 |
{ |
| 340 |
CUDTSocket* s = i->second; |
| 341 |
s->breakSocket_LOCKED(); |
| 342 |
|
| 343 |
#if ENABLE_BONDING |
| 344 |
if (s->m_GroupOf) |
| 345 |
{ |
| 346 |
HLOGC(smlog.Debug, |
| 347 |
log << "@" << s->m_SocketID << " IS MEMBER OF $" << s->m_GroupOf->id() |
| 348 |
<< " (IPE?) - REMOVING FROM GROUP"); |
| 349 |
s->removeFromGroup(false); |
| 350 |
} |
| 351 |
#endif |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
{ |
| 356 |
ExclusiveLock glock(m_GlobControlLock); |
| 357 |
|
| 358 |
for (sockets_t::iterator i = m_Sockets.begin(); i != m_Sockets.end(); ++i) |
| 359 |
{ |
| 360 |
CUDTSocket* s = i->second; |
| 361 |
|
| 362 |
m_ClosedSockets[i->first] = s; |
| 363 |
|
| 364 |
// remove from listener's queue |
| 365 |
sockets_t::iterator ls = m_Sockets.find(s->m_ListenSocket); |
| 366 |
if (ls == m_Sockets.end()) |
| 367 |
{ |
| 368 |
ls = m_ClosedSockets.find(s->m_ListenSocket); |
| 369 |
if (ls == m_ClosedSockets.end()) |
| 370 |
continue; |
| 371 |
} |
| 372 |
|
| 373 |
enterCS(ls->second->m_AcceptLock); |
| 374 |
ls->second->m_QueuedSockets.erase(s->m_SocketID); |
| 375 |
leaveCS(ls->second->m_AcceptLock); |
| 376 |
} |
| 377 |
m_Sockets.clear(); |
| 378 |
|
| 379 |
for (sockets_t::iterator j = m_ClosedSockets.begin(); j != m_ClosedSockets.end(); ++j) |
| 380 |
{ |
| 381 |
j->second->m_tsClosureTimeStamp = steady_clock::time_point(); |
| 382 |
} |
| 383 |
|
| 384 |
#if ENABLE_BONDING |
| 385 |
for (groups_t::iterator j = m_Groups.begin(); j != m_Groups.end(); ++j) |
| 386 |
{ |
| 387 |
SRTSOCKET id = j->second->m_GroupID; |
| 388 |
m_ClosedGroups[id] = j->second; |
| 389 |
} |
| 390 |
m_Groups.clear(); |
| 391 |
#endif |
| 392 |
} |
| 393 |
|
| 394 |
HLOGC(inlog.Debug, log << "GC: GLOBAL EXIT - releasing all CLOSED sockets."); |
| 395 |
while (true) |
| 396 |
{ |
| 397 |
checkBrokenSockets(); |
| 398 |
|
| 399 |
enterCS(m_GlobControlLock); |
| 400 |
bool empty = m_ClosedSockets.empty(); |
| 401 |
leaveCS(m_GlobControlLock); |
| 402 |
|
| 403 |
if (empty) |
| 404 |
break; |
| 405 |
|
| 406 |
HLOGC(inlog.Debug, log << "GC: checkBrokenSockets didn't wipe all sockets, repeating after 1s sleep"); |
| 407 |
srt::sync::this_thread::sleep_for(milliseconds_from(1)); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
int srt::CUDTUnited::startup() |
| 415 |
{ |
| 416 |
ScopedLock gcinit(m_InitLock); |
| 417 |
m_iInstanceCount++; |
| 418 |
if (m_bGCStatus) |
| 419 |
return (m_iInstanceCount == 1) ? 1 : 0; |
| 420 |
else |
| 421 |
return startGarbageCollector() ? 0 : -1; |
| 422 |
} |
| 423 |
|
| 424 |
int srt::CUDTUnited::cleanupAtFork() |
| 425 |
{ |
| 426 |
cleanupAllSockets(); |
| 427 |
resetThread(&m_GCThread); |
| 428 |
resetCond(m_GCStopCond); |
| 429 |
m_GCStopLock.unlock(); |
| 430 |
setupCond(m_GCStopCond, "GCStop"); |
| 431 |
m_iInstanceCount=0; |
| 432 |
m_bGCStatus = false; |
| 433 |
return 0; |
| 434 |
} |
| 435 |
|
| 436 |
int srt::CUDTUnited::cleanup() |
| 437 |
{ |
| 438 |
// IMPORTANT!!! |
| 439 |
// In this function there must be NO LOGGING AT ALL. This function may |
| 440 |
// potentially be called from within the global program destructor, and |
| 441 |
// therefore some of the facilities used by the logging system - including |
| 442 |
// the default std::cerr object bound to it by default, but also a different |
| 443 |
// stream that the user's app has bound to it, and which got destroyed |
| 444 |
// together with already exited main() - may be already deleted when |
| 445 |
// executing this procedure. |
| 446 |
ScopedLock gcinit(m_InitLock); |
| 447 |
|
| 448 |
if (--m_iInstanceCount > 0) |
| 449 |
return 0; |
| 450 |
|
| 451 |
stopGarbageCollector(); |
| 452 |
closeAllSockets(); |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
SRTSOCKET srt::CUDTUnited::generateSocketID(bool for_group) |
| 457 |
{ |
| 458 |
ScopedLock guard(m_IDLock); |
| 459 |
|
| 460 |
int sockval = m_SocketIDGenerator - 1; |
| 461 |
|
| 462 |
// First problem: zero-value should be avoided by various reasons. |
| 463 |
|
| 464 |
if (sockval <= 0) |
| 465 |
{ |
| 466 |
// We have a rollover on the socket value, so |
| 467 |
// definitely we haven't made the Columbus mistake yet. |
| 468 |
m_SocketIDGenerator = MAX_SOCKET_VAL; |
| 469 |
sockval = MAX_SOCKET_VAL; |
| 470 |
} |
| 471 |
|
| 472 |
// Check all sockets if any of them has this value. |
| 473 |
// Socket IDs are begin created this way: |
| 474 |
// |
| 475 |
// Initial random |
| 476 |
// | |
| 477 |
// | |
| 478 |
// | |
| 479 |
// | |
| 480 |
// ... |
| 481 |
// The only problem might be if the number rolls over |
| 482 |
// and reaches the same value from the opposite side. |
| 483 |
// This is still a valid socket value, but this time |
| 484 |
// we have to check, which sockets have been used already. |
| 485 |
if (sockval == m_SocketIDGenerator_init) |
| 486 |
{ |
| 487 |
// Mark that since this point on the checks for |
| 488 |
// whether the socket ID is in use must be done. |
| 489 |
m_SocketIDGenerator_init = 0; |
| 490 |
} |
| 491 |
|
| 492 |
// This is when all socket numbers have been already used once. |
| 493 |
// This may happen after many years of running an application |
| 494 |
// constantly when the connection breaks and gets restored often. |
| 495 |
if (m_SocketIDGenerator_init == 0) |
| 496 |
{ |
| 497 |
int startval = sockval; |
| 498 |
for (;;) // Roll until an unused value is found |
| 499 |
{ |
| 500 |
enterCS(m_GlobControlLock); |
| 501 |
const bool exists = |
| 502 |
#if ENABLE_BONDING |
| 503 |
for_group |
| 504 |
? m_Groups.count(sockval | SRTGROUP_MASK) |
| 505 |
: |
| 506 |
#endif |
| 507 |
m_Sockets.count(sockval); |
| 508 |
leaveCS(m_GlobControlLock); |
| 509 |
|
| 510 |
if (exists) |
| 511 |
{ |
| 512 |
// The socket value is in use. |
| 513 |
--sockval; |
| 514 |
if (sockval <= 0) |
| 515 |
sockval = MAX_SOCKET_VAL; |
| 516 |
|
| 517 |
// Before continuing, check if we haven't rolled back to start again |
| 518 |
// This is virtually impossible, so just make an RTI error. |
| 519 |
if (sockval == startval) |
| 520 |
{ |
| 521 |
// Of course, we don't lack memory, but actually this is so impossible |
| 522 |
// that a complete memory extinction is much more possible than this. |
| 523 |
// So treat this rather as a formal fallback for something that "should |
| 524 |
// never happen". This should make the socket creation functions, from |
| 525 |
// socket_create and accept, return this error. |
| 526 |
|
| 527 |
m_SocketIDGenerator = sockval + 1; // so that any next call will cause the same error |
| 528 |
throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 529 |
} |
| 530 |
|
| 531 |
// try again, if this is a free socket |
| 532 |
continue; |
| 533 |
} |
| 534 |
|
| 535 |
// No socket found, this ID is free to use |
| 536 |
m_SocketIDGenerator = sockval; |
| 537 |
break; |
| 538 |
} |
| 539 |
} |
| 540 |
else |
| 541 |
{ |
| 542 |
m_SocketIDGenerator = sockval; |
| 543 |
} |
| 544 |
|
| 545 |
// The socket value counter remains with the value rolled |
| 546 |
// without the group bit set; only the returned value may have |
| 547 |
// the group bit set. |
| 548 |
|
| 549 |
if (for_group) |
| 550 |
sockval = m_SocketIDGenerator | SRTGROUP_MASK; |
| 551 |
else |
| 552 |
sockval = m_SocketIDGenerator; |
| 553 |
|
| 554 |
LOGC(smlog.Debug, log << "generateSocketID: " << (for_group ? "(group)" : "") << ": @" << sockval); |
| 555 |
|
| 556 |
return sockval; |
| 557 |
} |
| 558 |
|
| 559 |
SRTSOCKET srt::CUDTUnited::newSocket(CUDTSocket** pps) |
| 560 |
{ |
| 561 |
// XXX consider using some replacement of std::unique_ptr |
| 562 |
// so that exceptions will clean up the object without the |
| 563 |
// need for a dedicated code. |
| 564 |
CUDTSocket* ns = NULL; |
| 565 |
|
| 566 |
try |
| 567 |
{ |
| 568 |
ns = new CUDTSocket; |
| 569 |
} |
| 570 |
catch (...) |
| 571 |
{ |
| 572 |
delete ns; |
| 573 |
throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 574 |
} |
| 575 |
|
| 576 |
try |
| 577 |
{ |
| 578 |
ns->m_SocketID = generateSocketID(); |
| 579 |
} |
| 580 |
catch (...) |
| 581 |
{ |
| 582 |
delete ns; |
| 583 |
throw; |
| 584 |
} |
| 585 |
ns->m_Status = SRTS_INIT; |
| 586 |
ns->m_ListenSocket = 0; |
| 587 |
ns->core().m_SocketID = ns->m_SocketID; |
| 588 |
ns->core().m_pCache = m_pCache; |
| 589 |
|
| 590 |
try |
| 591 |
{ |
| 592 |
HLOGC(smlog.Debug, log << CONID(ns->m_SocketID) << "newSocket: mapping socket " << ns->m_SocketID); |
| 593 |
|
| 594 |
// protect the m_Sockets structure. |
| 595 |
ExclusiveLock cs(m_GlobControlLock); |
| 596 |
m_Sockets[ns->m_SocketID] = ns; |
| 597 |
} |
| 598 |
catch (...) |
| 599 |
{ |
| 600 |
// failure and rollback |
| 601 |
delete ns; |
| 602 |
ns = NULL; |
| 603 |
throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 604 |
} |
| 605 |
|
| 606 |
startGarbageCollector(); |
| 607 |
if (pps) |
| 608 |
*pps = ns; |
| 609 |
|
| 610 |
return ns->m_SocketID; |
| 611 |
} |
| 612 |
|
| 613 |
// XXX NOTE: TSan reports here false positive against the call |
| 614 |
// to CRcvQueue::removeListener. This here will apply shared |
| 615 |
// lock on m_GlobControlLock in the call of locateSocket, while |
| 616 |
// having applied a shared lock on CRcvQueue::m_pListener in |
| 617 |
// CRcvQueue::worker_ProcessConnectionRequest. As this thread |
| 618 |
// locks both mutexes as shared, it doesn't form a deadlock. |
| 619 |
int srt::CUDTUnited::newConnection(const SRTSOCKET listen, |
| 620 |
const sockaddr_any& peer, |
| 621 |
const CPacket& hspkt, |
| 622 |
CHandShake& w_hs, |
| 623 |
int& w_error, |
| 624 |
CUDT*& w_acpu) |
| 625 |
{ |
| 626 |
CUDTSocket* ns = NULL; |
| 627 |
w_acpu = NULL; |
| 628 |
|
| 629 |
w_error = SRT_REJ_IPE; |
| 630 |
|
| 631 |
// Can't manage this error through an exception because this is |
| 632 |
// running in the listener loop. |
| 633 |
CUDTSocket* ls = locateSocket(listen); |
| 634 |
if (!ls) |
| 635 |
{ |
| 636 |
LOGC(cnlog.Error, log << "IPE: newConnection by listener socket id=" << listen << " which DOES NOT EXIST."); |
| 637 |
return -1; |
| 638 |
} |
| 639 |
|
| 640 |
HLOGC(cnlog.Debug, |
| 641 |
log << "newConnection: creating new socket after listener @" << listen |
| 642 |
<< " contacted with backlog=" << ls->m_uiBackLog); |
| 643 |
|
| 644 |
// if this connection has already been processed |
| 645 |
if ((ns = locatePeer(peer, w_hs.m_iID, w_hs.m_iISN)) != NULL) |
| 646 |
{ |
| 647 |
if (ns->core().m_bBroken) |
| 648 |
{ |
| 649 |
// last connection from the "peer" address has been broken |
| 650 |
ns->setClosed(); |
| 651 |
|
| 652 |
ScopedLock acceptcg(ls->m_AcceptLock); |
| 653 |
ls->m_QueuedSockets.erase(ns->m_SocketID); |
| 654 |
} |
| 655 |
else |
| 656 |
{ |
| 657 |
// connection already exist, this is a repeated connection request |
| 658 |
// respond with existing HS information |
| 659 |
HLOGC(cnlog.Debug, log << "newConnection: located a WORKING peer @" << w_hs.m_iID << " - ADAPTING."); |
| 660 |
|
| 661 |
w_hs.m_iISN = ns->core().m_iISN; |
| 662 |
w_hs.m_iMSS = ns->core().MSS(); |
| 663 |
w_hs.m_iFlightFlagSize = ns->core().m_config.iFlightFlagSize; |
| 664 |
w_hs.m_iReqType = URQ_CONCLUSION; |
| 665 |
w_hs.m_iID = ns->m_SocketID; |
| 666 |
|
| 667 |
// Report the original UDT because it will be |
| 668 |
// required to complete the HS data for conclusion response. |
| 669 |
w_acpu = &ns->core(); |
| 670 |
|
| 671 |
return 0; |
| 672 |
|
| 673 |
// except for this situation a new connection should be started |
| 674 |
} |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
HLOGC(cnlog.Debug, |
| 679 |
log << "newConnection: NOT located any peer @" << w_hs.m_iID << " - resuming with initial connection."); |
| 680 |
} |
| 681 |
|
| 682 |
// exceeding backlog, refuse the connection request |
| 683 |
|
| 684 |
enterCS(ls->m_AcceptLock); |
| 685 |
size_t backlog = ls->m_QueuedSockets.size(); |
| 686 |
leaveCS(ls->m_AcceptLock); |
| 687 |
if (backlog >= ls->m_uiBackLog) |
| 688 |
{ |
| 689 |
w_error = SRT_REJ_BACKLOG; |
| 690 |
LOGC(cnlog.Note, log << "newConnection: listen backlog=" << ls->m_uiBackLog << " EXCEEDED"); |
| 691 |
return -1; |
| 692 |
} |
| 693 |
|
| 694 |
try |
| 695 |
{ |
| 696 |
// Protect the config of the listener socket from a data race. |
| 697 |
ScopedLock lck(ls->core().m_ConnectionLock); |
| 698 |
ns = new CUDTSocket(*ls); |
| 699 |
// No need to check the peer, this is the address from which the request has come. |
| 700 |
ns->m_PeerAddr = peer; |
| 701 |
} |
| 702 |
catch (...) |
| 703 |
{ |
| 704 |
w_error = SRT_REJ_RESOURCE; |
| 705 |
delete ns; |
| 706 |
LOGC(cnlog.Error, log << "IPE: newConnection: unexpected exception (probably std::bad_alloc)"); |
| 707 |
return -1; |
| 708 |
} |
| 709 |
|
| 710 |
ns->core().m_RejectReason = SRT_REJ_UNKNOWN; // pre-set a universal value |
| 711 |
|
| 712 |
try |
| 713 |
{ |
| 714 |
ns->m_SocketID = generateSocketID(); |
| 715 |
} |
| 716 |
catch (const CUDTException&) |
| 717 |
{ |
| 718 |
LOGC(cnlog.Fatal, log << "newConnection: IPE: all sockets occupied? Last gen=" << m_SocketIDGenerator); |
| 719 |
// generateSocketID throws exception, which can be naturally handled |
| 720 |
// when the call is derived from the API call, but here it's called |
| 721 |
// internally in response to receiving a handshake. It must be handled |
| 722 |
// here and turned into an erroneous return value. |
| 723 |
delete ns; |
| 724 |
return -1; |
| 725 |
} |
| 726 |
|
| 727 |
ns->m_ListenSocket = listen; |
| 728 |
ns->core().m_SocketID = ns->m_SocketID; |
| 729 |
ns->m_PeerID = w_hs.m_iID; |
| 730 |
ns->m_iISN = w_hs.m_iISN; |
| 731 |
|
| 732 |
HLOGC(cnlog.Debug, |
| 733 |
log << "newConnection: DATA: lsnid=" << listen << " id=" << ns->core().m_SocketID |
| 734 |
<< " peerid=" << ns->core().m_PeerID << " ISN=" << ns->m_iISN); |
| 735 |
|
| 736 |
int error = 0; |
| 737 |
bool should_submit_to_accept = true; |
| 738 |
|
| 739 |
// Set the error code for all prospective problems below. |
| 740 |
// It won't be interpreted when result was successful. |
| 741 |
w_error = SRT_REJ_RESOURCE; |
| 742 |
|
| 743 |
// These can throw exception only when the memory allocation failed. |
| 744 |
// CUDT::connect() translates exception into CUDTException. |
| 745 |
// CUDT::open() may only throw original std::bad_alloc from new. |
| 746 |
// This is only to make the library extra safe (when your machine lacks |
| 747 |
// memory, it will continue to work, but fail to accept connection). |
| 748 |
|
| 749 |
try |
| 750 |
{ |
| 751 |
// This assignment must happen b4 the call to CUDT::connect() because |
| 752 |
// this call causes sending the SRT Handshake through this socket. |
| 753 |
// Without this mapping the socket cannot be found and therefore |
| 754 |
// the SRT Handshake message would fail. |
| 755 |
HLOGC(cnlog.Debug, log << |
| 756 |
"newConnection: incoming " << peer.str() << ", mapping socket " << ns->m_SocketID); |
| 757 |
{ |
| 758 |
ExclusiveLock cg(m_GlobControlLock); |
| 759 |
m_Sockets[ns->m_SocketID] = ns; |
| 760 |
} |
| 761 |
|
| 762 |
if (ls->core().m_cbAcceptHook) |
| 763 |
{ |
| 764 |
if (!ls->core().runAcceptHook(&ns->core(), peer.get(), w_hs, hspkt)) |
| 765 |
{ |
| 766 |
w_error = ns->core().m_RejectReason; |
| 767 |
|
| 768 |
error = 1; |
| 769 |
goto ERR_ROLLBACK; |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
// bind to the same addr of listening socket |
| 774 |
ns->core().open(); |
| 775 |
if (!updateListenerMux(ns, ls)) |
| 776 |
{ |
| 777 |
// This is highly unlikely if not impossible, but there's |
| 778 |
// a theoretical runtime chance of failure so it should be |
| 779 |
// handled |
| 780 |
ns->core().m_RejectReason = SRT_REJ_IPE; |
| 781 |
throw false; // let it jump directly into the omni exception handler |
| 782 |
} |
| 783 |
|
| 784 |
ns->core().acceptAndRespond(ls->m_SelfAddr, peer, hspkt, (w_hs)); |
| 785 |
} |
| 786 |
catch (...) |
| 787 |
{ |
| 788 |
// Extract the error that was set in this new failed entity. |
| 789 |
w_error = ns->core().m_RejectReason; |
| 790 |
error = 1; |
| 791 |
goto ERR_ROLLBACK; |
| 792 |
} |
| 793 |
|
| 794 |
ns->m_Status = SRTS_CONNECTED; |
| 795 |
|
| 796 |
// copy address information of local node |
| 797 |
// Precisely, what happens here is: |
| 798 |
// - Get the IP address and port from the system database |
| 799 |
ns->core().m_pSndQueue->m_pChannel->getSockAddr((ns->m_SelfAddr)); |
| 800 |
// - OVERWRITE just the IP address itself by a value taken from piSelfIP |
| 801 |
// (the family is used exactly as the one taken from what has been returned |
| 802 |
// by getsockaddr) |
| 803 |
CIPAddress::pton((ns->m_SelfAddr), ns->core().m_piSelfIP, peer); |
| 804 |
|
| 805 |
{ |
| 806 |
// protect the m_PeerRec structure (and group existence) |
| 807 |
ExclusiveLock glock(m_GlobControlLock); |
| 808 |
try |
| 809 |
{ |
| 810 |
HLOGC(cnlog.Debug, log << "newConnection: mapping peer " << ns->m_PeerID |
| 811 |
<< " to that socket (" << ns->m_SocketID << ")"); |
| 812 |
m_PeerRec[ns->getPeerSpec()].insert(ns->m_SocketID); |
| 813 |
|
| 814 |
LOGC(cnlog.Note, log << "@" << ns->m_SocketID << " connection on listener @" << listen |
| 815 |
<< " (" << ns->m_SelfAddr.str() << ") from peer @" << ns->m_PeerID << " (" << peer.str() << ")"); |
| 816 |
} |
| 817 |
catch (...) |
| 818 |
{ |
| 819 |
LOGC(cnlog.Error, log << "newConnection: error when mapping peer!"); |
| 820 |
error = 2; |
| 821 |
} |
| 822 |
|
| 823 |
// The access to m_GroupOf should be also protected, as the group |
| 824 |
// could be requested deletion in the meantime. This will hold any possible |
| 825 |
// removal from group and resetting m_GroupOf field. |
| 826 |
|
| 827 |
#if ENABLE_BONDING |
| 828 |
if (ns->m_GroupOf) |
| 829 |
{ |
| 830 |
// XXX this might require another check of group type. |
| 831 |
// For redundancy group, at least, update the status in the group |
| 832 |
CUDTGroup* g = ns->m_GroupOf; |
| 833 |
ScopedLock grlock(g->m_GroupLock); |
| 834 |
if (g->m_bClosing) |
| 835 |
{ |
| 836 |
error = 1; // "INTERNAL REJECTION" |
| 837 |
goto ERR_ROLLBACK; |
| 838 |
} |
| 839 |
|
| 840 |
// Check if this is the first socket in the group. |
| 841 |
// If so, give it up to accept, otherwise just do nothing |
| 842 |
// The client will be informed about the newly added connection at the |
| 843 |
// first moment when attempting to get the group status. |
| 844 |
for (CUDTGroup::gli_t gi = g->m_Group.begin(); gi != g->m_Group.end(); ++gi) |
| 845 |
{ |
| 846 |
if (gi->laststatus == SRTS_CONNECTED) |
| 847 |
{ |
| 848 |
HLOGC(cnlog.Debug, |
| 849 |
log << "Found another connected socket in the group: $" << gi->id |
| 850 |
<< " - socket will be NOT given up for accepting"); |
| 851 |
should_submit_to_accept = false; |
| 852 |
break; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
// Update the status in the group so that the next |
| 857 |
// operation can include the socket in the group operation. |
| 858 |
CUDTGroup::SocketData* gm = ns->m_GroupMemberData; |
| 859 |
|
| 860 |
HLOGC(cnlog.Debug, |
| 861 |
log << "newConnection(GROUP): Socket @" << ns->m_SocketID << " BELONGS TO $" << g->id() << " - will " |
| 862 |
<< (should_submit_to_accept ? "" : "NOT ") << "report in accept"); |
| 863 |
gm->sndstate = SRT_GST_IDLE; |
| 864 |
gm->rcvstate = SRT_GST_IDLE; |
| 865 |
gm->laststatus = SRTS_CONNECTED; |
| 866 |
|
| 867 |
if (!g->m_bConnected) |
| 868 |
{ |
| 869 |
HLOGC(cnlog.Debug, log << "newConnection(GROUP): First socket connected, SETTING GROUP CONNECTED"); |
| 870 |
g->m_bConnected = true; |
| 871 |
} |
| 872 |
|
| 873 |
// XXX PROBLEM!!! These events are subscribed here so that this is done once, lazily, |
| 874 |
// but groupwise connections could be accepted from multiple listeners for the same group! |
| 875 |
// m_listener MUST BE A CONTAINER, NOT POINTER!!! |
| 876 |
// ALSO: Maybe checking "the same listener" is not necessary as subscription may be done |
| 877 |
// multiple times anyway? |
| 878 |
if (!g->m_listener) |
| 879 |
{ |
| 880 |
// Newly created group from the listener, which hasn't yet |
| 881 |
// the listener set. |
| 882 |
g->m_listener = ls; |
| 883 |
|
| 884 |
// Listen on both first connected socket and continued sockets. |
| 885 |
// This might help with jump-over situations, and in regular continued |
| 886 |
// sockets the IN event won't be reported anyway. |
| 887 |
int listener_modes = SRT_EPOLL_ACCEPT | SRT_EPOLL_UPDATE; |
| 888 |
epoll_add_usock_INTERNAL(g->m_RcvEID, ls, &listener_modes); |
| 889 |
|
| 890 |
// This listening should be done always when a first connected socket |
| 891 |
// appears as accepted off the listener. This is for the sake of swait() calls |
| 892 |
// inside the group receiving and sending functions so that they get |
| 893 |
// interrupted when a new socket is connected. |
| 894 |
} |
| 895 |
|
| 896 |
// Add also per-direction subscription for the about-to-be-accepted socket. |
| 897 |
// Both first accepted socket that makes the group-accept and every next |
| 898 |
// socket that adds a new link. |
| 899 |
int read_modes = SRT_EPOLL_IN | SRT_EPOLL_ERR; |
| 900 |
int write_modes = SRT_EPOLL_OUT | SRT_EPOLL_ERR; |
| 901 |
epoll_add_usock_INTERNAL(g->m_RcvEID, ns, &read_modes); |
| 902 |
epoll_add_usock_INTERNAL(g->m_SndEID, ns, &write_modes); |
| 903 |
|
| 904 |
// With app reader, do not set groupPacketArrival (block the |
| 905 |
// provider array feature completely for now). |
| 906 |
|
| 907 |
/* SETUP HERE IF NEEDED |
| 908 |
ns->core().m_cbPacketArrival.set(ns->m_pUDT, &CUDT::groupPacketArrival); |
| 909 |
*/ |
| 910 |
} |
| 911 |
else |
| 912 |
{ |
| 913 |
HLOGC(cnlog.Debug, log << "newConnection: Socket @" << ns->m_SocketID << " is not in a group"); |
| 914 |
} |
| 915 |
#endif |
| 916 |
} |
| 917 |
|
| 918 |
if (should_submit_to_accept) |
| 919 |
{ |
| 920 |
enterCS(ls->m_AcceptLock); |
| 921 |
try |
| 922 |
{ |
| 923 |
ls->m_QueuedSockets[ns->m_SocketID] = ns->m_PeerAddr; |
| 924 |
} |
| 925 |
catch (...) |
| 926 |
{ |
| 927 |
LOGC(cnlog.Error, log << "newConnection: error when queuing socket!"); |
| 928 |
error = 3; |
| 929 |
} |
| 930 |
leaveCS(ls->m_AcceptLock); |
| 931 |
|
| 932 |
HLOGC(cnlog.Debug, log << "ACCEPT: new socket @" << ns->m_SocketID << " submitted for acceptance"); |
| 933 |
// acknowledge users waiting for new connections on the listening socket |
| 934 |
m_EPoll.update_events(listen, ls->core().m_sPollID, SRT_EPOLL_ACCEPT, true); |
| 935 |
|
| 936 |
CGlobEvent::triggerEvent(); |
| 937 |
|
| 938 |
// XXX the exact value of 'error' is ignored |
| 939 |
if (error > 0) |
| 940 |
{ |
| 941 |
goto ERR_ROLLBACK; |
| 942 |
} |
| 943 |
|
| 944 |
// wake up a waiting accept() call |
| 945 |
CSync::lock_notify_one(ls->m_AcceptCond, ls->m_AcceptLock); |
| 946 |
} |
| 947 |
else |
| 948 |
{ |
| 949 |
HLOGC(cnlog.Debug, |
| 950 |
log << "ACCEPT: new socket @" << ns->m_SocketID |
| 951 |
<< " NOT submitted to acceptance, another socket in the group is already connected"); |
| 952 |
|
| 953 |
// acknowledge INTERNAL users waiting for new connections on the listening socket |
| 954 |
// that are reported when a new socket is connected within an already connected group. |
| 955 |
m_EPoll.update_events(listen, ls->core().m_sPollID, SRT_EPOLL_UPDATE, true); |
| 956 |
CGlobEvent::triggerEvent(); |
| 957 |
} |
| 958 |
|
| 959 |
ERR_ROLLBACK: |
| 960 |
// XXX the exact value of 'error' is ignored |
| 961 |
if (error > 0) |
| 962 |
{ |
| 963 |
#if ENABLE_LOGGING |
| 964 |
static const char* why[] = { |
| 965 |
"UNKNOWN ERROR", "INTERNAL REJECTION", "IPE when mapping a socket", "IPE when inserting a socket"}; |
| 966 |
LOGC(cnlog.Warn, |
| 967 |
log << CONID(ns->m_SocketID) << "newConnection: connection rejected due to: " << why[error] << " - " |
| 968 |
<< RequestTypeStr(URQFailure(w_error))); |
| 969 |
#endif |
| 970 |
|
| 971 |
SRTSOCKET id = ns->m_SocketID; |
| 972 |
ns->core().closeInternal(); |
| 973 |
ns->setClosed(); |
| 974 |
|
| 975 |
// The mapped socket should be now unmapped to preserve the situation that |
| 976 |
// was in the original UDT code. |
| 977 |
// In SRT additionally the acceptAndRespond() function (it was called probably |
| 978 |
// connect() in UDT code) may fail, in which case this socket should not be |
| 979 |
// further processed and should be removed. |
| 980 |
{ |
| 981 |
ExclusiveLock cg(m_GlobControlLock); |
| 982 |
|
| 983 |
#if ENABLE_BONDING |
| 984 |
if (ns->m_GroupOf) |
| 985 |
{ |
| 986 |
HLOGC(smlog.Debug, |
| 987 |
log << "@" << ns->m_SocketID << " IS MEMBER OF $" << ns->m_GroupOf->id() |
| 988 |
<< " - REMOVING FROM GROUP"); |
| 989 |
ns->removeFromGroup(true); |
| 990 |
} |
| 991 |
#endif |
| 992 |
m_Sockets.erase(id); |
| 993 |
m_ClosedSockets[id] = ns; |
| 994 |
} |
| 995 |
|
| 996 |
return -1; |
| 997 |
} |
| 998 |
|
| 999 |
return 1; |
| 1000 |
} |
| 1001 |
|
| 1002 |
// static forwarder |
| 1003 |
int srt::CUDT::installAcceptHook(SRTSOCKET lsn, srt_listen_callback_fn* hook, void* opaq) |
| 1004 |
{ |
| 1005 |
return uglobal().installAcceptHook(lsn, hook, opaq); |
| 1006 |
} |
| 1007 |
|
| 1008 |
int srt::CUDTUnited::installAcceptHook(const SRTSOCKET lsn, srt_listen_callback_fn* hook, void* opaq) |
| 1009 |
{ |
| 1010 |
try |
| 1011 |
{ |
| 1012 |
CUDTSocket* s = locateSocket(lsn, ERH_THROW); |
| 1013 |
s->core().installAcceptHook(hook, opaq); |
| 1014 |
} |
| 1015 |
catch (CUDTException& e) |
| 1016 |
{ |
| 1017 |
SetThreadLocalError(e); |
| 1018 |
return SRT_ERROR; |
| 1019 |
} |
| 1020 |
|
| 1021 |
return 0; |
| 1022 |
} |
| 1023 |
|
| 1024 |
int srt::CUDT::installConnectHook(SRTSOCKET lsn, srt_connect_callback_fn* hook, void* opaq) |
| 1025 |
{ |
| 1026 |
return uglobal().installConnectHook(lsn, hook, opaq); |
| 1027 |
} |
| 1028 |
|
| 1029 |
int srt::CUDTUnited::installConnectHook(const SRTSOCKET u, srt_connect_callback_fn* hook, void* opaq) |
| 1030 |
{ |
| 1031 |
try |
| 1032 |
{ |
| 1033 |
#if ENABLE_BONDING |
| 1034 |
if (u & SRTGROUP_MASK) |
| 1035 |
{ |
| 1036 |
GroupKeeper k(*this, u, ERH_THROW); |
| 1037 |
k.group->installConnectHook(hook, opaq); |
| 1038 |
return 0; |
| 1039 |
} |
| 1040 |
#endif |
| 1041 |
CUDTSocket* s = locateSocket(u, ERH_THROW); |
| 1042 |
s->core().installConnectHook(hook, opaq); |
| 1043 |
} |
| 1044 |
catch (CUDTException& e) |
| 1045 |
{ |
| 1046 |
SetThreadLocalError(e); |
| 1047 |
return SRT_ERROR; |
| 1048 |
} |
| 1049 |
|
| 1050 |
return 0; |
| 1051 |
} |
| 1052 |
|
| 1053 |
SRT_SOCKSTATUS srt::CUDTUnited::getStatus(const SRTSOCKET u) |
| 1054 |
{ |
| 1055 |
// protects the m_Sockets structure |
| 1056 |
SharedLock cg(m_GlobControlLock); |
| 1057 |
|
| 1058 |
sockets_t::const_iterator i = m_Sockets.find(u); |
| 1059 |
|
| 1060 |
if (i == m_Sockets.end()) |
| 1061 |
{ |
| 1062 |
if (m_ClosedSockets.find(u) != m_ClosedSockets.end()) |
| 1063 |
return SRTS_CLOSED; |
| 1064 |
|
| 1065 |
return SRTS_NONEXIST; |
| 1066 |
} |
| 1067 |
return i->second->getStatus(); |
| 1068 |
} |
| 1069 |
|
| 1070 |
int srt::CUDTUnited::bind(CUDTSocket* s, const sockaddr_any& name) |
| 1071 |
{ |
| 1072 |
ScopedLock cg(s->m_ControlLock); |
| 1073 |
|
| 1074 |
// cannot bind a socket more than once |
| 1075 |
if (s->m_Status != SRTS_INIT) |
| 1076 |
throw CUDTException(MJ_NOTSUP, MN_NONE, 0); |
| 1077 |
|
| 1078 |
if (s->core().m_config.iIpV6Only == -1 && name.family() == AF_INET6 && name.isany()) |
| 1079 |
{ |
| 1080 |
// V6ONLY option must be set explicitly if you want to bind to a wildcard address in IPv6 |
| 1081 |
HLOGP(smlog.Error, |
| 1082 |
"bind: when binding to :: (IPv6 wildcard), SRTO_IPV6ONLY option must be set explicitly to 0 or 1"); |
| 1083 |
|
| 1084 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1085 |
} |
| 1086 |
|
| 1087 |
s->core().open(); |
| 1088 |
updateMux(s, name); |
| 1089 |
s->m_Status = SRTS_OPENED; |
| 1090 |
|
| 1091 |
// copy address information of local node |
| 1092 |
s->core().m_pSndQueue->m_pChannel->getSockAddr((s->m_SelfAddr)); |
| 1093 |
|
| 1094 |
return 0; |
| 1095 |
} |
| 1096 |
|
| 1097 |
int srt::CUDTUnited::bind(CUDTSocket* s, UDPSOCKET udpsock) |
| 1098 |
{ |
| 1099 |
ScopedLock cg(s->m_ControlLock); |
| 1100 |
|
| 1101 |
// cannot bind a socket more than once |
| 1102 |
if (s->m_Status != SRTS_INIT) |
| 1103 |
throw CUDTException(MJ_NOTSUP, MN_NONE, 0); |
| 1104 |
|
| 1105 |
sockaddr_any name; |
| 1106 |
socklen_t namelen = sizeof name; // max of inet and inet6 |
| 1107 |
|
| 1108 |
// This will preset the sa_family as well; the namelen is given simply large |
| 1109 |
// enough for any family here. |
| 1110 |
if (::getsockname(udpsock, &name.sa, &namelen) == -1) |
| 1111 |
throw CUDTException(MJ_NOTSUP, MN_INVAL); |
| 1112 |
|
| 1113 |
// Successfully extracted, so update the size |
| 1114 |
name.len = namelen; |
| 1115 |
|
| 1116 |
s->core().open(); |
| 1117 |
updateMux(s, name, &udpsock); |
| 1118 |
s->m_Status = SRTS_OPENED; |
| 1119 |
|
| 1120 |
// copy address information of local node |
| 1121 |
s->core().m_pSndQueue->m_pChannel->getSockAddr(s->m_SelfAddr); |
| 1122 |
|
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
int srt::CUDTUnited::listen(const SRTSOCKET u, int backlog) |
| 1127 |
{ |
| 1128 |
if (backlog <= 0) |
| 1129 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1130 |
|
| 1131 |
// Don't search for the socket if it's already -1; |
| 1132 |
// this never is a valid socket. |
| 1133 |
if (u == UDT::INVALID_SOCK) |
| 1134 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 1135 |
|
| 1136 |
CUDTSocket* s = locateSocket(u); |
| 1137 |
if (!s) |
| 1138 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 1139 |
|
| 1140 |
ScopedLock cg(s->m_ControlLock); |
| 1141 |
|
| 1142 |
// NOTE: since now the socket is protected against simultaneous access. |
| 1143 |
// In the meantime the socket might have been closed, which means that |
| 1144 |
// it could have changed the state. It could be also set listen in another |
| 1145 |
// thread, so check it out. |
| 1146 |
|
| 1147 |
if (s->core().m_config.bRendezvous) |
| 1148 |
throw CUDTException(MJ_NOTSUP, MN_ISRENDEZVOUS, 0); |
| 1149 |
|
| 1150 |
switch(s->m_Status) |
| 1151 |
{ |
| 1152 |
case SRTS_INIT: |
| 1153 |
throw CUDTException(MJ_NOTSUP, MN_ISUNBOUND, 0); |
| 1154 |
break; |
| 1155 |
case SRTS_OPENED: |
| 1156 |
s->m_uiBackLog = backlog; |
| 1157 |
s->core().setListenState(); // propagates CUDTException, |
| 1158 |
s->m_Status = SRTS_LISTENING; |
| 1159 |
break; |
| 1160 |
case SRTS_LISTENING: |
| 1161 |
s->m_uiBackLog = backlog; |
| 1162 |
break; |
| 1163 |
case SRTS_CONNECTING: |
| 1164 |
case SRTS_CONNECTED: |
| 1165 |
throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0); |
| 1166 |
break; |
| 1167 |
case SRTS_BROKEN: |
| 1168 |
case SRTS_CLOSING: |
| 1169 |
case SRTS_CLOSED: |
| 1170 |
case SRTS_NONEXIST: |
| 1171 |
throw CUDTException(MJ_SETUP, MN_CLOSED, 0); |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
return 0; |
| 1177 |
} |
| 1178 |
|
| 1179 |
SRTSOCKET srt::CUDTUnited::accept_bond(const SRTSOCKET listeners[], int lsize, int64_t msTimeOut) |
| 1180 |
{ |
| 1181 |
CEPollDesc* ed = 0; |
| 1182 |
int eid = m_EPoll.create(&ed); |
| 1183 |
|
| 1184 |
// Destroy it at return - this function can be interrupted |
| 1185 |
// by an exception. |
| 1186 |
struct AtReturn |
| 1187 |
{ |
| 1188 |
int eid; |
| 1189 |
CUDTUnited* that; |
| 1190 |
AtReturn(CUDTUnited* t, int e) |
| 1191 |
: eid(e) |
| 1192 |
, that(t) |
| 1193 |
{ |
| 1194 |
} |
| 1195 |
~AtReturn() { that->m_EPoll.release(eid); } |
| 1196 |
} l_ar(this, eid); |
| 1197 |
|
| 1198 |
// Subscribe all of listeners for accept |
| 1199 |
int events = SRT_EPOLL_ACCEPT; |
| 1200 |
|
| 1201 |
for (int i = 0; i < lsize; ++i) |
| 1202 |
{ |
| 1203 |
srt_epoll_add_usock(eid, listeners[i], &events); |
| 1204 |
} |
| 1205 |
|
| 1206 |
CEPoll::fmap_t st; |
| 1207 |
m_EPoll.swait(*ed, (st), msTimeOut, true); |
| 1208 |
|
| 1209 |
if (st.empty()) |
| 1210 |
{ |
| 1211 |
// Sanity check |
| 1212 |
throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0); |
| 1213 |
} |
| 1214 |
|
| 1215 |
// Theoretically we can have a situation that more than one |
| 1216 |
// listener is ready for accept. In this case simply get |
| 1217 |
// only the first found. |
| 1218 |
int lsn = st.begin()->first; |
| 1219 |
sockaddr_storage dummy; |
| 1220 |
int outlen = sizeof dummy; |
| 1221 |
return accept(lsn, ((sockaddr*)&dummy), (&outlen)); |
| 1222 |
} |
| 1223 |
|
| 1224 |
SRTSOCKET srt::CUDTUnited::accept(const SRTSOCKET listen, sockaddr* pw_addr, int* pw_addrlen) |
| 1225 |
{ |
| 1226 |
if (pw_addr && !pw_addrlen) |
| 1227 |
{ |
| 1228 |
LOGC(cnlog.Error, log << "srt_accept: provided address, but address length parameter is missing"); |
| 1229 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1230 |
} |
| 1231 |
|
| 1232 |
CUDTSocket* ls = locateSocket(listen); |
| 1233 |
|
| 1234 |
if (ls == NULL) |
| 1235 |
{ |
| 1236 |
LOGC(cnlog.Error, log << "srt_accept: invalid listener socket ID value: " << listen); |
| 1237 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 1238 |
} |
| 1239 |
|
| 1240 |
// the "listen" socket must be in LISTENING status |
| 1241 |
if (ls->m_Status != SRTS_LISTENING) |
| 1242 |
{ |
| 1243 |
LOGC(cnlog.Error, log << "srt_accept: socket @" << listen << " is not in listening state (forgot srt_listen?)"); |
| 1244 |
throw CUDTException(MJ_NOTSUP, MN_NOLISTEN, 0); |
| 1245 |
} |
| 1246 |
|
| 1247 |
// no "accept" in rendezvous connection setup |
| 1248 |
if (ls->core().m_config.bRendezvous) |
| 1249 |
{ |
| 1250 |
LOGC(cnlog.Fatal, |
| 1251 |
log << "CUDTUnited::accept: RENDEZVOUS flag passed through check in srt_listen when it set listen state"); |
| 1252 |
// This problem should never happen because `srt_listen` function should have |
| 1253 |
// checked this situation before and not set listen state in result. |
| 1254 |
// Inform the user about the invalid state in the universal way. |
| 1255 |
throw CUDTException(MJ_NOTSUP, MN_NOLISTEN, 0); |
| 1256 |
} |
| 1257 |
|
| 1258 |
SRTSOCKET u = CUDT::INVALID_SOCK; |
| 1259 |
bool accepted = false; |
| 1260 |
|
| 1261 |
// !!only one connection can be set up each time!! |
| 1262 |
while (!accepted) |
| 1263 |
{ |
| 1264 |
UniqueLock accept_lock(ls->m_AcceptLock); |
| 1265 |
CSync accept_sync(ls->m_AcceptCond, accept_lock); |
| 1266 |
|
| 1267 |
if ((ls->m_Status != SRTS_LISTENING) || ls->core().m_bBroken) |
| 1268 |
{ |
| 1269 |
// This socket has been closed. |
| 1270 |
accepted = true; |
| 1271 |
} |
| 1272 |
else if (ls->m_QueuedSockets.size() > 0) |
| 1273 |
{ |
| 1274 |
map<SRTSOCKET, sockaddr_any>::iterator b = ls->m_QueuedSockets.begin(); |
| 1275 |
|
| 1276 |
if (pw_addr != NULL && pw_addrlen != NULL) |
| 1277 |
{ |
| 1278 |
// Check if the length of the buffer to fill the name in |
| 1279 |
// was large enough. |
| 1280 |
const int len = b->second.size(); |
| 1281 |
if (*pw_addrlen < len) |
| 1282 |
{ |
| 1283 |
// In case when the address cannot be rewritten, |
| 1284 |
// DO NOT accept, but leave the socket in the queue. |
| 1285 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
u = b->first; |
| 1290 |
ls->m_QueuedSockets.erase(b); |
| 1291 |
accepted = true; |
| 1292 |
} |
| 1293 |
else if (!ls->core().m_config.bSynRecving) |
| 1294 |
{ |
| 1295 |
accepted = true; |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (!accepted && (ls->m_Status == SRTS_LISTENING)) |
| 1299 |
accept_sync.wait(); |
| 1300 |
|
| 1301 |
if (ls->m_QueuedSockets.empty()) |
| 1302 |
m_EPoll.update_events(listen, ls->core().m_sPollID, SRT_EPOLL_ACCEPT, false); |
| 1303 |
} |
| 1304 |
|
| 1305 |
if (u == CUDT::INVALID_SOCK) |
| 1306 |
{ |
| 1307 |
// non-blocking receiving, no connection available |
| 1308 |
if (!ls->core().m_config.bSynRecving) |
| 1309 |
{ |
| 1310 |
LOGC(cnlog.Error, log << "srt_accept: no pending connection available at the moment"); |
| 1311 |
throw CUDTException(MJ_AGAIN, MN_RDAVAIL, 0); |
| 1312 |
} |
| 1313 |
|
| 1314 |
LOGC(cnlog.Error, log << "srt_accept: listener socket @" << listen << " is already closed"); |
| 1315 |
// listening socket is closed |
| 1316 |
throw CUDTException(MJ_SETUP, MN_CLOSED, 0); |
| 1317 |
} |
| 1318 |
|
| 1319 |
CUDTSocket* s = locateSocket(u); |
| 1320 |
if (s == NULL) |
| 1321 |
{ |
| 1322 |
LOGC(cnlog.Error, log << "srt_accept: pending connection has unexpectedly closed"); |
| 1323 |
throw CUDTException(MJ_SETUP, MN_CLOSED, 0); |
| 1324 |
} |
| 1325 |
|
| 1326 |
// Set properly the SRTO_GROUPCONNECT flag |
| 1327 |
s->core().m_config.iGroupConnect = 0; |
| 1328 |
|
| 1329 |
// Check if LISTENER has the SRTO_GROUPCONNECT flag set, |
| 1330 |
// and the already accepted socket has successfully joined |
| 1331 |
// the mirror group. If so, RETURN THE GROUP ID, not the socket ID. |
| 1332 |
#if ENABLE_BONDING |
| 1333 |
if (ls->core().m_config.iGroupConnect == 1 && s->m_GroupOf) |
| 1334 |
{ |
| 1335 |
// Put a lock to protect the group against accidental deletion |
| 1336 |
// in the meantime. |
| 1337 |
SharedLock glock(m_GlobControlLock); |
| 1338 |
// Check again; it's unlikely to happen, but |
| 1339 |
// it's a theoretically possible scenario |
| 1340 |
if (s->m_GroupOf) |
| 1341 |
{ |
| 1342 |
u = s->m_GroupOf->m_GroupID; |
| 1343 |
s->core().m_config.iGroupConnect = 1; // should be derived from ls, but make sure |
| 1344 |
|
| 1345 |
// Mark the beginning of the connection at the moment |
| 1346 |
// when the group ID is returned to the app caller |
| 1347 |
s->m_GroupOf->m_stats.tsLastSampleTime = steady_clock::now(); |
| 1348 |
} |
| 1349 |
else |
| 1350 |
{ |
| 1351 |
LOGC(smlog.Error, log << "accept: IPE: socket's group deleted in the meantime of accept process???"); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
#endif |
| 1355 |
|
| 1356 |
ScopedLock cg(s->m_ControlLock); |
| 1357 |
|
| 1358 |
if (pw_addr != NULL && pw_addrlen != NULL) |
| 1359 |
{ |
| 1360 |
memcpy((pw_addr), s->m_PeerAddr.get(), s->m_PeerAddr.size()); |
| 1361 |
*pw_addrlen = s->m_PeerAddr.size(); |
| 1362 |
} |
| 1363 |
|
| 1364 |
return u; |
| 1365 |
} |
| 1366 |
|
| 1367 |
int srt::CUDTUnited::connect(SRTSOCKET u, const sockaddr* srcname, const sockaddr* tarname, int namelen) |
| 1368 |
{ |
| 1369 |
// Here both srcname and tarname must be specified |
| 1370 |
if (!srcname || !tarname || namelen < int(sizeof(sockaddr_in))) |
| 1371 |
{ |
| 1372 |
LOGC(aclog.Error, |
| 1373 |
log << "connect(with source): invalid call: srcname=" << srcname << " tarname=" << tarname |
| 1374 |
<< " namelen=" << namelen); |
| 1375 |
throw CUDTException(MJ_NOTSUP, MN_INVAL); |
| 1376 |
} |
| 1377 |
|
| 1378 |
sockaddr_any source_addr(srcname, namelen); |
| 1379 |
if (source_addr.len == 0) |
| 1380 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1381 |
sockaddr_any target_addr(tarname, namelen); |
| 1382 |
if (target_addr.len == 0) |
| 1383 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1384 |
|
| 1385 |
#if ENABLE_BONDING |
| 1386 |
// Check affiliation of the socket. It's now allowed for it to be |
| 1387 |
// a group or socket. For a group, add automatically a socket to |
| 1388 |
// the group. |
| 1389 |
if (u & SRTGROUP_MASK) |
| 1390 |
{ |
| 1391 |
GroupKeeper k(*this, u, ERH_THROW); |
| 1392 |
// Note: forced_isn is ignored when connecting a group. |
| 1393 |
// The group manages the ISN by itself ALWAYS, that is, |
| 1394 |
// it's generated anew for the very first socket, and then |
| 1395 |
// derived by all sockets in the group. |
| 1396 |
SRT_SOCKGROUPCONFIG gd[1] = {srt_prepare_endpoint(srcname, tarname, namelen)}; |
| 1397 |
|
| 1398 |
// When connecting to exactly one target, only this very target |
| 1399 |
// can be returned as a socket, so rewritten back array can be ignored. |
| 1400 |
return singleMemberConnect(k.group, gd); |
| 1401 |
} |
| 1402 |
#endif |
| 1403 |
|
| 1404 |
CUDTSocket* s = locateSocket(u); |
| 1405 |
if (s == NULL) |
| 1406 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 1407 |
|
| 1408 |
// For a single socket, just do bind, then connect |
| 1409 |
bind(s, source_addr); |
| 1410 |
return connectIn(s, target_addr, SRT_SEQNO_NONE); |
| 1411 |
} |
| 1412 |
|
| 1413 |
int srt::CUDTUnited::connect(const SRTSOCKET u, const sockaddr* name, int namelen, int32_t forced_isn) |
| 1414 |
{ |
| 1415 |
if (!name || namelen < int(sizeof(sockaddr_in))) |
| 1416 |
{ |
| 1417 |
LOGC(aclog.Error, log << "connect(): invalid call: name=" << name << " namelen=" << namelen); |
| 1418 |
throw CUDTException(MJ_NOTSUP, MN_INVAL); |
| 1419 |
} |
| 1420 |
|
| 1421 |
sockaddr_any target_addr(name, namelen); |
| 1422 |
if (target_addr.len == 0) |
| 1423 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 1424 |
|
| 1425 |
#if ENABLE_BONDING |
| 1426 |
// Check affiliation of the socket. It's now allowed for it to be |
| 1427 |
// a group or socket. For a group, add automatically a socket to |
| 1428 |
// the group. |
| 1429 |
if (u & SRTGROUP_MASK) |
| 1430 |
{ |
| 1431 |
GroupKeeper k(*this, u, ERH_THROW); |
| 1432 |
|
| 1433 |
// Note: forced_isn is ignored when connecting a group. |
| 1434 |
// The group manages the ISN by itself ALWAYS, that is, |
| 1435 |
// it's generated anew for the very first socket, and then |
| 1436 |
// derived by all sockets in the group. |
| 1437 |
SRT_SOCKGROUPCONFIG gd[1] = {srt_prepare_endpoint(NULL, name, namelen)}; |
| 1438 |
return singleMemberConnect(k.group, gd); |
| 1439 |
} |
| 1440 |
#endif |
| 1441 |
|
| 1442 |
CUDTSocket* s = locateSocket(u); |
| 1443 |
if (!s) |
| 1444 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 1445 |
|
| 1446 |
return connectIn(s, target_addr, forced_isn); |
| 1447 |
} |
| 1448 |
|
| 1449 |
#if ENABLE_BONDING |
| 1450 |
int srt::CUDTUnited::singleMemberConnect(CUDTGroup* pg, SRT_SOCKGROUPCONFIG* gd) |
| 1451 |
{ |
| 1452 |
int gstat = groupConnect(pg, gd, 1); |
| 1453 |
if (gstat == -1) |
| 1454 |
{ |
| 1455 |
// We have only one element here, so refer to it. |
| 1456 |
// Sanity check |
| 1457 |
if (gd->errorcode == SRT_SUCCESS) |
| 1458 |
gd->errorcode = SRT_EINVPARAM; |
| 1459 |
|
| 1460 |
CodeMajor mj = CodeMajor(gd->errorcode / 1000); |
| 1461 |
CodeMinor mn = CodeMinor(gd->errorcode % 1000); |
| 1462 |
|
| 1463 |
return CUDT::APIError(mj, mn); |
| 1464 |
} |
| 1465 |
|
| 1466 |
return gstat; |
| 1467 |
} |
| 1468 |
|
| 1469 |
// [[using assert(pg->m_iBusy > 0)]] |
| 1470 |
int srt::CUDTUnited::groupConnect(CUDTGroup* pg, SRT_SOCKGROUPCONFIG* targets, int arraysize) |
| 1471 |
{ |
| 1472 |
CUDTGroup& g = *pg; |
| 1473 |
SRT_ASSERT(g.m_iBusy > 0); |
| 1474 |
|
| 1475 |
// Check and report errors on data brought in by srt_prepare_endpoint, |
| 1476 |
// as the latter function has no possibility to report errors. |
| 1477 |
for (int tii = 0; tii < arraysize; ++tii) |
| 1478 |
{ |
| 1479 |
if (targets[tii].srcaddr.ss_family != targets[tii].peeraddr.ss_family) |
| 1480 |
{ |
| 1481 |
LOGC(aclog.Error, log << "srt_connect/group: family differs on source and target address"); |
| 1482 |
throw CUDTException(MJ_NOTSUP, MN_INVAL); |
| 1483 |
} |
| 1484 |
|
| 1485 |
if (targets[tii].weight > CUDT::MAX_WEIGHT) |
| 1486 |
{ |
| 1487 |
LOGC(aclog.Error, log << "srt_connect/group: weight value must be between 0 and " << (+CUDT::MAX_WEIGHT)); |
| 1488 |
throw CUDTException(MJ_NOTSUP, MN_INVAL); |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
// If the open state switched to OPENED, the blocking mode |
| 1493 |
// must make it wait for connecting it. Doing connect when the |
| 1494 |
// group is already OPENED returns immediately, regardless if the |
| 1495 |
// connection is going to later succeed or fail (this will be |
| 1496 |
// known in the group state information). |
| 1497 |
bool block_new_opened = !g.m_bOpened && g.m_bSynRecving; |
| 1498 |
const bool was_empty = g.groupEmpty(); |
| 1499 |
|
| 1500 |
// In case the group was retried connection, clear first all epoll readiness. |
| 1501 |
const int ncleared = m_EPoll.update_events(g.id(), g.m_sPollID, SRT_EPOLL_ERR, false); |
| 1502 |
if (was_empty || ncleared) |
| 1503 |
{ |
| 1504 |
HLOGC(aclog.Debug, |
| 1505 |
log << "srt_connect/group: clearing IN/OUT because was_empty=" << was_empty |
| 1506 |
<< " || ncleared=" << ncleared); |
| 1507 |
// IN/OUT only in case when the group is empty, otherwise it would |
| 1508 |
// clear out correct readiness resulting from earlier calls. |
| 1509 |
// This also should happen if ERR flag was set, as IN and OUT could be set, too. |
| 1510 |
m_EPoll.update_events(g.id(), g.m_sPollID, SRT_EPOLL_IN | SRT_EPOLL_OUT, false); |
| 1511 |
} |
| 1512 |
SRTSOCKET retval = -1; |
| 1513 |
|
| 1514 |
int eid = -1; |
| 1515 |
int connect_modes = SRT_EPOLL_CONNECT | SRT_EPOLL_ERR; |
| 1516 |
if (block_new_opened) |
| 1517 |
{ |
| 1518 |
// Create this eid only to block-wait for the first |
| 1519 |
// connection. |
| 1520 |
eid = srt_epoll_create(); |
| 1521 |
} |
| 1522 |
|
| 1523 |
// Use private map to avoid searching in the |
| 1524 |
// overall map. |
| 1525 |
map<SRTSOCKET, CUDTSocket*> spawned; |
| 1526 |
|
| 1527 |
HLOGC(aclog.Debug, |
| 1528 |
log << "groupConnect: will connect " << arraysize << " links and " |
| 1529 |
<< (block_new_opened ? "BLOCK until any is ready" : "leave the process in background")); |
| 1530 |
|
| 1531 |
for (int tii = 0; tii < arraysize; ++tii) |
| 1532 |
{ |
| 1533 |
sockaddr_any target_addr(targets[tii].peeraddr); |
| 1534 |
sockaddr_any source_addr(targets[tii].srcaddr); |
| 1535 |
SRTSOCKET& sid_rloc = targets[tii].id; |
| 1536 |
int& erc_rloc = targets[tii].errorcode; |
| 1537 |
erc_rloc = SRT_SUCCESS; // preinitialized |
| 1538 |
HLOGC(aclog.Debug, log << "groupConnect: taking on " << sockaddr_any(targets[tii].peeraddr).str()); |
| 1539 |
|
| 1540 |
CUDTSocket* ns = 0; |
| 1541 |
|
| 1542 |
// NOTE: After calling newSocket, the socket is mapped into m_Sockets. |
| 1543 |
// It must be MANUALLY removed from this list in case we need it deleted. |
| 1544 |
SRTSOCKET sid = newSocket(&ns); |
| 1545 |
|
| 1546 |
if (pg->m_cbConnectHook) |
| 1547 |
{ |
| 1548 |
// Derive the connect hook by the socket, if set on the group |
| 1549 |
ns->core().m_cbConnectHook = pg->m_cbConnectHook; |
| 1550 |
} |
| 1551 |
|
| 1552 |
SRT_SocketOptionObject* config = targets[tii].config; |
| 1553 |
|
| 1554 |
// XXX Support non-blocking mode: |
| 1555 |
// If the group has nonblocking set for connect (SNDSYN), |
| 1556 |
// then it must set so on the socket. Then, the connection |
| 1557 |
// process is asynchronous. The socket appears first as |
| 1558 |
// GST_PENDING state, and only after the socket becomes |
| 1559 |
// connected does its status in the group turn into GST_IDLE. |
| 1560 |
|
| 1561 |
// Set all options that were requested by the options set on a group |
| 1562 |
// prior to connecting. |
| 1563 |
string error_reason SRT_ATR_UNUSED; |
| 1564 |
try |
| 1565 |
{ |
| 1566 |
for (size_t i = 0; i < g.m_config.size(); ++i) |
| 1567 |
{ |
| 1568 |
HLOGC(aclog.Debug, log << "groupConnect: OPTION @" << sid << " #" << g.m_config[i].so); |
| 1569 |
error_reason = "group-derived option: #" + Sprint(g.m_config[i].so); |
| 1570 |
ns->core().setOpt(g.m_config[i].so, &g.m_config[i].value[0], (int)g.m_config[i].value.size()); |
| 1571 |
} |
| 1572 |
|
| 1573 |
// Do not try to set a user option if failed already. |
| 1574 |
if (config) |
| 1575 |
{ |
| 1576 |
error_reason = "user option"; |
| 1577 |
ns->core().applyMemberConfigObject(*config); |
| 1578 |
} |
| 1579 |
|
| 1580 |
error_reason = "bound address"; |
| 1581 |
// We got it. Bind the socket, if the source address was set |
| 1582 |
if (!source_addr.empty()) |
| 1583 |
bind(ns, source_addr); |
| 1584 |
} |
| 1585 |
catch (CUDTException& e) |
| 1586 |
{ |
| 1587 |
// Just notify the problem, but the loop must continue. |
| 1588 |
// Set the original error as reported. |
| 1589 |
targets[tii].errorcode = e.getErrorCode(); |
| 1590 |
LOGC(aclog.Error, log << "srt_connect_group: failed to set " << error_reason); |
| 1591 |
} |
| 1592 |
catch (...) |
| 1593 |
{ |
| 1594 |
// Set the general EINVPARAM - this error should never happen |
| 1595 |
LOGC(aclog.Error, log << "IPE: CUDT::setOpt reported unknown exception"); |
| 1596 |
targets[tii].errorcode = SRT_EINVPARAM; |
| 1597 |
} |
| 1598 |
|
| 1599 |
// Add socket to the group. |
| 1600 |
// Do it after setting all stored options, as some of them may |
| 1601 |
// influence some group data. |
| 1602 |
|
| 1603 |
srt::groups::SocketData data = srt::groups::prepareSocketData(ns); |
| 1604 |
if (targets[tii].token != -1) |
| 1605 |
{ |
| 1606 |
// Reuse the token, if specified by the caller |
| 1607 |
data.token = targets[tii].token; |
| 1608 |
} |
| 1609 |
else |
| 1610 |
{ |
| 1611 |
// Otherwise generate and write back the token |
| 1612 |
data.token = CUDTGroup::genToken(); |
| 1613 |
targets[tii].token = data.token; |
| 1614 |
} |
| 1615 |
|
| 1616 |
{ |
| 1617 |
ExclusiveLock cs(m_GlobControlLock); |
| 1618 |
if (m_Sockets.count(sid) == 0) |
| 1619 |
{ |
| 1620 |
HLOGC(aclog.Debug, log << "srt_connect_group: socket @" << sid << " deleted in process"); |
| 1621 |
// Someone deleted the socket in the meantime? |
| 1622 |
// Unlikely, but possible in theory. |
| 1623 |
// Don't delete anything - it's already done. |
| 1624 |
continue; |
| 1625 |
} |
| 1626 |
|
| 1627 |
// There's nothing wrong with preparing the data first |
| 1628 |
// even if this happens for nothing. But now, under the lock |
| 1629 |
// and after checking that the socket still exists, check now |
| 1630 |
// if this succeeded, and then also if the group is still usable. |
| 1631 |
// The group will surely exist because it's set busy, until the |
| 1632 |
// end of this function. But it might be simultaneously requested closed. |
| 1633 |
bool proceed = true; |
| 1634 |
|
| 1635 |
if (targets[tii].errorcode != SRT_SUCCESS) |
| 1636 |
{ |
| 1637 |
HLOGC(aclog.Debug, |
| 1638 |
log << "srt_connect_group: not processing @" << sid << " due to error in setting options"); |
| 1639 |
proceed = false; |
| 1640 |
} |
| 1641 |
|
| 1642 |
if (g.m_bClosing) |
| 1643 |
{ |
| 1644 |
HLOGC(aclog.Debug, |
| 1645 |
log << "srt_connect_group: not processing @" << sid << " due to CLOSED GROUP $" << g.m_GroupID); |
| 1646 |
proceed = false; |
| 1647 |
} |
| 1648 |
|
| 1649 |
if (proceed) |
| 1650 |
{ |
| 1651 |
CUDTGroup::SocketData* f = g.add(data); |
| 1652 |
ns->m_GroupMemberData = f; |
| 1653 |
ns->m_GroupOf = &g; |
| 1654 |
f->weight = targets[tii].weight; |
| 1655 |
HLOGC(aclog.Debug, log << "srt_connect_group: socket @" << sid << " added to group $" << g.m_GroupID); |
| 1656 |
} |
| 1657 |
else |
| 1658 |
{ |
| 1659 |
targets[tii].id = CUDT::INVALID_SOCK; |
| 1660 |
delete ns; |
| 1661 |
m_Sockets.erase(sid); |
| 1662 |
|
| 1663 |
// If failed to set options, then do not continue |
| 1664 |
// neither with binding, nor with connecting. |
| 1665 |
continue; |
| 1666 |
} |
| 1667 |
} |
| 1668 |
|
| 1669 |
// XXX This should be re-enabled later, this should |
| 1670 |
// be probably still in use to exchange information about |
| 1671 |
// packets asymmetrically lost. But for no other purpose. |
| 1672 |
/* |
| 1673 |
ns->core().m_cbPacketArrival.set(ns->m_pUDT, &CUDT::groupPacketArrival); |
| 1674 |
*/ |
| 1675 |
|
| 1676 |
int isn = g.currentSchedSequence(); |
| 1677 |
|
| 1678 |
// Set it the groupconnect option, as all in-group sockets should have. |
| 1679 |
ns->core().m_config.iGroupConnect = 1; |
| 1680 |
|
| 1681 |
// Every group member will have always nonblocking |
| 1682 |
// (this implies also non-blocking connect/accept). |
| 1683 |
// The group facility functions will block when necessary |
| 1684 |
// using epoll_wait. |
| 1685 |
ns->core().m_config.bSynRecving = false; |
| 1686 |
ns->core().m_config.bSynSending = false; |
| 1687 |
|
| 1688 |
HLOGC(aclog.Debug, log << "groupConnect: NOTIFIED AS PENDING @" << sid << " both read and write"); |
| 1689 |
// If this socket is not to block the current connect process, |
| 1690 |
// it may still be needed for the further check if the redundant |
| 1691 |
// connection succeeded or failed and whether the new socket is |
| 1692 |
// ready to use or needs to be closed. |
| 1693 |
epoll_add_usock_INTERNAL(g.m_SndEID, ns, &connect_modes); |
| 1694 |
epoll_add_usock_INTERNAL(g.m_RcvEID, ns, &connect_modes); |
| 1695 |
|
| 1696 |
// Adding a socket on which we need to block to BOTH these tracking EIDs |
| 1697 |
// and the blocker EID. We'll simply remove from them later all sockets that |
| 1698 |
// got connected state or were broken. |
| 1699 |
|
| 1700 |
if (block_new_opened) |
| 1701 |
{ |
| 1702 |
HLOGC(aclog.Debug, log << "groupConnect: WILL BLOCK on @" << sid << " until connected"); |
| 1703 |
epoll_add_usock_INTERNAL(eid, ns, &connect_modes); |
| 1704 |
} |
| 1705 |
|
| 1706 |
// And connect |
| 1707 |
try |
| 1708 |
{ |
| 1709 |
HLOGC(aclog.Debug, log << "groupConnect: connecting a new socket with ISN=" << isn); |
| 1710 |
connectIn(ns, target_addr, isn); |
| 1711 |
} |
| 1712 |
catch (const CUDTException& e) |
| 1713 |
{ |
| 1714 |
LOGC(aclog.Error, |
| 1715 |
log << "groupConnect: socket @" << sid << " in group " << pg->id() << " failed to connect"); |
| 1716 |
// We know it does belong to a group. |
| 1717 |
// Remove it first because this involves a mutex, and we want |
| 1718 |
// to avoid locking more than one mutex at a time. |
| 1719 |
erc_rloc = e.getErrorCode(); |
| 1720 |
targets[tii].errorcode = e.getErrorCode(); |
| 1721 |
targets[tii].id = CUDT::INVALID_SOCK; |
| 1722 |
|
| 1723 |
ExclusiveLock cl(m_GlobControlLock); |
| 1724 |
ns->removeFromGroup(false); |
| 1725 |
m_Sockets.erase(ns->m_SocketID); |
| 1726 |
// Intercept to delete the socket on failure. |
| 1727 |
delete ns; |
| 1728 |
continue; |
| 1729 |
} |
| 1730 |
catch (...) |
| 1731 |
{ |
| 1732 |
LOGC(aclog.Fatal, log << "groupConnect: IPE: UNKNOWN EXCEPTION from connectIn"); |
| 1733 |
targets[tii].errorcode = SRT_ESYSOBJ; |
| 1734 |
targets[tii].id = CUDT::INVALID_SOCK; |
| 1735 |
ExclusiveLock cl(m_GlobControlLock); |
| 1736 |
ns->removeFromGroup(false); |
| 1737 |
m_Sockets.erase(ns->m_SocketID); |
| 1738 |
// Intercept to delete the socket on failure. |
| 1739 |
delete ns; |
| 1740 |
|
| 1741 |
// Do not use original exception, it may crash off a C API. |
| 1742 |
throw CUDTException(MJ_SYSTEMRES, MN_OBJECT); |
| 1743 |
} |
| 1744 |
|
| 1745 |
SRT_SOCKSTATUS st; |
| 1746 |
{ |
| 1747 |
ScopedLock grd(ns->m_ControlLock); |
| 1748 |
st = ns->getStatus(); |
| 1749 |
} |
| 1750 |
|
| 1751 |
{ |
| 1752 |
// NOTE: Not applying m_GlobControlLock because the group is now |
| 1753 |
// set busy, so it won't be deleted, even if it was requested to be closed. |
| 1754 |
ScopedLock grd(g.m_GroupLock); |
| 1755 |
|
| 1756 |
if (!ns->m_GroupOf) |
| 1757 |
{ |
| 1758 |
// The situation could get changed between the unlock and lock of m_GroupLock. |
| 1759 |
// This must be checked again. |
| 1760 |
// If a socket has been removed from group, it means that some other thread is |
| 1761 |
// currently trying to delete the socket. Therefore it doesn't have, and even shouldn't, |
| 1762 |
// be deleted here. Just exit with error report. |
| 1763 |
LOGC(aclog.Error, log << "groupConnect: self-created member socket deleted during process, SKIPPING."); |
| 1764 |
|
| 1765 |
// Do not report the error from here, just ignore this socket. |
| 1766 |
continue; |
| 1767 |
} |
| 1768 |
|
| 1769 |
// If m_GroupOf is not NULL, the m_IncludedIter is still valid. |
| 1770 |
CUDTGroup::SocketData* f = ns->m_GroupMemberData; |
| 1771 |
|
| 1772 |
// Now under a group lock, we need to make sure the group isn't being closed |
| 1773 |
// in order not to add a socket to a dead group. |
| 1774 |
if (g.m_bClosing) |
| 1775 |
{ |
| 1776 |
LOGC(aclog.Error, log << "groupConnect: group deleted while connecting; breaking the process"); |
| 1777 |
|
| 1778 |
// Set the status as pending so that the socket is taken care of later. |
| 1779 |
// Note that all earlier sockets that were processed in this loop were either |
| 1780 |
// set BROKEN or PENDING. |
| 1781 |
f->sndstate = SRT_GST_PENDING; |
| 1782 |
f->rcvstate = SRT_GST_PENDING; |
| 1783 |
retval = -1; |
| 1784 |
break; |
| 1785 |
} |
| 1786 |
|
| 1787 |
HLOGC(aclog.Debug, |
| 1788 |
log << "groupConnect: @" << sid << " connection successful, setting group OPEN (was " |
| 1789 |
<< (g.m_bOpened ? "ALREADY" : "NOT") << "), will " << (block_new_opened ? "" : "NOT ") |
| 1790 |
<< "block the connect call, status:" << SockStatusStr(st)); |
| 1791 |
|
| 1792 |
// XXX OPEN OR CONNECTED? |
| 1793 |
// BLOCK IF NOT OPEN OR BLOCK IF NOT CONNECTED? |
| 1794 |
// |
| 1795 |
// What happens to blocking when there are 2 connections |
| 1796 |
// pending, about to be broken, and srt_connect() is called again? |
| 1797 |
// SHOULD BLOCK the latter, even though is OPEN. |
| 1798 |
// Or, OPEN should be removed from here and srt_connect(_group) |
| 1799 |
// should block always if the group doesn't have neither 1 connected link |
| 1800 |
g.m_bOpened = true; |
| 1801 |
|
| 1802 |
g.m_stats.tsLastSampleTime = steady_clock::now(); |
| 1803 |
|
| 1804 |
f->laststatus = st; |
| 1805 |
// Check the socket status and update it. |
| 1806 |
// Turn the group state of the socket to IDLE only if |
| 1807 |
// connection is established or in progress |
| 1808 |
f->agent = source_addr; |
| 1809 |
f->peer = target_addr; |
| 1810 |
|
| 1811 |
if (st >= SRTS_BROKEN) |
| 1812 |
{ |
| 1813 |
f->sndstate = SRT_GST_BROKEN; |
| 1814 |
f->rcvstate = SRT_GST_BROKEN; |
| 1815 |
epoll_remove_socket_INTERNAL(g.m_SndEID, ns); |
| 1816 |
epoll_remove_socket_INTERNAL(g.m_RcvEID, ns); |
| 1817 |
} |
| 1818 |
else |
| 1819 |
{ |
| 1820 |
f->sndstate = SRT_GST_PENDING; |
| 1821 |
f->rcvstate = SRT_GST_PENDING; |
| 1822 |
spawned[sid] = ns; |
| 1823 |
|
| 1824 |
sid_rloc = sid; |
| 1825 |
erc_rloc = 0; |
| 1826 |
retval = sid; |
| 1827 |
} |
| 1828 |
} |
| 1829 |
} |
| 1830 |
|
| 1831 |
if (retval == -1) |
| 1832 |
{ |
| 1833 |
HLOGC(aclog.Debug, log << "groupConnect: none succeeded as background-spawn, exit with error"); |
| 1834 |
block_new_opened = false; // Avoid executing further while loop |
| 1835 |
} |
| 1836 |
|
| 1837 |
vector<SRTSOCKET> broken; |
| 1838 |
|
| 1839 |
while (block_new_opened) |
| 1840 |
{ |
| 1841 |
if (spawned.empty()) |
| 1842 |
{ |
| 1843 |
// All were removed due to errors. |
| 1844 |
retval = -1; |
| 1845 |
break; |
| 1846 |
} |
| 1847 |
HLOGC(aclog.Debug, log << "groupConnect: first connection, applying EPOLL WAITING."); |
| 1848 |
int len = (int)spawned.size(); |
| 1849 |
vector<SRTSOCKET> ready(spawned.size()); |
| 1850 |
const int estat = srt_epoll_wait(eid, |
| 1851 |
NULL, |
| 1852 |
NULL, // IN/ACCEPT |
| 1853 |
&ready[0], |
| 1854 |
&len, // OUT/CONNECT |
| 1855 |
-1, // indefinitely (FIXME Check if it needs to REGARD CONNECTION TIMEOUT!) |
| 1856 |
NULL, |
| 1857 |
NULL, |
| 1858 |
NULL, |
| 1859 |
NULL); |
| 1860 |
|
| 1861 |
// Sanity check. Shouldn't happen if subs are in sync with spawned. |
| 1862 |
if (estat == -1) |
| 1863 |
{ |
| 1864 |
#if ENABLE_LOGGING |
| 1865 |
CUDTException& x = CUDT::getlasterror(); |
| 1866 |
if (x.getErrorCode() != SRT_EPOLLEMPTY) |
| 1867 |
{ |
| 1868 |
LOGC(aclog.Error, |
| 1869 |
log << "groupConnect: srt_epoll_wait failed not because empty, unexpected IPE:" |
| 1870 |
<< x.getErrorMessage()); |
| 1871 |
} |
| 1872 |
#endif |
| 1873 |
HLOGC(aclog.Debug, log << "groupConnect: srt_epoll_wait failed - breaking the wait loop"); |
| 1874 |
retval = -1; |
| 1875 |
break; |
| 1876 |
} |
| 1877 |
|
| 1878 |
// At the moment when you are going to work with real sockets, |
| 1879 |
// lock the groups so that no one messes up with something here |
| 1880 |
// in the meantime. |
| 1881 |
|
| 1882 |
ScopedLock lock(*g.exp_groupLock()); |
| 1883 |
|
| 1884 |
// NOTE: UNDER m_GroupLock, NO API FUNCTION CALLS DARE TO HAPPEN BELOW! |
| 1885 |
|
| 1886 |
// Check first if a socket wasn't closed in the meantime. It will be |
| 1887 |
// automatically removed from all EIDs, but there's no sense in keeping |
| 1888 |
// them in 'spawned' map. |
| 1889 |
for (map<SRTSOCKET, CUDTSocket*>::iterator y = spawned.begin(); y != spawned.end(); ++y) |
| 1890 |
{ |
| 1891 |
SRTSOCKET sid = y->first; |
| 1892 |
if (y->second->getStatus() >= SRTS_BROKEN) |
| 1893 |
{ |
| 1894 |
HLOGC(aclog.Debug, |
| 1895 |
log << "groupConnect: Socket @" << sid |
| 1896 |
<< " got BROKEN in the meantine during the check, remove from candidates"); |
| 1897 |
// Remove from spawned and try again |
| 1898 |
broken.push_back(sid); |
| 1899 |
|
| 1900 |
epoll_remove_socket_INTERNAL(eid, y->second); |
| 1901 |
epoll_remove_socket_INTERNAL(g.m_SndEID, y->second); |
| 1902 |
epoll_remove_socket_INTERNAL(g.m_RcvEID, y->second); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
// Remove them outside the loop because this can't be done |
| 1907 |
// while iterating over the same container. |
| 1908 |
for (size_t i = 0; i < broken.size(); ++i) |
| 1909 |
spawned.erase(broken[i]); |
| 1910 |
|
| 1911 |
// Check the sockets if they were reported due |
| 1912 |
// to have connected or due to have failed. |
| 1913 |
// Distill successful ones. If distilled nothing, return -1. |
| 1914 |
// If not all sockets were reported in this instance, repeat |
| 1915 |
// the call until you get information about all of them. |
| 1916 |
for (int i = 0; i < len; ++i) |
| 1917 |
{ |
| 1918 |
map<SRTSOCKET, CUDTSocket*>::iterator x = spawned.find(ready[i]); |
| 1919 |
if (x == spawned.end()) |
| 1920 |
{ |
| 1921 |
// Might be removed above - ignore it. |
| 1922 |
continue; |
| 1923 |
} |
| 1924 |
|
| 1925 |
SRTSOCKET sid = x->first; |
| 1926 |
CUDTSocket* s = x->second; |
| 1927 |
|
| 1928 |
// Check status. If failed, remove from spawned |
| 1929 |
// and try again. |
| 1930 |
SRT_SOCKSTATUS st = s->getStatus(); |
| 1931 |
if (st >= SRTS_BROKEN) |
| 1932 |
{ |
| 1933 |
HLOGC(aclog.Debug, |
| 1934 |
log << "groupConnect: Socket @" << sid |
| 1935 |
<< " got BROKEN during background connect, remove & TRY AGAIN"); |
| 1936 |
// Remove from spawned and try again |
| 1937 |
if (spawned.erase(sid)) |
| 1938 |
broken.push_back(sid); |
| 1939 |
|
| 1940 |
epoll_remove_socket_INTERNAL(eid, s); |
| 1941 |
epoll_remove_socket_INTERNAL(g.m_SndEID, s); |
| 1942 |
epoll_remove_socket_INTERNAL(g.m_RcvEID, s); |
| 1943 |
|
| 1944 |
continue; |
| 1945 |
} |
| 1946 |
|
| 1947 |
if (st == SRTS_CONNECTED) |
| 1948 |
{ |
| 1949 |
HLOGC(aclog.Debug, |
| 1950 |
log << "groupConnect: Socket @" << sid << " got CONNECTED as first in the group - reporting"); |
| 1951 |
retval = sid; |
| 1952 |
g.m_bConnected = true; |
| 1953 |
block_new_opened = false; // Interrupt also rolling epoll (outer loop) |
| 1954 |
|
| 1955 |
// Remove this socket from SND EID because it doesn't need to |
| 1956 |
// be connection-tracked anymore. Don't remove from the RCV EID |
| 1957 |
// however because RCV procedure relies on epoll also for reading |
| 1958 |
// and when found this socket connected it will "upgrade" it to |
| 1959 |
// read-ready tracking only. |
| 1960 |
epoll_remove_socket_INTERNAL(g.m_SndEID, s); |
| 1961 |
break; |
| 1962 |
} |
| 1963 |
|
| 1964 |
// Spurious? |
| 1965 |
HLOGC(aclog.Debug, |
| 1966 |
log << "groupConnect: Socket @" << sid << " got spurious wakeup in " << SockStatusStr(st) |
| 1967 |
<< " TRY AGAIN"); |
| 1968 |
} |
| 1969 |
// END of m_GroupLock CS - you can safely use API functions now. |
| 1970 |
} |
| 1971 |
// Finished, delete epoll. |
| 1972 |
if (eid != -1) |
| 1973 |
{ |
| 1974 |
HLOGC(aclog.Debug, log << "connect FIRST IN THE GROUP finished, removing E" << eid); |
| 1975 |
srt_epoll_release(eid); |
| 1976 |
} |
| 1977 |
|
| 1978 |
for (vector<SRTSOCKET>::iterator b = broken.begin(); b != broken.end(); ++b) |
| 1979 |
{ |
| 1980 |
CUDTSocket* s = locateSocket(*b, ERH_RETURN); |
| 1981 |
if (!s) |
| 1982 |
continue; |
| 1983 |
|
| 1984 |
// This will also automatically remove it from the group and all eids |
| 1985 |
close(s); |
| 1986 |
} |
| 1987 |
|
| 1988 |
// There's no possibility to report a problem on every connection |
| 1989 |
// separately in case when every single connection has failed. What |
| 1990 |
// is more interesting, it's only a matter of luck that all connections |
| 1991 |
// fail at exactly the same time. OTOH if all are to fail, this |
| 1992 |
// function will still be polling sockets to determine the last man |
| 1993 |
// standing. Each one could, however, break by a different reason, |
| 1994 |
// for example, one by timeout, another by wrong passphrase. Check |
| 1995 |
// the `errorcode` field to determine the reason for particular link. |
| 1996 |
if (retval == -1) |
| 1997 |
throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0); |
| 1998 |
|
| 1999 |
return retval; |
| 2000 |
} |
| 2001 |
#endif |
| 2002 |
|
| 2003 |
int srt::CUDTUnited::connectIn(CUDTSocket* s, const sockaddr_any& target_addr, int32_t forced_isn) |
| 2004 |
{ |
| 2005 |
ScopedLock cg(s->m_ControlLock); |
| 2006 |
// a socket can "connect" only if it is in the following states: |
| 2007 |
// - OPENED: assume the socket binding parameters are configured |
| 2008 |
// - INIT: configure binding parameters here |
| 2009 |
// - any other (meaning, already connected): report error |
| 2010 |
|
| 2011 |
if (s->m_Status == SRTS_INIT) |
| 2012 |
{ |
| 2013 |
if (s->core().m_config.bRendezvous) |
| 2014 |
throw CUDTException(MJ_NOTSUP, MN_ISRENDUNBOUND, 0); |
| 2015 |
|
| 2016 |
// If bind() was done first on this socket, then the |
| 2017 |
// socket will not perform this step. This actually does the |
| 2018 |
// same thing as bind() does, just with empty address so that |
| 2019 |
// the binding parameters are autoselected. |
| 2020 |
|
| 2021 |
s->core().open(); |
| 2022 |
sockaddr_any autoselect_sa(target_addr.family()); |
| 2023 |
// This will create such a sockaddr_any that |
| 2024 |
// will return true from empty(). |
| 2025 |
updateMux(s, autoselect_sa); // <<---- updateMux |
| 2026 |
// -> C(Snd|Rcv)Queue::init |
| 2027 |
// -> pthread_create(...C(Snd|Rcv)Queue::worker...) |
| 2028 |
s->m_Status = SRTS_OPENED; |
| 2029 |
} |
| 2030 |
else |
| 2031 |
{ |
| 2032 |
// Only SRTS_OPENED status is otherwise expected. |
| 2033 |
// Although depending on the state, different type of errors |
| 2034 |
if (int(s->m_Status) >= SRTS_BROKEN) // BROKEN, CLOSING, CLOSED, NONEXIST |
| 2035 |
throw CUDTException(MJ_SETUP, MN_CLOSED, 0); |
| 2036 |
|
| 2037 |
if (s->m_Status != SRTS_OPENED) // LISTENING, CONNECTING, CONNECTED |
| 2038 |
throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0); |
| 2039 |
|
| 2040 |
// status = SRTS_OPENED, so family should be known already. |
| 2041 |
if (target_addr.family() != s->m_SelfAddr.family()) |
| 2042 |
{ |
| 2043 |
LOGP(cnlog.Error, "srt_connect: socket is bound to a different family than target address"); |
| 2044 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 2045 |
} |
| 2046 |
} |
| 2047 |
|
| 2048 |
// connect_complete() may be called before connect() returns. |
| 2049 |
// So we need to update the status before connect() is called, |
| 2050 |
// otherwise the status may be overwritten with wrong value |
| 2051 |
// (CONNECTED vs. CONNECTING). |
| 2052 |
s->m_Status = SRTS_CONNECTING; |
| 2053 |
|
| 2054 |
/* |
| 2055 |
* In blocking mode, connect can block for up to 30 seconds for |
| 2056 |
* rendez-vous mode. Holding the s->m_ControlLock prevent close |
| 2057 |
* from cancelling the connect |
| 2058 |
*/ |
| 2059 |
try |
| 2060 |
{ |
| 2061 |
// record peer address |
| 2062 |
s->m_PeerAddr = target_addr; |
| 2063 |
s->core().startConnect(target_addr, forced_isn); |
| 2064 |
} |
| 2065 |
catch (const CUDTException&) // Interceptor, just to change the state. |
| 2066 |
{ |
| 2067 |
s->m_Status = SRTS_OPENED; |
| 2068 |
throw; |
| 2069 |
} |
| 2070 |
|
| 2071 |
return 0; |
| 2072 |
} |
| 2073 |
|
| 2074 |
int srt::CUDTUnited::close(const SRTSOCKET u) |
| 2075 |
{ |
| 2076 |
#if ENABLE_BONDING |
| 2077 |
if (u & SRTGROUP_MASK) |
| 2078 |
{ |
| 2079 |
GroupKeeper k(*this, u, ERH_THROW); |
| 2080 |
k.group->close(); |
| 2081 |
deleteGroup(k.group); |
| 2082 |
return 0; |
| 2083 |
} |
| 2084 |
#endif |
| 2085 |
#if ENABLE_HEAVY_LOGGING |
| 2086 |
// Wrapping the log into a destructor so that it |
| 2087 |
// is printed AFTER the destructor of SocketKeeper. |
| 2088 |
struct ScopedExitLog |
| 2089 |
{ |
| 2090 |
const CUDTSocket* const ps; |
| 2091 |
ScopedExitLog(const CUDTSocket* p): ps(p){} |
| 2092 |
~ScopedExitLog() |
| 2093 |
{ |
| 2094 |
if (ps) // Could be not acquired by SocketKeeper, occasionally |
| 2095 |
{ |
| 2096 |
HLOGC(smlog.Debug, log << "CUDTUnited::close/end: @" << ps->m_SocketID << " busy=" << ps->isStillBusy()); |
| 2097 |
} |
| 2098 |
} |
| 2099 |
}; |
| 2100 |
#endif |
| 2101 |
|
| 2102 |
SocketKeeper k(*this, u, ERH_THROW); |
| 2103 |
IF_HEAVY_LOGGING(ScopedExitLog slog(k.socket)); |
| 2104 |
HLOGC(smlog.Debug, log << "CUDTUnited::close/begin: @" << u << " busy=" << k.socket->isStillBusy()); |
| 2105 |
|
| 2106 |
return close(k.socket); |
| 2107 |
} |
| 2108 |
|
| 2109 |
#if ENABLE_BONDING |
| 2110 |
void srt::CUDTUnited::deleteGroup(CUDTGroup* g) |
| 2111 |
{ |
| 2112 |
using srt_logging::gmlog; |
| 2113 |
|
| 2114 |
srt::sync::ExclusiveLock cg(m_GlobControlLock); |
| 2115 |
return deleteGroup_LOCKED(g); |
| 2116 |
} |
| 2117 |
|
| 2118 |
// [[using locked(m_GlobControlLock)]] |
| 2119 |
void srt::CUDTUnited::deleteGroup_LOCKED(CUDTGroup* g) |
| 2120 |
{ |
| 2121 |
SRT_ASSERT(g->groupEmpty()); |
| 2122 |
|
| 2123 |
// After that the group is no longer findable by GroupKeeper |
| 2124 |
m_Groups.erase(g->m_GroupID); |
| 2125 |
m_ClosedGroups[g->m_GroupID] = g; |
| 2126 |
|
| 2127 |
// Paranoid check: since the group is in m_ClosedGroups |
| 2128 |
// it may potentially be deleted. Make sure no socket points |
| 2129 |
// to it. Actually all sockets should have been already removed |
| 2130 |
// from the group container, so if any does, it's invalid. |
| 2131 |
for (sockets_t::iterator i = m_Sockets.begin(); i != m_Sockets.end(); ++i) |
| 2132 |
{ |
| 2133 |
CUDTSocket* s = i->second; |
| 2134 |
if (s->m_GroupOf == g) |
| 2135 |
{ |
| 2136 |
HLOGC(smlog.Debug, log << "deleteGroup: IPE: existing @" << s->m_SocketID << " points to a dead group!"); |
| 2137 |
s->m_GroupOf = NULL; |
| 2138 |
s->m_GroupMemberData = NULL; |
| 2139 |
} |
| 2140 |
} |
| 2141 |
|
| 2142 |
// Just in case, do it in closed sockets, too, although this should be |
| 2143 |
// always done before moving to it. |
| 2144 |
for (sockets_t::iterator i = m_ClosedSockets.begin(); i != m_ClosedSockets.end(); ++i) |
| 2145 |
{ |
| 2146 |
CUDTSocket* s = i->second; |
| 2147 |
if (s->m_GroupOf == g) |
| 2148 |
{ |
| 2149 |
HLOGC(smlog.Debug, log << "deleteGroup: IPE: closed @" << s->m_SocketID << " points to a dead group!"); |
| 2150 |
s->m_GroupOf = NULL; |
| 2151 |
s->m_GroupMemberData = NULL; |
| 2152 |
} |
| 2153 |
} |
| 2154 |
} |
| 2155 |
#endif |
| 2156 |
|
| 2157 |
int srt::CUDTUnited::close(CUDTSocket* s) |
| 2158 |
{ |
| 2159 |
// Set the closing flag BEFORE you attempt to acquire |
| 2160 |
s->setBreaking(); |
| 2161 |
|
| 2162 |
HLOGC(smlog.Debug, log << s->core().CONID() << "CLOSE. Acquiring control lock"); |
| 2163 |
ScopedLock socket_cg(s->m_ControlLock); |
| 2164 |
HLOGC(smlog.Debug, log << s->core().CONID() << "CLOSING (removing from listening, closing CUDT)"); |
| 2165 |
|
| 2166 |
const bool synch_close_snd = s->core().m_config.bSynSending; |
| 2167 |
|
| 2168 |
SRTSOCKET u = s->m_SocketID; |
| 2169 |
|
| 2170 |
if (s->m_Status == SRTS_LISTENING) |
| 2171 |
{ |
| 2172 |
if (s->core().m_bBroken) |
| 2173 |
return 0; |
| 2174 |
|
| 2175 |
s->m_tsClosureTimeStamp = steady_clock::now(); |
| 2176 |
s->core().m_bBroken = true; |
| 2177 |
|
| 2178 |
// Change towards original UDT: |
| 2179 |
// Leave all the closing activities for garbageCollect to happen, |
| 2180 |
// however remove the listener from the RcvQueue IMMEDIATELY. |
| 2181 |
// Even though garbageCollect would eventually remove the listener |
| 2182 |
// as well, there would be some time interval between now and the |
| 2183 |
// moment when it's done, and during this time the application will |
| 2184 |
// be unable to bind to this port that the about-to-delete listener |
| 2185 |
// is currently occupying (due to blocked slot in the RcvQueue). |
| 2186 |
|
| 2187 |
HLOGC(smlog.Debug, log << s->core().CONID() << "CLOSING (removing listener immediately)"); |
| 2188 |
s->core().notListening(); |
| 2189 |
s->m_Status = SRTS_CLOSING; |
| 2190 |
|
| 2191 |
// broadcast all "accept" waiting |
| 2192 |
CSync::lock_notify_all(s->m_AcceptCond, s->m_AcceptLock); |
| 2193 |
} |
| 2194 |
else |
| 2195 |
{ |
| 2196 |
s->m_Status = SRTS_CLOSING; |
| 2197 |
// Note: this call may be done on a socket that hasn't finished |
| 2198 |
// sending all packets scheduled for sending, which means, this call |
| 2199 |
// may block INDEFINITELY. As long as it's acceptable to block the |
| 2200 |
// call to srt_close(), and all functions in all threads where this |
| 2201 |
// very socket is used, this shall not block the central database. |
| 2202 |
s->core().closeInternal(); |
| 2203 |
|
| 2204 |
// synchronize with garbage collection. |
| 2205 |
HLOGC(smlog.Debug, |
| 2206 |
log << "@" << u << "U::close done. GLOBAL CLOSE: " << s->core().CONID() |
| 2207 |
<< "Acquiring GLOBAL control lock"); |
| 2208 |
ExclusiveLock manager_cg(m_GlobControlLock); |
| 2209 |
// since "s" is located before m_GlobControlLock, locate it again in case |
| 2210 |
// it became invalid |
| 2211 |
// XXX This is very weird; if we state that the CUDTSocket object |
| 2212 |
// could not be deleted between locks, then definitely it couldn't |
| 2213 |
// also change the pointer value. There's no other reason for getting |
| 2214 |
// this iterator but to obtain the 's' pointer, which is impossible to |
| 2215 |
// be different than previous 's' (m_Sockets is a map that stores pointers |
| 2216 |
// transparently). This iterator isn't even later used to delete the socket |
| 2217 |
// from the container, though it would be more efficient. |
| 2218 |
// FURTHER RESEARCH REQUIRED. |
| 2219 |
sockets_t::iterator i = m_Sockets.find(u); |
| 2220 |
if ((i == m_Sockets.end()) || (i->second->m_Status == SRTS_CLOSED)) |
| 2221 |
{ |
| 2222 |
HLOGC(smlog.Debug, log << "@" << u << "U::close: NOT AN ACTIVE SOCKET, returning."); |
| 2223 |
return 0; |
| 2224 |
} |
| 2225 |
s = i->second; |
| 2226 |
s->setClosed(); |
| 2227 |
|
| 2228 |
#if ENABLE_BONDING |
| 2229 |
if (s->m_GroupOf) |
| 2230 |
{ |
| 2231 |
HLOGC(smlog.Debug, |
| 2232 |
log << "@" << s->m_SocketID << " IS MEMBER OF $" << s->m_GroupOf->id() << " - REMOVING FROM GROUP"); |
| 2233 |
s->removeFromGroup(true); |
| 2234 |
} |
| 2235 |
#endif |
| 2236 |
|
| 2237 |
m_Sockets.erase(s->m_SocketID); |
| 2238 |
m_ClosedSockets[s->m_SocketID] = s; |
| 2239 |
HLOGC(smlog.Debug, log << "@" << u << "U::close: Socket MOVED TO CLOSED for collecting later."); |
| 2240 |
|
| 2241 |
CGlobEvent::triggerEvent(); |
| 2242 |
} |
| 2243 |
|
| 2244 |
HLOGC(smlog.Debug, log << "@" << u << ": GLOBAL: CLOSING DONE"); |
| 2245 |
|
| 2246 |
// Check if the ID is still in closed sockets before you access it |
| 2247 |
// (the last triggerEvent could have deleted it). |
| 2248 |
if (synch_close_snd) |
| 2249 |
{ |
| 2250 |
#if SRT_ENABLE_CLOSE_SYNCH |
| 2251 |
|
| 2252 |
HLOGC(smlog.Debug, log << "@" << u << " GLOBAL CLOSING: sync-waiting for releasing sender resources..."); |
| 2253 |
for (;;) |
| 2254 |
{ |
| 2255 |
CSndBuffer* sb = s->core().m_pSndBuffer; |
| 2256 |
|
| 2257 |
// Disconnected from buffer - nothing more to check. |
| 2258 |
if (!sb) |
| 2259 |
{ |
| 2260 |
HLOGC(smlog.Debug, |
| 2261 |
log << "@" << u << " GLOBAL CLOSING: sending buffer disconnected. Allowed to close."); |
| 2262 |
break; |
| 2263 |
} |
| 2264 |
|
| 2265 |
// Sender buffer empty |
| 2266 |
if (sb->getCurrBufSize() == 0) |
| 2267 |
{ |
| 2268 |
HLOGC(smlog.Debug, log << "@" << u << " GLOBAL CLOSING: sending buffer depleted. Allowed to close."); |
| 2269 |
break; |
| 2270 |
} |
| 2271 |
|
| 2272 |
// Ok, now you are keeping GC thread hands off the internal data. |
| 2273 |
// You can check then if it has already deleted the socket or not. |
| 2274 |
// The socket is either in m_ClosedSockets or is already gone. |
| 2275 |
|
| 2276 |
// Done the other way, but still done. You can stop waiting. |
| 2277 |
bool isgone = false; |
| 2278 |
{ |
| 2279 |
SharedLock manager_cg(m_GlobControlLock); |
| 2280 |
isgone = m_ClosedSockets.count(u) == 0; |
| 2281 |
} |
| 2282 |
if (!isgone) |
| 2283 |
{ |
| 2284 |
isgone = !s->core().m_bOpened; |
| 2285 |
} |
| 2286 |
if (isgone) |
| 2287 |
{ |
| 2288 |
HLOGC(smlog.Debug, |
| 2289 |
log << "@" << u << " GLOBAL CLOSING: ... gone in the meantime, whatever. Exiting close()."); |
| 2290 |
break; |
| 2291 |
} |
| 2292 |
|
| 2293 |
HLOGC(smlog.Debug, log << "@" << u << " GLOBAL CLOSING: ... still waiting for any update."); |
| 2294 |
// How to handle a possible error here? |
| 2295 |
CGlobEvent::waitForEvent(); |
| 2296 |
|
| 2297 |
// Continue waiting in case when an event happened or 1s waiting time passed for checkpoint. |
| 2298 |
} |
| 2299 |
#endif |
| 2300 |
} |
| 2301 |
|
| 2302 |
/* |
| 2303 |
This code is PUT ASIDE for now. |
| 2304 |
Most likely this will be never required. |
| 2305 |
It had to hold the closing activity until the time when the receiver buffer is depleted. |
| 2306 |
However the closing of the socket should only happen when the receiver has received |
| 2307 |
an information about that the reading is no longer possible (error report from recv/recvfile). |
| 2308 |
When this happens, the receiver buffer is definitely depleted already and there's no need to check |
| 2309 |
anything. |
| 2310 |
|
| 2311 |
Should there appear any other conditions in future under which the closing process should be |
| 2312 |
delayed until the receiver buffer is empty, this code can be filled here. |
| 2313 |
|
| 2314 |
if ( synch_close_rcv ) |
| 2315 |
{ |
| 2316 |
... |
| 2317 |
} |
| 2318 |
*/ |
| 2319 |
CSync::notify_one_relaxed(m_GCStopCond); |
| 2320 |
|
| 2321 |
return 0; |
| 2322 |
} |
| 2323 |
|
| 2324 |
void srt::CUDTUnited::getpeername(const SRTSOCKET u, sockaddr* pw_name, int* pw_namelen) |
| 2325 |
{ |
| 2326 |
if (!pw_name || !pw_namelen) |
| 2327 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 2328 |
|
| 2329 |
if (getStatus(u) != SRTS_CONNECTED) |
| 2330 |
throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0); |
| 2331 |
|
| 2332 |
CUDTSocket* s = locateSocket(u); |
| 2333 |
|
| 2334 |
if (!s) |
| 2335 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2336 |
|
| 2337 |
if (!s->core().m_bConnected || s->core().m_bBroken) |
| 2338 |
throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0); |
| 2339 |
|
| 2340 |
const int len = s->m_PeerAddr.size(); |
| 2341 |
if (*pw_namelen < len) |
| 2342 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 2343 |
|
| 2344 |
memcpy((pw_name), &s->m_PeerAddr.sa, len); |
| 2345 |
*pw_namelen = len; |
| 2346 |
} |
| 2347 |
|
| 2348 |
void srt::CUDTUnited::getsockname(const SRTSOCKET u, sockaddr* pw_name, int* pw_namelen) |
| 2349 |
{ |
| 2350 |
if (!pw_name || !pw_namelen) |
| 2351 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 2352 |
|
| 2353 |
CUDTSocket* s = locateSocket(u); |
| 2354 |
|
| 2355 |
if (!s) |
| 2356 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2357 |
|
| 2358 |
if (s->core().m_bBroken) |
| 2359 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2360 |
|
| 2361 |
if (s->m_Status == SRTS_INIT) |
| 2362 |
throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0); |
| 2363 |
|
| 2364 |
const int len = s->m_SelfAddr.size(); |
| 2365 |
if (*pw_namelen < len) |
| 2366 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 2367 |
|
| 2368 |
memcpy((pw_name), &s->m_SelfAddr.sa, len); |
| 2369 |
*pw_namelen = len; |
| 2370 |
} |
| 2371 |
|
| 2372 |
int srt::CUDTUnited::select(UDT::UDSET* readfds, UDT::UDSET* writefds, UDT::UDSET* exceptfds, const timeval* timeout) |
| 2373 |
{ |
| 2374 |
const steady_clock::time_point entertime = steady_clock::now(); |
| 2375 |
|
| 2376 |
const int64_t timeo_us = timeout ? static_cast<int64_t>(timeout->tv_sec) * 1000000 + timeout->tv_usec : -1; |
| 2377 |
const steady_clock::duration timeo(microseconds_from(timeo_us)); |
| 2378 |
|
| 2379 |
// initialize results |
| 2380 |
int count = 0; |
| 2381 |
set<SRTSOCKET> rs, ws, es; |
| 2382 |
|
| 2383 |
// retrieve related UDT sockets |
| 2384 |
vector<CUDTSocket*> ru, wu, eu; |
| 2385 |
CUDTSocket* s; |
| 2386 |
if (readfds) |
| 2387 |
for (set<SRTSOCKET>::iterator i1 = readfds->begin(); i1 != readfds->end(); ++i1) |
| 2388 |
{ |
| 2389 |
if (getStatus(*i1) == SRTS_BROKEN) |
| 2390 |
{ |
| 2391 |
rs.insert(*i1); |
| 2392 |
++count; |
| 2393 |
} |
| 2394 |
else if (!(s = locateSocket(*i1))) |
| 2395 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2396 |
else |
| 2397 |
ru.push_back(s); |
| 2398 |
} |
| 2399 |
if (writefds) |
| 2400 |
for (set<SRTSOCKET>::iterator i2 = writefds->begin(); i2 != writefds->end(); ++i2) |
| 2401 |
{ |
| 2402 |
if (getStatus(*i2) == SRTS_BROKEN) |
| 2403 |
{ |
| 2404 |
ws.insert(*i2); |
| 2405 |
++count; |
| 2406 |
} |
| 2407 |
else if (!(s = locateSocket(*i2))) |
| 2408 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2409 |
else |
| 2410 |
wu.push_back(s); |
| 2411 |
} |
| 2412 |
if (exceptfds) |
| 2413 |
for (set<SRTSOCKET>::iterator i3 = exceptfds->begin(); i3 != exceptfds->end(); ++i3) |
| 2414 |
{ |
| 2415 |
if (getStatus(*i3) == SRTS_BROKEN) |
| 2416 |
{ |
| 2417 |
es.insert(*i3); |
| 2418 |
++count; |
| 2419 |
} |
| 2420 |
else if (!(s = locateSocket(*i3))) |
| 2421 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2422 |
else |
| 2423 |
eu.push_back(s); |
| 2424 |
} |
| 2425 |
|
| 2426 |
do |
| 2427 |
{ |
| 2428 |
// query read sockets |
| 2429 |
for (vector<CUDTSocket*>::iterator j1 = ru.begin(); j1 != ru.end(); ++j1) |
| 2430 |
{ |
| 2431 |
s = *j1; |
| 2432 |
|
| 2433 |
if (s->readReady() || s->m_Status == SRTS_CLOSED) |
| 2434 |
{ |
| 2435 |
rs.insert(s->m_SocketID); |
| 2436 |
++count; |
| 2437 |
} |
| 2438 |
} |
| 2439 |
|
| 2440 |
// query write sockets |
| 2441 |
for (vector<CUDTSocket*>::iterator j2 = wu.begin(); j2 != wu.end(); ++j2) |
| 2442 |
{ |
| 2443 |
s = *j2; |
| 2444 |
|
| 2445 |
if (s->writeReady() || s->m_Status == SRTS_CLOSED) |
| 2446 |
{ |
| 2447 |
ws.insert(s->m_SocketID); |
| 2448 |
++count; |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
// query exceptions on sockets |
| 2453 |
for (vector<CUDTSocket*>::iterator j3 = eu.begin(); j3 != eu.end(); ++j3) |
| 2454 |
{ |
| 2455 |
// check connection request status, not supported now |
| 2456 |
} |
| 2457 |
|
| 2458 |
if (0 < count) |
| 2459 |
break; |
| 2460 |
|
| 2461 |
CGlobEvent::waitForEvent(); |
| 2462 |
} while (timeo > steady_clock::now() - entertime); |
| 2463 |
|
| 2464 |
if (readfds) |
| 2465 |
*readfds = rs; |
| 2466 |
|
| 2467 |
if (writefds) |
| 2468 |
*writefds = ws; |
| 2469 |
|
| 2470 |
if (exceptfds) |
| 2471 |
*exceptfds = es; |
| 2472 |
|
| 2473 |
return count; |
| 2474 |
} |
| 2475 |
|
| 2476 |
int srt::CUDTUnited::selectEx(const vector<SRTSOCKET>& fds, |
| 2477 |
vector<SRTSOCKET>* readfds, |
| 2478 |
vector<SRTSOCKET>* writefds, |
| 2479 |
vector<SRTSOCKET>* exceptfds, |
| 2480 |
int64_t msTimeOut) |
| 2481 |
{ |
| 2482 |
const steady_clock::time_point entertime = steady_clock::now(); |
| 2483 |
|
| 2484 |
const int64_t timeo_us = msTimeOut >= 0 ? msTimeOut * 1000 : -1; |
| 2485 |
const steady_clock::duration timeo(microseconds_from(timeo_us)); |
| 2486 |
|
| 2487 |
// initialize results |
| 2488 |
int count = 0; |
| 2489 |
if (readfds) |
| 2490 |
readfds->clear(); |
| 2491 |
if (writefds) |
| 2492 |
writefds->clear(); |
| 2493 |
if (exceptfds) |
| 2494 |
exceptfds->clear(); |
| 2495 |
|
| 2496 |
do |
| 2497 |
{ |
| 2498 |
for (vector<SRTSOCKET>::const_iterator i = fds.begin(); i != fds.end(); ++i) |
| 2499 |
{ |
| 2500 |
CUDTSocket* s = locateSocket(*i); |
| 2501 |
|
| 2502 |
if ((!s) || s->core().m_bBroken || (s->m_Status == SRTS_CLOSED)) |
| 2503 |
{ |
| 2504 |
if (exceptfds) |
| 2505 |
{ |
| 2506 |
exceptfds->push_back(*i); |
| 2507 |
++count; |
| 2508 |
} |
| 2509 |
continue; |
| 2510 |
} |
| 2511 |
|
| 2512 |
if (readfds) |
| 2513 |
{ |
| 2514 |
if ((s->core().m_bConnected && s->core().m_pRcvBuffer->isRcvDataReady()) || |
| 2515 |
(s->core().m_bListening && (s->m_QueuedSockets.size() > 0))) |
| 2516 |
{ |
| 2517 |
readfds->push_back(s->m_SocketID); |
| 2518 |
++count; |
| 2519 |
} |
| 2520 |
} |
| 2521 |
|
| 2522 |
if (writefds) |
| 2523 |
{ |
| 2524 |
if (s->core().m_bConnected && |
| 2525 |
(s->core().m_pSndBuffer->getCurrBufSize() < s->core().m_config.iSndBufSize)) |
| 2526 |
{ |
| 2527 |
writefds->push_back(s->m_SocketID); |
| 2528 |
++count; |
| 2529 |
} |
| 2530 |
} |
| 2531 |
} |
| 2532 |
|
| 2533 |
if (count > 0) |
| 2534 |
break; |
| 2535 |
|
| 2536 |
CGlobEvent::waitForEvent(); |
| 2537 |
} while (timeo > steady_clock::now() - entertime); |
| 2538 |
|
| 2539 |
return count; |
| 2540 |
} |
| 2541 |
|
| 2542 |
int srt::CUDTUnited::epoll_create() |
| 2543 |
{ |
| 2544 |
return m_EPoll.create(); |
| 2545 |
} |
| 2546 |
|
| 2547 |
int srt::CUDTUnited::epoll_clear_usocks(int eid) |
| 2548 |
{ |
| 2549 |
return m_EPoll.clear_usocks(eid); |
| 2550 |
} |
| 2551 |
|
| 2552 |
int srt::CUDTUnited::epoll_add_usock(const int eid, const SRTSOCKET u, const int* events) |
| 2553 |
{ |
| 2554 |
int ret = -1; |
| 2555 |
#if ENABLE_BONDING |
| 2556 |
if (u & SRTGROUP_MASK) |
| 2557 |
{ |
| 2558 |
GroupKeeper k(*this, u, ERH_THROW); |
| 2559 |
ret = m_EPoll.update_usock(eid, u, events); |
| 2560 |
k.group->addEPoll(eid); |
| 2561 |
return 0; |
| 2562 |
} |
| 2563 |
#endif |
| 2564 |
|
| 2565 |
CUDTSocket* s = locateSocket(u); |
| 2566 |
if (s) |
| 2567 |
{ |
| 2568 |
ret = epoll_add_usock_INTERNAL(eid, s, events); |
| 2569 |
} |
| 2570 |
else |
| 2571 |
{ |
| 2572 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL); |
| 2573 |
} |
| 2574 |
|
| 2575 |
return ret; |
| 2576 |
} |
| 2577 |
|
| 2578 |
// NOTE: WILL LOCK (serially): |
| 2579 |
// - CEPoll::m_EPollLock |
| 2580 |
// - CUDT::m_RecvLock |
| 2581 |
int srt::CUDTUnited::epoll_add_usock_INTERNAL(const int eid, CUDTSocket* s, const int* events) |
| 2582 |
{ |
| 2583 |
int ret = m_EPoll.update_usock(eid, s->m_SocketID, events); |
| 2584 |
s->core().addEPoll(eid); |
| 2585 |
return ret; |
| 2586 |
} |
| 2587 |
|
| 2588 |
int srt::CUDTUnited::epoll_add_ssock(const int eid, const SYSSOCKET s, const int* events) |
| 2589 |
{ |
| 2590 |
return m_EPoll.add_ssock(eid, s, events); |
| 2591 |
} |
| 2592 |
|
| 2593 |
int srt::CUDTUnited::epoll_update_ssock(const int eid, const SYSSOCKET s, const int* events) |
| 2594 |
{ |
| 2595 |
return m_EPoll.update_ssock(eid, s, events); |
| 2596 |
} |
| 2597 |
|
| 2598 |
template <class EntityType> |
| 2599 |
int srt::CUDTUnited::epoll_remove_entity(const int eid, EntityType* ent) |
| 2600 |
{ |
| 2601 |
// XXX Not sure if this is anyhow necessary because setting readiness |
| 2602 |
// to false doesn't actually trigger any action. Further research needed. |
| 2603 |
HLOGC(ealog.Debug, log << "epoll_remove_usock: CLEARING readiness on E" << eid << " of @" << ent->id()); |
| 2604 |
ent->removeEPollEvents(eid); |
| 2605 |
|
| 2606 |
// First remove the EID from the subscribed in the socket so that |
| 2607 |
// a possible call to update_events: |
| 2608 |
// - if happens before this call, can find the epoll bit update possible |
| 2609 |
// - if happens after this call, will not strike this EID |
| 2610 |
HLOGC(ealog.Debug, log << "epoll_remove_usock: REMOVING E" << eid << " from back-subscirbers in @" << ent->id()); |
| 2611 |
ent->removeEPollID(eid); |
| 2612 |
|
| 2613 |
HLOGC(ealog.Debug, log << "epoll_remove_usock: CLEARING subscription on E" << eid << " of @" << ent->id()); |
| 2614 |
int no_events = 0; |
| 2615 |
int ret = m_EPoll.update_usock(eid, ent->id(), &no_events); |
| 2616 |
|
| 2617 |
return ret; |
| 2618 |
} |
| 2619 |
|
| 2620 |
// Needed internal access! |
| 2621 |
int srt::CUDTUnited::epoll_remove_socket_INTERNAL(const int eid, CUDTSocket* s) |
| 2622 |
{ |
| 2623 |
return epoll_remove_entity(eid, &s->core()); |
| 2624 |
} |
| 2625 |
|
| 2626 |
#if ENABLE_BONDING |
| 2627 |
int srt::CUDTUnited::epoll_remove_group_INTERNAL(const int eid, CUDTGroup* g) |
| 2628 |
{ |
| 2629 |
return epoll_remove_entity(eid, g); |
| 2630 |
} |
| 2631 |
#endif |
| 2632 |
|
| 2633 |
int srt::CUDTUnited::epoll_remove_usock(const int eid, const SRTSOCKET u) |
| 2634 |
{ |
| 2635 |
CUDTSocket* s = 0; |
| 2636 |
|
| 2637 |
#if ENABLE_BONDING |
| 2638 |
CUDTGroup* g = 0; |
| 2639 |
if (u & SRTGROUP_MASK) |
| 2640 |
{ |
| 2641 |
GroupKeeper k(*this, u, ERH_THROW); |
| 2642 |
g = k.group; |
| 2643 |
return epoll_remove_entity(eid, g); |
| 2644 |
} |
| 2645 |
else |
| 2646 |
#endif |
| 2647 |
{ |
| 2648 |
s = locateSocket(u); |
| 2649 |
if (s) |
| 2650 |
return epoll_remove_entity(eid, &s->core()); |
| 2651 |
} |
| 2652 |
|
| 2653 |
LOGC(ealog.Error, |
| 2654 |
log << "remove_usock: @" << u << " not found as either socket or group. Removing only from epoll system."); |
| 2655 |
int no_events = 0; |
| 2656 |
return m_EPoll.update_usock(eid, u, &no_events); |
| 2657 |
} |
| 2658 |
|
| 2659 |
int srt::CUDTUnited::epoll_remove_ssock(const int eid, const SYSSOCKET s) |
| 2660 |
{ |
| 2661 |
return m_EPoll.remove_ssock(eid, s); |
| 2662 |
} |
| 2663 |
|
| 2664 |
int srt::CUDTUnited::epoll_uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut) |
| 2665 |
{ |
| 2666 |
return m_EPoll.uwait(eid, fdsSet, fdsSize, msTimeOut); |
| 2667 |
} |
| 2668 |
|
| 2669 |
int32_t srt::CUDTUnited::epoll_set(int eid, int32_t flags) |
| 2670 |
{ |
| 2671 |
return m_EPoll.setflags(eid, flags); |
| 2672 |
} |
| 2673 |
|
| 2674 |
int srt::CUDTUnited::epoll_release(const int eid) |
| 2675 |
{ |
| 2676 |
return m_EPoll.release(eid); |
| 2677 |
} |
| 2678 |
|
| 2679 |
srt::CUDTSocket* srt::CUDTUnited::locateSocket(const SRTSOCKET u, ErrorHandling erh) |
| 2680 |
{ |
| 2681 |
SharedLock cg(m_GlobControlLock); |
| 2682 |
CUDTSocket* s = locateSocket_LOCKED(u); |
| 2683 |
if (!s) |
| 2684 |
{ |
| 2685 |
if (erh == ERH_RETURN) |
| 2686 |
return NULL; |
| 2687 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2688 |
} |
| 2689 |
|
| 2690 |
return s; |
| 2691 |
} |
| 2692 |
|
| 2693 |
// [[using locked(m_GlobControlLock)]]; |
| 2694 |
srt::CUDTSocket* srt::CUDTUnited::locateSocket_LOCKED(SRTSOCKET u) |
| 2695 |
{ |
| 2696 |
sockets_t::iterator i = m_Sockets.find(u); |
| 2697 |
|
| 2698 |
if ((i == m_Sockets.end()) || (i->second->m_Status == SRTS_CLOSED)) |
| 2699 |
{ |
| 2700 |
return NULL; |
| 2701 |
} |
| 2702 |
|
| 2703 |
return i->second; |
| 2704 |
} |
| 2705 |
|
| 2706 |
#if ENABLE_BONDING |
| 2707 |
srt::CUDTGroup* srt::CUDTUnited::locateAcquireGroup(SRTSOCKET u, ErrorHandling erh) |
| 2708 |
{ |
| 2709 |
SharedLock cg(m_GlobControlLock); |
| 2710 |
|
| 2711 |
const groups_t::iterator i = m_Groups.find(u); |
| 2712 |
if (i == m_Groups.end()) |
| 2713 |
{ |
| 2714 |
if (erh == ERH_THROW) |
| 2715 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2716 |
return NULL; |
| 2717 |
} |
| 2718 |
|
| 2719 |
ScopedLock cgroup(*i->second->exp_groupLock()); |
| 2720 |
i->second->apiAcquire(); |
| 2721 |
return i->second; |
| 2722 |
} |
| 2723 |
|
| 2724 |
srt::CUDTGroup* srt::CUDTUnited::acquireSocketsGroup(CUDTSocket* s) |
| 2725 |
{ |
| 2726 |
SharedLock cg(m_GlobControlLock); |
| 2727 |
CUDTGroup* g = s->m_GroupOf; |
| 2728 |
if (!g) |
| 2729 |
return NULL; |
| 2730 |
|
| 2731 |
// With m_GlobControlLock locked, we are sure the group |
| 2732 |
// still exists, if it wasn't removed from this socket. |
| 2733 |
g->apiAcquire(); |
| 2734 |
return g; |
| 2735 |
} |
| 2736 |
#endif |
| 2737 |
|
| 2738 |
srt::CUDTSocket* srt::CUDTUnited::locateAcquireSocket(SRTSOCKET u, ErrorHandling erh) |
| 2739 |
{ |
| 2740 |
SharedLock cg(m_GlobControlLock); |
| 2741 |
|
| 2742 |
CUDTSocket* s = locateSocket_LOCKED(u); |
| 2743 |
if (!s) |
| 2744 |
{ |
| 2745 |
if (erh == ERH_THROW) |
| 2746 |
throw CUDTException(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 2747 |
return NULL; |
| 2748 |
} |
| 2749 |
|
| 2750 |
s->apiAcquire(); |
| 2751 |
return s; |
| 2752 |
} |
| 2753 |
|
| 2754 |
bool srt::CUDTUnited::acquireSocket(CUDTSocket* s) |
| 2755 |
{ |
| 2756 |
// Note that before using this function you must be certain |
| 2757 |
// that the socket isn't broken already and it still has at least |
| 2758 |
// one more GC cycle to live. In other words, you must be certain |
| 2759 |
// that this pointer passed here isn't dangling and was obtained |
| 2760 |
// directly from m_Sockets, or even better, has been acquired |
| 2761 |
// by some other functionality already, which is only about to |
| 2762 |
// be released earlier than you need. |
| 2763 |
SharedLock cg(m_GlobControlLock); |
| 2764 |
s->apiAcquire(); |
| 2765 |
// Keep the lock so that no one changes anything in the meantime. |
| 2766 |
// If the socket m_Status == SRTS_CLOSED (set by setClosed()), then |
| 2767 |
// this socket is no longer present in the m_Sockets container |
| 2768 |
if (s->m_Status >= SRTS_BROKEN) |
| 2769 |
{ |
| 2770 |
s->apiRelease(); |
| 2771 |
return false; |
| 2772 |
} |
| 2773 |
|
| 2774 |
return true; |
| 2775 |
} |
| 2776 |
|
| 2777 |
srt::CUDTSocket* srt::CUDTUnited::locatePeer(const sockaddr_any& peer, const SRTSOCKET id, int32_t isn) |
| 2778 |
{ |
| 2779 |
SharedLock cg(m_GlobControlLock); |
| 2780 |
|
| 2781 |
map<int64_t, set<SRTSOCKET> >::iterator i = m_PeerRec.find(CUDTSocket::getPeerSpec(id, isn)); |
| 2782 |
if (i == m_PeerRec.end()) |
| 2783 |
return NULL; |
| 2784 |
|
| 2785 |
for (set<SRTSOCKET>::iterator j = i->second.begin(); j != i->second.end(); ++j) |
| 2786 |
{ |
| 2787 |
sockets_t::iterator k = m_Sockets.find(*j); |
| 2788 |
// this socket might have been closed and moved m_ClosedSockets |
| 2789 |
if (k == m_Sockets.end()) |
| 2790 |
continue; |
| 2791 |
|
| 2792 |
if (k->second->m_PeerAddr == peer) |
| 2793 |
{ |
| 2794 |
return k->second; |
| 2795 |
} |
| 2796 |
} |
| 2797 |
|
| 2798 |
return NULL; |
| 2799 |
} |
| 2800 |
|
| 2801 |
void srt::CUDTUnited::checkBrokenSockets() |
| 2802 |
{ |
| 2803 |
ExclusiveLock cg(m_GlobControlLock); |
| 2804 |
|
| 2805 |
#if ENABLE_BONDING |
| 2806 |
vector<SRTSOCKET> delgids; |
| 2807 |
|
| 2808 |
for (groups_t::iterator i = m_ClosedGroups.begin(); i != m_ClosedGroups.end(); ++i) |
| 2809 |
{ |
| 2810 |
// isStillBusy requires lock on the group, so only after an API |
| 2811 |
// function that uses it returns, and so clears the busy flag, |
| 2812 |
// a new API function won't be called anyway until it can acquire |
| 2813 |
// GlobControlLock, and all functions that have already seen this |
| 2814 |
// group as closing will not continue with the API and return. |
| 2815 |
// If we caught some API function still using the closed group, |
| 2816 |
// it's not going to wait, will be checked next time. |
| 2817 |
if (i->second->isStillBusy()) |
| 2818 |
continue; |
| 2819 |
|
| 2820 |
delgids.push_back(i->first); |
| 2821 |
delete i->second; |
| 2822 |
i->second = NULL; // just for a case, avoid a dangling pointer |
| 2823 |
} |
| 2824 |
|
| 2825 |
for (vector<SRTSOCKET>::iterator di = delgids.begin(); di != delgids.end(); ++di) |
| 2826 |
{ |
| 2827 |
m_ClosedGroups.erase(*di); |
| 2828 |
} |
| 2829 |
#endif |
| 2830 |
|
| 2831 |
// set of sockets To Be Closed and To Be Removed |
| 2832 |
vector<SRTSOCKET> tbc; |
| 2833 |
vector<SRTSOCKET> tbr; |
| 2834 |
|
| 2835 |
for (sockets_t::iterator i = m_Sockets.begin(); i != m_Sockets.end(); ++i) |
| 2836 |
{ |
| 2837 |
CUDTSocket* s = i->second; |
| 2838 |
if (!s->core().m_bBroken) |
| 2839 |
continue; |
| 2840 |
|
| 2841 |
if (s->m_Status == SRTS_LISTENING) |
| 2842 |
{ |
| 2843 |
const steady_clock::duration elapsed = steady_clock::now() - s->m_tsClosureTimeStamp.load(); |
| 2844 |
// A listening socket should wait an extra 3 seconds |
| 2845 |
// in case a client is connecting. |
| 2846 |
if (elapsed < milliseconds_from(CUDT::COMM_CLOSE_BROKEN_LISTENER_TIMEOUT_MS)) |
| 2847 |
continue; |
| 2848 |
} |
| 2849 |
else |
| 2850 |
{ |
| 2851 |
CUDT& u = s->core(); |
| 2852 |
|
| 2853 |
enterCS(u.m_RcvBufferLock); |
| 2854 |
bool has_avail_packets = u.m_pRcvBuffer && u.m_pRcvBuffer->hasAvailablePackets(); |
| 2855 |
leaveCS(u.m_RcvBufferLock); |
| 2856 |
|
| 2857 |
if (has_avail_packets) |
| 2858 |
{ |
| 2859 |
const int bc = u.m_iBrokenCounter.load(); |
| 2860 |
if (bc > 0) |
| 2861 |
{ |
| 2862 |
// if there is still data in the receiver buffer, wait longer |
| 2863 |
s->core().m_iBrokenCounter.store(bc - 1); |
| 2864 |
continue; |
| 2865 |
} |
| 2866 |
} |
| 2867 |
} |
| 2868 |
|
| 2869 |
#if ENABLE_BONDING |
| 2870 |
if (s->m_GroupOf) |
| 2871 |
{ |
| 2872 |
HLOGC(smlog.Debug, |
| 2873 |
log << "@" << s->m_SocketID << " IS MEMBER OF $" << s->m_GroupOf->id() << " - REMOVING FROM GROUP"); |
| 2874 |
s->removeFromGroup(true); |
| 2875 |
} |
| 2876 |
#endif |
| 2877 |
|
| 2878 |
HLOGC(smlog.Debug, log << "checkBrokenSockets: moving BROKEN socket to CLOSED: @" << i->first); |
| 2879 |
|
| 2880 |
// close broken connections and start removal timer |
| 2881 |
s->setClosed(); |
| 2882 |
tbc.push_back(i->first); |
| 2883 |
m_ClosedSockets[i->first] = s; |
| 2884 |
|
| 2885 |
// remove from listener's queue |
| 2886 |
sockets_t::iterator ls = m_Sockets.find(s->m_ListenSocket); |
| 2887 |
if (ls == m_Sockets.end()) |
| 2888 |
{ |
| 2889 |
ls = m_ClosedSockets.find(s->m_ListenSocket); |
| 2890 |
if (ls == m_ClosedSockets.end()) |
| 2891 |
continue; |
| 2892 |
} |
| 2893 |
|
| 2894 |
enterCS(ls->second->m_AcceptLock); |
| 2895 |
ls->second->m_QueuedSockets.erase(s->m_SocketID); |
| 2896 |
leaveCS(ls->second->m_AcceptLock); |
| 2897 |
} |
| 2898 |
|
| 2899 |
for (sockets_t::iterator j = m_ClosedSockets.begin(); j != m_ClosedSockets.end(); ++j) |
| 2900 |
{ |
| 2901 |
CUDTSocket* ps = j->second; |
| 2902 |
|
| 2903 |
// NOTE: There is still a hypothetical risk here that ps |
| 2904 |
// was made busy while the socket was already moved to m_ClosedSocket, |
| 2905 |
// if the socket was acquired through CUDTUnited::acquireSocket (that is, |
| 2906 |
// busy flag acquisition was done through the CUDTSocket* pointer rather |
| 2907 |
// than through the numeric ID). Therefore this way of busy acquisition |
| 2908 |
// should be done only if at the moment of acquisition there are certainly |
| 2909 |
// other conditions applying on the socket that prevent it from being deleted. |
| 2910 |
if (ps->isStillBusy()) |
| 2911 |
{ |
| 2912 |
HLOGC(smlog.Debug, log << "checkBrokenSockets: @" << ps->m_SocketID << " is still busy, SKIPPING THIS CYCLE."); |
| 2913 |
continue; |
| 2914 |
} |
| 2915 |
|
| 2916 |
CUDT& u = ps->core(); |
| 2917 |
|
| 2918 |
// HLOGC(smlog.Debug, log << "checking CLOSED socket: " << j->first); |
| 2919 |
if (!is_zero(u.m_tsLingerExpiration)) |
| 2920 |
{ |
| 2921 |
// asynchronous close: |
| 2922 |
if ((!u.m_pSndBuffer) || (0 == u.m_pSndBuffer->getCurrBufSize()) || |
| 2923 |
(u.m_tsLingerExpiration <= steady_clock::now())) |
| 2924 |
{ |
| 2925 |
HLOGC(smlog.Debug, log << "checkBrokenSockets: marking CLOSED qualified @" << ps->m_SocketID); |
| 2926 |
u.m_tsLingerExpiration = steady_clock::time_point(); |
| 2927 |
u.m_bClosing = true; |
| 2928 |
ps->m_tsClosureTimeStamp = steady_clock::now(); |
| 2929 |
} |
| 2930 |
} |
| 2931 |
|
| 2932 |
// timeout 1 second to destroy a socket AND it has been removed from |
| 2933 |
// RcvUList |
| 2934 |
const steady_clock::time_point now = steady_clock::now(); |
| 2935 |
const steady_clock::duration closed_ago = now - ps->m_tsClosureTimeStamp.load(); |
| 2936 |
if (closed_ago > seconds_from(1)) |
| 2937 |
{ |
| 2938 |
CRNode* rnode = u.m_pRNode; |
| 2939 |
if (!rnode || !rnode->m_bOnList) |
| 2940 |
{ |
| 2941 |
HLOGC(smlog.Debug, |
| 2942 |
log << "checkBrokenSockets: @" << ps->m_SocketID << " closed " |
| 2943 |
<< FormatDuration(closed_ago) << " ago and removed from RcvQ - will remove"); |
| 2944 |
|
| 2945 |
// HLOGC(smlog.Debug, log << "will unref socket: " << j->first); |
| 2946 |
tbr.push_back(j->first); |
| 2947 |
} |
| 2948 |
} |
| 2949 |
} |
| 2950 |
|
| 2951 |
// move closed sockets to the ClosedSockets structure |
| 2952 |
for (vector<SRTSOCKET>::iterator k = tbc.begin(); k != tbc.end(); ++k) |
| 2953 |
m_Sockets.erase(*k); |
| 2954 |
|
| 2955 |
// remove those timeout sockets |
| 2956 |
for (vector<SRTSOCKET>::iterator l = tbr.begin(); l != tbr.end(); ++l) |
| 2957 |
removeSocket(*l); |
| 2958 |
|
| 2959 |
HLOGC(smlog.Debug, log << "checkBrokenSockets: after removal: m_ClosedSockets.size()=" << m_ClosedSockets.size()); |
| 2960 |
} |
| 2961 |
|
| 2962 |
// [[using locked(m_GlobControlLock)]] |
| 2963 |
void srt::CUDTUnited::removeSocket(const SRTSOCKET u) |
| 2964 |
{ |
| 2965 |
sockets_t::iterator i = m_ClosedSockets.find(u); |
| 2966 |
|
| 2967 |
// invalid socket ID |
| 2968 |
if (i == m_ClosedSockets.end()) |
| 2969 |
return; |
| 2970 |
|
| 2971 |
CUDTSocket* const s = i->second; |
| 2972 |
|
| 2973 |
// The socket may be in the trashcan now, but could |
| 2974 |
// still be under processing in the sender/receiver worker |
| 2975 |
// threads. If that's the case, SKIP IT THIS TIME. The |
| 2976 |
// socket will be checked next time the GC rollover starts. |
| 2977 |
CSNode* sn = s->core().m_pSNode; |
| 2978 |
if (sn && sn->m_iHeapLoc != -1) |
| 2979 |
return; |
| 2980 |
|
| 2981 |
CRNode* rn = s->core().m_pRNode; |
| 2982 |
if (rn && rn->m_bOnList) |
| 2983 |
return; |
| 2984 |
|
| 2985 |
if (s->isStillBusy()) |
| 2986 |
{ |
| 2987 |
HLOGC(smlog.Debug, log << "@" << s->m_SocketID << " is still busy, NOT deleting"); |
| 2988 |
return; |
| 2989 |
} |
| 2990 |
|
| 2991 |
HLOGC(smlog.Note, log << "@" << s->m_SocketID << " busy=" << s->isStillBusy()); |
| 2992 |
|
| 2993 |
#if ENABLE_BONDING |
| 2994 |
if (s->m_GroupOf) |
| 2995 |
{ |
| 2996 |
HLOGC(smlog.Debug, |
| 2997 |
log << "@" << s->m_SocketID << " IS MEMBER OF $" << s->m_GroupOf->id() << " - REMOVING FROM GROUP"); |
| 2998 |
s->removeFromGroup(true); |
| 2999 |
} |
| 3000 |
#endif |
| 3001 |
// decrease multiplexer reference count, and remove it if necessary |
| 3002 |
const int mid = s->m_iMuxID; |
| 3003 |
|
| 3004 |
{ |
| 3005 |
ScopedLock cg(s->m_AcceptLock); |
| 3006 |
|
| 3007 |
// if it is a listener, close all un-accepted sockets in its queue |
| 3008 |
// and remove them later |
| 3009 |
for (map<SRTSOCKET, sockaddr_any>::iterator q = s->m_QueuedSockets.begin(); |
| 3010 |
q != s->m_QueuedSockets.end(); ++ q) |
| 3011 |
{ |
| 3012 |
sockets_t::iterator si = m_Sockets.find(q->first); |
| 3013 |
if (si == m_Sockets.end()) |
| 3014 |
{ |
| 3015 |
// gone in the meantime |
| 3016 |
LOGC(smlog.Error, |
| 3017 |
log << "removeSocket: IPE? socket @" << (q->first) << " being queued for listener socket @" |
| 3018 |
<< s->m_SocketID << " is GONE in the meantime ???"); |
| 3019 |
continue; |
| 3020 |
} |
| 3021 |
|
| 3022 |
CUDTSocket* as = si->second; |
| 3023 |
|
| 3024 |
as->breakSocket_LOCKED(); |
| 3025 |
m_ClosedSockets[q->first] = as; |
| 3026 |
m_Sockets.erase(q->first); |
| 3027 |
} |
| 3028 |
} |
| 3029 |
|
| 3030 |
// remove from peer rec |
| 3031 |
map<int64_t, set<SRTSOCKET> >::iterator j = m_PeerRec.find(s->getPeerSpec()); |
| 3032 |
if (j != m_PeerRec.end()) |
| 3033 |
{ |
| 3034 |
j->second.erase(u); |
| 3035 |
if (j->second.empty()) |
| 3036 |
m_PeerRec.erase(j); |
| 3037 |
} |
| 3038 |
|
| 3039 |
/* |
| 3040 |
* Socket may be deleted while still having ePoll events set that would |
| 3041 |
* remains forever causing epoll_wait to unblock continuously for inexistent |
| 3042 |
* sockets. Get rid of all events for this socket. |
| 3043 |
*/ |
| 3044 |
m_EPoll.update_events(u, s->core().m_sPollID, SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR, false); |
| 3045 |
|
| 3046 |
// delete this one |
| 3047 |
m_ClosedSockets.erase(i); |
| 3048 |
|
| 3049 |
// XXX This below section can unlock m_GlobControlLock |
| 3050 |
// just for calling CUDT::closeInternal(), which is needed |
| 3051 |
// to avoid locking m_ConnectionLock after m_GlobControlLock, |
| 3052 |
// while m_ConnectionLock orders BEFORE m_GlobControlLock. |
| 3053 |
// This should be perfectly safe thing to do after the socket |
| 3054 |
// ID has been erased from m_ClosedSockets. No container access |
| 3055 |
// is done in this case. |
| 3056 |
// |
| 3057 |
// Report: P04-1.28, P04-2.27, P04-2.50, P04-2.55 |
| 3058 |
|
| 3059 |
HLOGC(smlog.Debug, log << "GC/removeSocket: closing associated UDT @" << u); |
| 3060 |
leaveCS(m_GlobControlLock); |
| 3061 |
s->core().closeInternal(); |
| 3062 |
enterCS(m_GlobControlLock); |
| 3063 |
HLOGC(smlog.Debug, log << "GC/removeSocket: DELETING SOCKET @" << u); |
| 3064 |
delete s; |
| 3065 |
HLOGC(smlog.Debug, log << "GC/removeSocket: socket @" << u << " DELETED. Checking muxer."); |
| 3066 |
|
| 3067 |
if (mid == -1) |
| 3068 |
{ |
| 3069 |
HLOGC(smlog.Debug, log << "GC/removeSocket: no muxer found, finishing."); |
| 3070 |
return; |
| 3071 |
} |
| 3072 |
|
| 3073 |
map<int, CMultiplexer>::iterator m; |
| 3074 |
m = m_mMultiplexer.find(mid); |
| 3075 |
if (m == m_mMultiplexer.end()) |
| 3076 |
{ |
| 3077 |
LOGC(smlog.Fatal, log << "IPE: For socket @" << u << " MUXER id=" << mid << " NOT FOUND!"); |
| 3078 |
return; |
| 3079 |
} |
| 3080 |
|
| 3081 |
CMultiplexer& mx = m->second; |
| 3082 |
|
| 3083 |
mx.m_iRefCount--; |
| 3084 |
HLOGC(smlog.Debug, log << "unrefing underlying muxer " << mid << " for @" << u << ", ref=" << mx.m_iRefCount); |
| 3085 |
if (0 == mx.m_iRefCount) |
| 3086 |
{ |
| 3087 |
HLOGC(smlog.Debug, |
| 3088 |
log << "MUXER id=" << mid << " lost last socket @" << u << " - deleting muxer bound to port " |
| 3089 |
<< mx.m_pChannel->bindAddressAny().hport()); |
| 3090 |
// The channel has no access to the queues and |
| 3091 |
// it looks like the multiplexer is the master of all of them. |
| 3092 |
// The queues must be silenced before closing the channel |
| 3093 |
// because this will cause error to be returned in any operation |
| 3094 |
// being currently done in the queues, if any. |
| 3095 |
mx.m_pSndQueue->setClosing(); |
| 3096 |
mx.m_pRcvQueue->setClosing(); |
| 3097 |
mx.destroy(); |
| 3098 |
m_mMultiplexer.erase(m); |
| 3099 |
} |
| 3100 |
} |
| 3101 |
|
| 3102 |
void srt::CUDTUnited::configureMuxer(CMultiplexer& w_m, const CUDTSocket* s, int af) |
| 3103 |
{ |
| 3104 |
w_m.m_mcfg = s->core().m_config; |
| 3105 |
w_m.m_iIPversion = af; |
| 3106 |
w_m.m_iRefCount = 1; |
| 3107 |
w_m.m_iID = s->m_SocketID; |
| 3108 |
} |
| 3109 |
|
| 3110 |
uint16_t srt::CUDTUnited::installMuxer(CUDTSocket* w_s, CMultiplexer& fw_sm) |
| 3111 |
{ |
| 3112 |
w_s->core().m_pSndQueue = fw_sm.m_pSndQueue; |
| 3113 |
w_s->core().m_pRcvQueue = fw_sm.m_pRcvQueue; |
| 3114 |
w_s->m_iMuxID = fw_sm.m_iID; |
| 3115 |
sockaddr_any sa; |
| 3116 |
fw_sm.m_pChannel->getSockAddr((sa)); |
| 3117 |
w_s->m_SelfAddr = sa; // Will be also completed later, but here it's needed for later checks |
| 3118 |
return sa.hport(); |
| 3119 |
} |
| 3120 |
|
| 3121 |
bool srt::CUDTUnited::inet6SettingsCompat(const sockaddr_any& muxaddr, const CSrtMuxerConfig& cfgMuxer, |
| 3122 |
const sockaddr_any& reqaddr, const CSrtMuxerConfig& cfgSocket) |
| 3123 |
{ |
| 3124 |
if (muxaddr.family() != AF_INET6) |
| 3125 |
return true; // Don't check - the family has been checked already |
| 3126 |
|
| 3127 |
if (reqaddr.isany()) |
| 3128 |
{ |
| 3129 |
if (cfgSocket.iIpV6Only == -1) // Treat as "adaptive" |
| 3130 |
return true; |
| 3131 |
|
| 3132 |
// If set explicitly, then it must be equal to the one of found muxer. |
| 3133 |
return cfgSocket.iIpV6Only == cfgMuxer.iIpV6Only; |
| 3134 |
} |
| 3135 |
|
| 3136 |
// If binding to the certain IPv6 address, then this setting doesn't matter. |
| 3137 |
return true; |
| 3138 |
} |
| 3139 |
|
| 3140 |
bool srt::CUDTUnited::channelSettingsMatch(const CSrtMuxerConfig& cfgMuxer, const CSrtConfig& cfgSocket) |
| 3141 |
{ |
| 3142 |
if (!cfgMuxer.bReuseAddr) |
| 3143 |
{ |
| 3144 |
HLOGP(smlog.Debug, "channelSettingsMatch: fail: the multiplexer is not reusable"); |
| 3145 |
return false; |
| 3146 |
} |
| 3147 |
|
| 3148 |
if (cfgMuxer.isCompatWith(cfgSocket)) |
| 3149 |
return true; |
| 3150 |
|
| 3151 |
HLOGP(smlog.Debug, "channelSettingsMatch: fail: some options have different values"); |
| 3152 |
return false; |
| 3153 |
} |
| 3154 |
|
| 3155 |
void srt::CUDTUnited::updateMux(CUDTSocket* s, const sockaddr_any& reqaddr, const UDPSOCKET* udpsock /*[[nullable]]*/) |
| 3156 |
{ |
| 3157 |
const int port = reqaddr.hport(); |
| 3158 |
ExclusiveLock cg(m_GlobControlLock); |
| 3159 |
|
| 3160 |
// If udpsock is provided, then this socket will be simply |
| 3161 |
// taken for binding as a good deal. It would be nice to make |
| 3162 |
// a sanity check to see if this UDP socket isn't already installed |
| 3163 |
// in some multiplexer, but we state this UDP socket isn't accessible |
| 3164 |
// anyway so this wouldn't be possible. |
| 3165 |
if (!udpsock) |
| 3166 |
{ |
| 3167 |
// If not, we need to see if there exist already a multiplexer bound |
| 3168 |
// to the same endpoint. |
| 3169 |
const CSrtConfig& cfgSocket = s->core().m_config; |
| 3170 |
|
| 3171 |
// This loop is going to check the attempted binding of |
| 3172 |
// address:port and socket settings against every existing |
| 3173 |
// multiplexer. Possible results of the check are: |
| 3174 |
|
| 3175 |
// 1. MATCH: identical address - reuse it and quit. |
| 3176 |
// 2. CONFLICT: report error: the binding partially overlaps |
| 3177 |
// so it neither can be reused nor is free to bind. |
| 3178 |
// 3. PASS: different and not overlapping - continue searching. |
| 3179 |
|
| 3180 |
// In this function the convention is: |
| 3181 |
// MATCH: do nothing and proceed with binding reusage, THEN break. |
| 3182 |
// CONFLICT: throw an exception. |
| 3183 |
// PASS: use 'continue' to pass to the next element. |
| 3184 |
|
| 3185 |
bool reuse_attempt = false; |
| 3186 |
for (map<int, CMultiplexer>::iterator i = m_mMultiplexer.begin(); i != m_mMultiplexer.end(); ++i) |
| 3187 |
{ |
| 3188 |
CMultiplexer const& m = i->second; |
| 3189 |
|
| 3190 |
// First, we need to find a multiplexer with the same port. |
| 3191 |
if (m.m_iPort != port) |
| 3192 |
{ |
| 3193 |
HLOGC(smlog.Debug, |
| 3194 |
log << "bind: muxer @" << m.m_iID << " found, but for port " << m.m_iPort |
| 3195 |
<< " (requested port: " << port << ")"); |
| 3196 |
continue; |
| 3197 |
} |
| 3198 |
|
| 3199 |
// If this is bound to the wildcard address, it can be reused if: |
| 3200 |
// - reqaddr is also a wildcard |
| 3201 |
// - channel settings match |
| 3202 |
// Otherwise it's a conflict. |
| 3203 |
sockaddr_any mux_addr; |
| 3204 |
m.m_pChannel->getSockAddr((mux_addr)); |
| 3205 |
|
| 3206 |
HLOGC(smlog.Debug, |
| 3207 |
log << "bind: Found existing muxer @" << m.m_iID << " : " << mux_addr.str() << " - check against " |
| 3208 |
<< reqaddr.str()); |
| 3209 |
|
| 3210 |
if (mux_addr.isany()) |
| 3211 |
{ |
| 3212 |
if (mux_addr.family() == AF_INET6) |
| 3213 |
{ |
| 3214 |
// With IPv6 we need to research two possibilities: |
| 3215 |
// iIpV6Only == 1 -> This means that it binds only :: wildcard, but not 0.0.0.0 |
| 3216 |
// iIpV6Only == 0 -> This means that it binds both :: and 0.0.0.0. |
| 3217 |
// iIpV6Only == -1 -> Hard to say what to do, but treat it as a potential conflict in any doubtful case. |
| 3218 |
|
| 3219 |
if (m.m_mcfg.iIpV6Only == 1) |
| 3220 |
{ |
| 3221 |
// PASS IF: candidate is IPv4, no matter the address |
| 3222 |
// MATCH IF: candidate is IPv6 with only=1 |
| 3223 |
// CONFLICT IF: candidate is IPv6 with only != 1 or IPv6 non-wildcard. |
| 3224 |
|
| 3225 |
if (reqaddr.family() == AF_INET) |
| 3226 |
{ |
| 3227 |
HLOGC(smlog.Debug, log << "bind: muxer @" << m.m_iID |
| 3228 |
<< " is :: v6only - requested IPv4 ANY is NOT IN THE WAY. Searching on."); |
| 3229 |
continue; |
| 3230 |
} |
| 3231 |
|
| 3232 |
// Candidate is AF_INET6 |
| 3233 |
|
| 3234 |
if (cfgSocket.iIpV6Only != 1 || !reqaddr.isany()) |
| 3235 |
{ |
| 3236 |
// CONFLICT: |
| 3237 |
// 1. attempting to make a wildcard IPv4 + IPv6 |
| 3238 |
// while the multiplexer for wildcard IPv6 exists. |
| 3239 |
// 2. If binding to a given address, it conflicts with the wildcard |
| 3240 |
LOGC(smlog.Error, |
| 3241 |
log << "bind: Address: " << reqaddr.str() |
| 3242 |
<< " conflicts with existing IPv6 wildcard binding: " << mux_addr.str()); |
| 3243 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3244 |
} |
| 3245 |
|
| 3246 |
// Otherwise, MATCH. |
| 3247 |
} |
| 3248 |
else if (m.m_mcfg.iIpV6Only == 0) |
| 3249 |
{ |
| 3250 |
// Muxer's address is a wildcard for :: and 0.0.0.0 at once. |
| 3251 |
// This way only IPv6 wildcard with v6only=0 is a perfect match and everything |
| 3252 |
// else is a conflict. |
| 3253 |
|
| 3254 |
if (reqaddr.family() == AF_INET6 && reqaddr.isany() && cfgSocket.iIpV6Only == 0) |
| 3255 |
{ |
| 3256 |
// MATCH |
| 3257 |
} |
| 3258 |
else |
| 3259 |
{ |
| 3260 |
// CONFLICT: attempting to make a wildcard IPv4 + IPv6 while |
| 3261 |
// the multiplexer for wildcard IPv6 exists. |
| 3262 |
LOGC(smlog.Error, |
| 3263 |
log << "bind: Address: " << reqaddr.str() << " v6only=" << cfgSocket.iIpV6Only |
| 3264 |
<< " conflicts with existing IPv6 + IPv4 wildcard binding: " << mux_addr.str()); |
| 3265 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3266 |
} |
| 3267 |
} |
| 3268 |
else // Case -1, by unknown reason. Accept only with -1 setting, others are conflict. |
| 3269 |
{ |
| 3270 |
if (reqaddr.family() == AF_INET6 && reqaddr.isany() && cfgSocket.iIpV6Only == -1) |
| 3271 |
{ |
| 3272 |
// MATCH |
| 3273 |
} |
| 3274 |
else |
| 3275 |
{ |
| 3276 |
LOGC(smlog.Error, |
| 3277 |
log << "bind: Address: " << reqaddr.str() << " v6only=" << cfgSocket.iIpV6Only |
| 3278 |
<< " conflicts with existing IPv6 v6only=unknown wildcard binding: " << mux_addr.str()); |
| 3279 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3280 |
} |
| 3281 |
} |
| 3282 |
} |
| 3283 |
else // muxer is IPv4 wildcard |
| 3284 |
{ |
| 3285 |
// Then only IPv4 wildcard is a match and: |
| 3286 |
// - IPv6 with only=true is PASS (not a conflict) |
| 3287 |
// - IPv6 with only=false is CONFLICT |
| 3288 |
// - IPv6 with only=undefined is CONFLICT |
| 3289 |
// REASON: we need to make a potential conflict a conflict as there will be |
| 3290 |
// no bind() call to check if this wouldn't be a conflict in result. If you want |
| 3291 |
// to have a binding to IPv6 that should avoid conflict with IPv4 wildcard binding, |
| 3292 |
// then SRTO_IPV6ONLY option must be explicitly set before binding. |
| 3293 |
// Also: |
| 3294 |
if (reqaddr.family() == AF_INET) |
| 3295 |
{ |
| 3296 |
if (reqaddr.isany()) |
| 3297 |
{ |
| 3298 |
// MATCH |
| 3299 |
} |
| 3300 |
else |
| 3301 |
{ |
| 3302 |
LOGC(smlog.Error, |
| 3303 |
log << "bind: Address: " << reqaddr.str() |
| 3304 |
<< " conflicts with existing IPv4 wildcard binding: " << mux_addr.str()); |
| 3305 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3306 |
} |
| 3307 |
} |
| 3308 |
else // AF_INET6 |
| 3309 |
{ |
| 3310 |
if (cfgSocket.iIpV6Only == 1 || !reqaddr.isany()) |
| 3311 |
{ |
| 3312 |
// PASS |
| 3313 |
HLOGC(smlog.Debug, log << "bind: muxer @" << m.m_iID |
| 3314 |
<< " is IPv4 wildcard - requested " << reqaddr.str() << " v6only=" << cfgSocket.iIpV6Only |
| 3315 |
<< " is NOT IN THE WAY. Searching on."); |
| 3316 |
continue; |
| 3317 |
} |
| 3318 |
else |
| 3319 |
{ |
| 3320 |
LOGC(smlog.Error, |
| 3321 |
log << "bind: Address: " << reqaddr.str() << " v6only=" << cfgSocket.iIpV6Only |
| 3322 |
<< " conflicts with existing IPv4 wildcard binding: " << mux_addr.str()); |
| 3323 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3324 |
} |
| 3325 |
} |
| 3326 |
} |
| 3327 |
|
| 3328 |
reuse_attempt = true; |
| 3329 |
HLOGC(smlog.Debug, log << "bind: wildcard address - multiplexer reusable"); |
| 3330 |
} |
| 3331 |
// Muxer address is NOT a wildcard, so conflicts only with WILDCARD of the same type |
| 3332 |
else if (reqaddr.isany() && reqaddr.family() == mux_addr.family()) |
| 3333 |
{ |
| 3334 |
LOGC(smlog.Error, |
| 3335 |
log << "bind: Wildcard address: " << reqaddr.str() |
| 3336 |
<< " conflicts with existing IP binding: " << mux_addr.str()); |
| 3337 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3338 |
} |
| 3339 |
// If this is bound to a certain address, AND: |
| 3340 |
else if (mux_addr.equal_address(reqaddr)) |
| 3341 |
{ |
| 3342 |
// - the address is the same as reqaddr |
| 3343 |
reuse_attempt = true; |
| 3344 |
HLOGC(smlog.Debug, log << "bind: same IP address - multiplexer reusable"); |
| 3345 |
} |
| 3346 |
else |
| 3347 |
{ |
| 3348 |
HLOGC(smlog.Debug, log << "bind: IP addresses differ - ALLOWED to create a new multiplexer"); |
| 3349 |
continue; |
| 3350 |
} |
| 3351 |
// Otherwise: |
| 3352 |
// - the address is different than reqaddr |
| 3353 |
// - the address can't be reused, but this can go on with new one. |
| 3354 |
|
| 3355 |
// If this is a reusage attempt: |
| 3356 |
if (reuse_attempt) |
| 3357 |
{ |
| 3358 |
// - if the channel settings match, it can be reused |
| 3359 |
if (channelSettingsMatch(m.m_mcfg, cfgSocket) && inet6SettingsCompat(mux_addr, m.m_mcfg, reqaddr, cfgSocket)) |
| 3360 |
{ |
| 3361 |
HLOGC(smlog.Debug, log << "bind: reusing multiplexer for port " << port); |
| 3362 |
// reuse the existing multiplexer |
| 3363 |
++i->second.m_iRefCount; |
| 3364 |
installMuxer((s), (i->second)); |
| 3365 |
return; |
| 3366 |
} |
| 3367 |
else |
| 3368 |
{ |
| 3369 |
// - if not, it's a conflict |
| 3370 |
LOGC(smlog.Error, |
| 3371 |
log << "bind: Address: " << reqaddr.str() << " conflicts with binding: " << mux_addr.str() |
| 3372 |
<< " due to channel settings"); |
| 3373 |
throw CUDTException(MJ_NOTSUP, MN_BUSYPORT, 0); |
| 3374 |
} |
| 3375 |
} |
| 3376 |
// If not, proceed to the next one, and when there are no reusage |
| 3377 |
// candidates, proceed with creating a new multiplexer. |
| 3378 |
|
| 3379 |
// Note that a binding to a different IP address is not treated |
| 3380 |
// as a candidate for either reusage or conflict. |
| 3381 |
LOGC(smlog.Fatal, log << "SHOULD NOT GET HERE!!!"); |
| 3382 |
SRT_ASSERT(false); |
| 3383 |
} |
| 3384 |
} |
| 3385 |
|
| 3386 |
|
| 3387 |
|
| 3388 |
// a new multiplexer is needed |
| 3389 |
CMultiplexer m; |
| 3390 |
configureMuxer((m), s, reqaddr.family()); |
| 3391 |
|
| 3392 |
try |
| 3393 |
{ |
| 3394 |
m.m_pChannel = new CChannel(); |
| 3395 |
m.m_pChannel->setConfig(m.m_mcfg); |
| 3396 |
|
| 3397 |
if (udpsock) |
| 3398 |
{ |
| 3399 |
// In this case, reqaddr contains the address |
| 3400 |
// that has been extracted already from the |
| 3401 |
// given socket |
| 3402 |
m.m_pChannel->attach(*udpsock, reqaddr); |
| 3403 |
} |
| 3404 |
else if (reqaddr.empty()) |
| 3405 |
{ |
| 3406 |
// The case of previously used case of a NULL address. |
| 3407 |
// This here is used to pass family only, in this case |
| 3408 |
// just automatically bind to the "0" address to autoselect |
| 3409 |
// everything. |
| 3410 |
m.m_pChannel->open(reqaddr.family()); |
| 3411 |
} |
| 3412 |
else |
| 3413 |
{ |
| 3414 |
// If at least the IP address is specified, then bind to that |
| 3415 |
// address, but still possibly autoselect the outgoing port, if the |
| 3416 |
// port was specified as 0. |
| 3417 |
m.m_pChannel->open(reqaddr); |
| 3418 |
} |
| 3419 |
|
| 3420 |
// AFTER OPENING, check the matter of IPV6_V6ONLY option, |
| 3421 |
// as it decides about the fact that the occupied binding address |
| 3422 |
// in case of wildcard is both :: and 0.0.0.0, or only ::. |
| 3423 |
if (reqaddr.family() == AF_INET6 && m.m_mcfg.iIpV6Only == -1) |
| 3424 |
{ |
| 3425 |
// XXX We don't know how probable it is to get the error here |
| 3426 |
// and resulting -1 value. As a fallback for that case, the value -1 |
| 3427 |
// is honored here, just all side-bindings for other sockes will be |
| 3428 |
// rejected as a potential conflict, even if binding would be accepted |
| 3429 |
// in these circumstances. Only a perfect match in case of potential |
| 3430 |
// overlapping will be accepted on the same port. |
| 3431 |
m.m_mcfg.iIpV6Only = m.m_pChannel->sockopt(IPPROTO_IPV6, IPV6_V6ONLY, -1); |
| 3432 |
} |
| 3433 |
|
| 3434 |
m.m_pTimer = new CTimer; |
| 3435 |
m.m_pSndQueue = new CSndQueue; |
| 3436 |
m.m_pSndQueue->init(m.m_pChannel, m.m_pTimer); |
| 3437 |
m.m_pRcvQueue = new CRcvQueue; |
| 3438 |
m.m_pRcvQueue->init(128, s->core().maxPayloadSize(), m.m_iIPversion, 1024, m.m_pChannel, m.m_pTimer); |
| 3439 |
|
| 3440 |
// Rewrite the port here, as it might be only known upon return |
| 3441 |
// from CChannel::open. |
| 3442 |
m.m_iPort = installMuxer((s), m); |
| 3443 |
swap(m_mMultiplexer[m.m_iID],m); |
| 3444 |
} |
| 3445 |
catch (const CUDTException&) |
| 3446 |
{ |
| 3447 |
m.destroy(); |
| 3448 |
throw; |
| 3449 |
} |
| 3450 |
catch (...) |
| 3451 |
{ |
| 3452 |
m.destroy(); |
| 3453 |
throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3454 |
} |
| 3455 |
|
| 3456 |
HLOGC(smlog.Debug, log << "bind: creating new multiplexer for port " << port); |
| 3457 |
} |
| 3458 |
|
| 3459 |
// This function is going to find a multiplexer for the port contained |
| 3460 |
// in the 'ls' listening socket. The multiplexer must exist when the listener |
| 3461 |
// exists, otherwise the dispatching procedure wouldn't even call this |
| 3462 |
// function. By historical reasons there's also a fallback for a case when the |
| 3463 |
// multiplexer wasn't found by id, the search by port number continues. |
| 3464 |
bool srt::CUDTUnited::updateListenerMux(CUDTSocket* s, const CUDTSocket* ls) |
| 3465 |
{ |
| 3466 |
ExclusiveLock cg(m_GlobControlLock); |
| 3467 |
const int port = ls->m_SelfAddr.hport(); |
| 3468 |
|
| 3469 |
HLOGC(smlog.Debug, |
| 3470 |
log << "updateListenerMux: finding muxer of listener socket @" << ls->m_SocketID << " muxid=" << ls->m_iMuxID |
| 3471 |
<< " bound=" << ls->m_SelfAddr.str() << " FOR @" << s->m_SocketID << " addr=" << s->m_SelfAddr.str() |
| 3472 |
<< "_->_" << s->m_PeerAddr.str()); |
| 3473 |
|
| 3474 |
// First thing that should be certain here is that there should exist |
| 3475 |
// a muxer with the ID written in the listener socket's mux ID. |
| 3476 |
|
| 3477 |
CMultiplexer* mux = map_getp(m_mMultiplexer, ls->m_iMuxID); |
| 3478 |
|
| 3479 |
// NOTE: |
| 3480 |
// THIS BELOW CODE is only for a highly unlikely situation when the listener |
| 3481 |
// socket has been closed in the meantime when the accepted socket is being |
| 3482 |
// processed. This procedure is different than updateMux because this time we |
| 3483 |
// only want to have a multiplexer socket to be assigned to the accepted socket. |
| 3484 |
// It is also unlikely that the listener socket is garbage-collected so fast, so |
| 3485 |
// this procedure will most likely find the multiplexer of the zombie listener socket, |
| 3486 |
// which no longer accepts new connections (the listener is withdrawn immediately from |
| 3487 |
// the port) that wasn't yet completely deleted. |
| 3488 |
CMultiplexer* fallback = NULL; |
| 3489 |
if (!mux) |
| 3490 |
{ |
| 3491 |
LOGC(smlog.Error, log << "updateListenerMux: IPE? listener muxer not found by ID, trying by port"); |
| 3492 |
|
| 3493 |
// To be used as first found with different IP version |
| 3494 |
|
| 3495 |
// find the listener's address |
| 3496 |
for (map<int, CMultiplexer>::iterator i = m_mMultiplexer.begin(); i != m_mMultiplexer.end(); ++i) |
| 3497 |
{ |
| 3498 |
CMultiplexer& m = i->second; |
| 3499 |
|
| 3500 |
#if ENABLE_HEAVY_LOGGING |
| 3501 |
ostringstream that_muxer; |
| 3502 |
that_muxer << "id=" << m.m_iID << " port=" << m.m_iPort |
| 3503 |
<< " ip=" << (m.m_iIPversion == AF_INET ? "v4" : "v6"); |
| 3504 |
#endif |
| 3505 |
|
| 3506 |
if (m.m_iPort == port) |
| 3507 |
{ |
| 3508 |
HLOGC(smlog.Debug, log << "updateListenerMux: reusing muxer: " << that_muxer.str()); |
| 3509 |
if (m.m_iIPversion == s->m_PeerAddr.family()) |
| 3510 |
{ |
| 3511 |
mux = &m; // best match |
| 3512 |
break; |
| 3513 |
} |
| 3514 |
else if (m.m_iIPversion == AF_INET6) |
| 3515 |
{ |
| 3516 |
// Allowed fallback case when we only need an accepted socket. |
| 3517 |
fallback = &m; |
| 3518 |
} |
| 3519 |
} |
| 3520 |
else |
| 3521 |
{ |
| 3522 |
HLOGC(smlog.Debug, log << "updateListenerMux: SKIPPING muxer: " << that_muxer.str()); |
| 3523 |
} |
| 3524 |
} |
| 3525 |
|
| 3526 |
if (!mux && fallback) |
| 3527 |
{ |
| 3528 |
// It is allowed to reuse this multiplexer, but the socket must allow both IPv4 and IPv6 |
| 3529 |
if (fallback->m_mcfg.iIpV6Only == 0) |
| 3530 |
{ |
| 3531 |
HLOGC(smlog.Warn, log << "updateListenerMux: reusing multiplexer from different family"); |
| 3532 |
mux = fallback; |
| 3533 |
} |
| 3534 |
} |
| 3535 |
} |
| 3536 |
|
| 3537 |
// Checking again because the above procedure could have set it |
| 3538 |
if (mux) |
| 3539 |
{ |
| 3540 |
// reuse the existing multiplexer |
| 3541 |
++mux->m_iRefCount; |
| 3542 |
s->core().m_pSndQueue = mux->m_pSndQueue; |
| 3543 |
s->core().m_pRcvQueue = mux->m_pRcvQueue; |
| 3544 |
s->m_iMuxID = mux->m_iID; |
| 3545 |
return true; |
| 3546 |
} |
| 3547 |
|
| 3548 |
return false; |
| 3549 |
} |
| 3550 |
|
| 3551 |
void* srt::CUDTUnited::garbageCollect(void* p) |
| 3552 |
{ |
| 3553 |
CUDTUnited* self = (CUDTUnited*)p; |
| 3554 |
|
| 3555 |
THREAD_STATE_INIT("SRT:GC"); |
| 3556 |
|
| 3557 |
UniqueLock gclock(self->m_GCStopLock); |
| 3558 |
|
| 3559 |
while (!self->m_bClosing) |
| 3560 |
{ |
| 3561 |
INCREMENT_THREAD_ITERATIONS(); |
| 3562 |
self->checkBrokenSockets(); |
| 3563 |
|
| 3564 |
HLOGC(inlog.Debug, log << "GC: sleep 1 s"); |
| 3565 |
self->m_GCStopCond.wait_for(gclock, seconds_from(1)); |
| 3566 |
} |
| 3567 |
THREAD_EXIT(); |
| 3568 |
return NULL; |
| 3569 |
} |
| 3570 |
|
| 3571 |
//////////////////////////////////////////////////////////////////////////////// |
| 3572 |
|
| 3573 |
int srt::CUDT::startup() |
| 3574 |
{ |
| 3575 |
#if HAVE_PTHREAD_ATFORK |
| 3576 |
static bool registered = false; |
| 3577 |
if (!registered) |
| 3578 |
{ |
| 3579 |
pthread_atfork(NULL, NULL, (void (*)()) srt::CUDT::cleanupAtFork); |
| 3580 |
registered = true; |
| 3581 |
} |
| 3582 |
#endif |
| 3583 |
return uglobal().startup(); |
| 3584 |
} |
| 3585 |
|
| 3586 |
int srt::CUDT::cleanup() |
| 3587 |
{ |
| 3588 |
return uglobal().cleanup(); |
| 3589 |
} |
| 3590 |
|
| 3591 |
int srt::CUDT::cleanupAtFork() |
| 3592 |
{ |
| 3593 |
CUDTUnited &context = uglobal(); |
| 3594 |
context.cleanupAtFork(); |
| 3595 |
new (&context) CUDTUnited(); |
| 3596 |
|
| 3597 |
return context.startup(); |
| 3598 |
} |
| 3599 |
|
| 3600 |
SRTSOCKET srt::CUDT::socket() |
| 3601 |
{ |
| 3602 |
try |
| 3603 |
{ |
| 3604 |
return uglobal().newSocket(); |
| 3605 |
} |
| 3606 |
catch (const CUDTException& e) |
| 3607 |
{ |
| 3608 |
SetThreadLocalError(e); |
| 3609 |
return INVALID_SOCK; |
| 3610 |
} |
| 3611 |
catch (const bad_alloc&) |
| 3612 |
{ |
| 3613 |
SetThreadLocalError(CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0)); |
| 3614 |
return INVALID_SOCK; |
| 3615 |
} |
| 3616 |
catch (const std::exception& ee) |
| 3617 |
{ |
| 3618 |
LOGC(aclog.Fatal, log << "socket: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3619 |
SetThreadLocalError(CUDTException(MJ_UNKNOWN, MN_NONE, 0)); |
| 3620 |
return INVALID_SOCK; |
| 3621 |
} |
| 3622 |
} |
| 3623 |
|
| 3624 |
srt::CUDT::APIError::APIError(const CUDTException& e) |
| 3625 |
{ |
| 3626 |
SetThreadLocalError(e); |
| 3627 |
} |
| 3628 |
|
| 3629 |
srt::CUDT::APIError::APIError(CodeMajor mj, CodeMinor mn, int syserr) |
| 3630 |
{ |
| 3631 |
SetThreadLocalError(CUDTException(mj, mn, syserr)); |
| 3632 |
} |
| 3633 |
|
| 3634 |
#if ENABLE_BONDING |
| 3635 |
// This is an internal function; 'type' should be pre-checked if it has a correct value. |
| 3636 |
// This doesn't have argument of GroupType due to header file conflicts. |
| 3637 |
|
| 3638 |
// [[using locked(s_UDTUnited.m_GlobControlLock)]] |
| 3639 |
srt::CUDTGroup& srt::CUDT::newGroup(const int type) |
| 3640 |
{ |
| 3641 |
const SRTSOCKET id = uglobal().generateSocketID(true); |
| 3642 |
|
| 3643 |
// Now map the group |
| 3644 |
return uglobal().addGroup(id, SRT_GROUP_TYPE(type)).set_id(id); |
| 3645 |
} |
| 3646 |
|
| 3647 |
SRTSOCKET srt::CUDT::createGroup(SRT_GROUP_TYPE gt) |
| 3648 |
{ |
| 3649 |
try |
| 3650 |
{ |
| 3651 |
srt::sync::ExclusiveLock globlock(uglobal().m_GlobControlLock); |
| 3652 |
return newGroup(gt).id(); |
| 3653 |
// Note: potentially, after this function exits, the group |
| 3654 |
// could be deleted, immediately, from a separate thread (though |
| 3655 |
// unlikely because the other thread would need some handle to |
| 3656 |
// keep it). But then, the first call to any API function would |
| 3657 |
// return invalid ID error. |
| 3658 |
} |
| 3659 |
catch (const CUDTException& e) |
| 3660 |
{ |
| 3661 |
return APIError(e); |
| 3662 |
} |
| 3663 |
catch (...) |
| 3664 |
{ |
| 3665 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3666 |
} |
| 3667 |
|
| 3668 |
return SRT_INVALID_SOCK; |
| 3669 |
} |
| 3670 |
|
| 3671 |
// [[using locked(m_ControlLock)]] |
| 3672 |
// [[using locked(CUDT::s_UDTUnited.m_GlobControlLock)]] |
| 3673 |
void srt::CUDTSocket::removeFromGroup(bool broken) |
| 3674 |
{ |
| 3675 |
CUDTGroup* g = m_GroupOf; |
| 3676 |
if (g) |
| 3677 |
{ |
| 3678 |
// Reset group-related fields immediately. They won't be accessed |
| 3679 |
// in the below calls, while the iterator will be invalidated for |
| 3680 |
// a short moment between removal from the group container and the end, |
| 3681 |
// while the GroupLock would be already taken out. It is safer to reset |
| 3682 |
// it to a NULL iterator before removal. |
| 3683 |
m_GroupOf = NULL; |
| 3684 |
m_GroupMemberData = NULL; |
| 3685 |
|
| 3686 |
bool still_have = g->remove(m_SocketID); |
| 3687 |
if (broken) |
| 3688 |
{ |
| 3689 |
// Activate the SRT_EPOLL_UPDATE event on the group |
| 3690 |
// if it was because of a socket that was earlier connected |
| 3691 |
// and became broken. This is not to be sent in case when |
| 3692 |
// it is a failure during connection, or the socket was |
| 3693 |
// explicitly removed from the group. |
| 3694 |
g->activateUpdateEvent(still_have); |
| 3695 |
} |
| 3696 |
|
| 3697 |
HLOGC(smlog.Debug, |
| 3698 |
log << "removeFromGroup: socket @" << m_SocketID << " NO LONGER A MEMBER of $" << g->id() << "; group is " |
| 3699 |
<< (still_have ? "still ACTIVE" : "now EMPTY")); |
| 3700 |
} |
| 3701 |
} |
| 3702 |
|
| 3703 |
SRTSOCKET srt::CUDT::getGroupOfSocket(SRTSOCKET socket) |
| 3704 |
{ |
| 3705 |
// Lock this for the whole function as we need the group |
| 3706 |
// to persist the call. |
| 3707 |
SharedLock glock(uglobal().m_GlobControlLock); |
| 3708 |
CUDTSocket* s = uglobal().locateSocket_LOCKED(socket); |
| 3709 |
if (!s || !s->m_GroupOf) |
| 3710 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3711 |
|
| 3712 |
return s->m_GroupOf->id(); |
| 3713 |
} |
| 3714 |
|
| 3715 |
int srt::CUDT::getGroupData(SRTSOCKET groupid, SRT_SOCKGROUPDATA* pdata, size_t* psize) |
| 3716 |
{ |
| 3717 |
if ((groupid & SRTGROUP_MASK) == 0 || !psize) |
| 3718 |
{ |
| 3719 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3720 |
} |
| 3721 |
|
| 3722 |
CUDTUnited::GroupKeeper k(uglobal(), groupid, CUDTUnited::ERH_RETURN); |
| 3723 |
if (!k.group) |
| 3724 |
{ |
| 3725 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3726 |
} |
| 3727 |
|
| 3728 |
// To get only the size of the group pdata=NULL can be used |
| 3729 |
return k.group->getGroupData(pdata, psize); |
| 3730 |
} |
| 3731 |
#endif |
| 3732 |
|
| 3733 |
int srt::CUDT::bind(SRTSOCKET u, const sockaddr* name, int namelen) |
| 3734 |
{ |
| 3735 |
try |
| 3736 |
{ |
| 3737 |
sockaddr_any sa(name, namelen); |
| 3738 |
if (sa.len == 0) |
| 3739 |
{ |
| 3740 |
// This happens if the namelen check proved it to be |
| 3741 |
// too small for particular family, or that family is |
| 3742 |
// not recognized (is none of AF_INET, AF_INET6). |
| 3743 |
// This is a user error. |
| 3744 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3745 |
} |
| 3746 |
CUDTSocket* s = uglobal().locateSocket(u); |
| 3747 |
if (!s) |
| 3748 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3749 |
|
| 3750 |
return uglobal().bind(s, sa); |
| 3751 |
} |
| 3752 |
catch (const CUDTException& e) |
| 3753 |
{ |
| 3754 |
return APIError(e); |
| 3755 |
} |
| 3756 |
catch (bad_alloc&) |
| 3757 |
{ |
| 3758 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3759 |
} |
| 3760 |
catch (const std::exception& ee) |
| 3761 |
{ |
| 3762 |
LOGC(aclog.Fatal, log << "bind: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3763 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3764 |
} |
| 3765 |
} |
| 3766 |
|
| 3767 |
int srt::CUDT::bind(SRTSOCKET u, UDPSOCKET udpsock) |
| 3768 |
{ |
| 3769 |
try |
| 3770 |
{ |
| 3771 |
CUDTSocket* s = uglobal().locateSocket(u); |
| 3772 |
if (!s) |
| 3773 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3774 |
|
| 3775 |
return uglobal().bind(s, udpsock); |
| 3776 |
} |
| 3777 |
catch (const CUDTException& e) |
| 3778 |
{ |
| 3779 |
return APIError(e); |
| 3780 |
} |
| 3781 |
catch (bad_alloc&) |
| 3782 |
{ |
| 3783 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3784 |
} |
| 3785 |
catch (const std::exception& ee) |
| 3786 |
{ |
| 3787 |
LOGC(aclog.Fatal, log << "bind/udp: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3788 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3789 |
} |
| 3790 |
} |
| 3791 |
|
| 3792 |
int srt::CUDT::listen(SRTSOCKET u, int backlog) |
| 3793 |
{ |
| 3794 |
try |
| 3795 |
{ |
| 3796 |
return uglobal().listen(u, backlog); |
| 3797 |
} |
| 3798 |
catch (const CUDTException& e) |
| 3799 |
{ |
| 3800 |
return APIError(e); |
| 3801 |
} |
| 3802 |
catch (bad_alloc&) |
| 3803 |
{ |
| 3804 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3805 |
} |
| 3806 |
catch (const std::exception& ee) |
| 3807 |
{ |
| 3808 |
LOGC(aclog.Fatal, log << "listen: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3809 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3810 |
} |
| 3811 |
} |
| 3812 |
|
| 3813 |
SRTSOCKET srt::CUDT::accept_bond(const SRTSOCKET listeners[], int lsize, int64_t msTimeOut) |
| 3814 |
{ |
| 3815 |
try |
| 3816 |
{ |
| 3817 |
return uglobal().accept_bond(listeners, lsize, msTimeOut); |
| 3818 |
} |
| 3819 |
catch (const CUDTException& e) |
| 3820 |
{ |
| 3821 |
SetThreadLocalError(e); |
| 3822 |
return INVALID_SOCK; |
| 3823 |
} |
| 3824 |
catch (bad_alloc&) |
| 3825 |
{ |
| 3826 |
SetThreadLocalError(CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0)); |
| 3827 |
return INVALID_SOCK; |
| 3828 |
} |
| 3829 |
catch (const std::exception& ee) |
| 3830 |
{ |
| 3831 |
LOGC(aclog.Fatal, log << "accept_bond: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3832 |
SetThreadLocalError(CUDTException(MJ_UNKNOWN, MN_NONE, 0)); |
| 3833 |
return INVALID_SOCK; |
| 3834 |
} |
| 3835 |
} |
| 3836 |
|
| 3837 |
SRTSOCKET srt::CUDT::accept(SRTSOCKET u, sockaddr* addr, int* addrlen) |
| 3838 |
{ |
| 3839 |
try |
| 3840 |
{ |
| 3841 |
return uglobal().accept(u, addr, addrlen); |
| 3842 |
} |
| 3843 |
catch (const CUDTException& e) |
| 3844 |
{ |
| 3845 |
SetThreadLocalError(e); |
| 3846 |
return INVALID_SOCK; |
| 3847 |
} |
| 3848 |
catch (const bad_alloc&) |
| 3849 |
{ |
| 3850 |
SetThreadLocalError(CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0)); |
| 3851 |
return INVALID_SOCK; |
| 3852 |
} |
| 3853 |
catch (const std::exception& ee) |
| 3854 |
{ |
| 3855 |
LOGC(aclog.Fatal, log << "accept: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3856 |
SetThreadLocalError(CUDTException(MJ_UNKNOWN, MN_NONE, 0)); |
| 3857 |
return INVALID_SOCK; |
| 3858 |
} |
| 3859 |
} |
| 3860 |
|
| 3861 |
int srt::CUDT::connect(SRTSOCKET u, const sockaddr* name, const sockaddr* tname, int namelen) |
| 3862 |
{ |
| 3863 |
try |
| 3864 |
{ |
| 3865 |
return uglobal().connect(u, name, tname, namelen); |
| 3866 |
} |
| 3867 |
catch (const CUDTException& e) |
| 3868 |
{ |
| 3869 |
return APIError(e); |
| 3870 |
} |
| 3871 |
catch (bad_alloc&) |
| 3872 |
{ |
| 3873 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3874 |
} |
| 3875 |
catch (std::exception& ee) |
| 3876 |
{ |
| 3877 |
LOGC(aclog.Fatal, log << "connect: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3878 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3879 |
} |
| 3880 |
} |
| 3881 |
|
| 3882 |
#if ENABLE_BONDING |
| 3883 |
int srt::CUDT::connectLinks(SRTSOCKET grp, SRT_SOCKGROUPCONFIG targets[], int arraysize) |
| 3884 |
{ |
| 3885 |
if (arraysize <= 0) |
| 3886 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3887 |
|
| 3888 |
if ((grp & SRTGROUP_MASK) == 0) |
| 3889 |
{ |
| 3890 |
// connectLinks accepts only GROUP id, not socket id. |
| 3891 |
return APIError(MJ_NOTSUP, MN_SIDINVAL, 0); |
| 3892 |
} |
| 3893 |
|
| 3894 |
try |
| 3895 |
{ |
| 3896 |
CUDTUnited::GroupKeeper k(uglobal(), grp, CUDTUnited::ERH_THROW); |
| 3897 |
return uglobal().groupConnect(k.group, targets, arraysize); |
| 3898 |
} |
| 3899 |
catch (CUDTException& e) |
| 3900 |
{ |
| 3901 |
return APIError(e); |
| 3902 |
} |
| 3903 |
catch (bad_alloc&) |
| 3904 |
{ |
| 3905 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3906 |
} |
| 3907 |
catch (std::exception& ee) |
| 3908 |
{ |
| 3909 |
LOGC(aclog.Fatal, log << "connect: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3910 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3911 |
} |
| 3912 |
} |
| 3913 |
#endif |
| 3914 |
|
| 3915 |
int srt::CUDT::connect(SRTSOCKET u, const sockaddr* name, int namelen, int32_t forced_isn) |
| 3916 |
{ |
| 3917 |
try |
| 3918 |
{ |
| 3919 |
return uglobal().connect(u, name, namelen, forced_isn); |
| 3920 |
} |
| 3921 |
catch (const CUDTException& e) |
| 3922 |
{ |
| 3923 |
return APIError(e); |
| 3924 |
} |
| 3925 |
catch (bad_alloc&) |
| 3926 |
{ |
| 3927 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 3928 |
} |
| 3929 |
catch (const std::exception& ee) |
| 3930 |
{ |
| 3931 |
LOGC(aclog.Fatal, log << "connect: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3932 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3933 |
} |
| 3934 |
} |
| 3935 |
|
| 3936 |
int srt::CUDT::close(SRTSOCKET u) |
| 3937 |
{ |
| 3938 |
try |
| 3939 |
{ |
| 3940 |
return uglobal().close(u); |
| 3941 |
} |
| 3942 |
catch (const CUDTException& e) |
| 3943 |
{ |
| 3944 |
return APIError(e); |
| 3945 |
} |
| 3946 |
catch (const std::exception& ee) |
| 3947 |
{ |
| 3948 |
LOGC(aclog.Fatal, log << "close: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3949 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3950 |
} |
| 3951 |
} |
| 3952 |
|
| 3953 |
int srt::CUDT::getpeername(SRTSOCKET u, sockaddr* name, int* namelen) |
| 3954 |
{ |
| 3955 |
try |
| 3956 |
{ |
| 3957 |
uglobal().getpeername(u, name, namelen); |
| 3958 |
return 0; |
| 3959 |
} |
| 3960 |
catch (const CUDTException& e) |
| 3961 |
{ |
| 3962 |
return APIError(e); |
| 3963 |
} |
| 3964 |
catch (const std::exception& ee) |
| 3965 |
{ |
| 3966 |
LOGC(aclog.Fatal, log << "getpeername: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3967 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3968 |
} |
| 3969 |
} |
| 3970 |
|
| 3971 |
int srt::CUDT::getsockname(SRTSOCKET u, sockaddr* name, int* namelen) |
| 3972 |
{ |
| 3973 |
try |
| 3974 |
{ |
| 3975 |
uglobal().getsockname(u, name, namelen); |
| 3976 |
return 0; |
| 3977 |
} |
| 3978 |
catch (const CUDTException& e) |
| 3979 |
{ |
| 3980 |
return APIError(e); |
| 3981 |
} |
| 3982 |
catch (const std::exception& ee) |
| 3983 |
{ |
| 3984 |
LOGC(aclog.Fatal, log << "getsockname: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 3985 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 3986 |
} |
| 3987 |
} |
| 3988 |
|
| 3989 |
int srt::CUDT::getsockopt(SRTSOCKET u, int, SRT_SOCKOPT optname, void* pw_optval, int* pw_optlen) |
| 3990 |
{ |
| 3991 |
if (!pw_optval || !pw_optlen) |
| 3992 |
{ |
| 3993 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 3994 |
} |
| 3995 |
|
| 3996 |
try |
| 3997 |
{ |
| 3998 |
#if ENABLE_BONDING |
| 3999 |
if (u & SRTGROUP_MASK) |
| 4000 |
{ |
| 4001 |
CUDTUnited::GroupKeeper k(uglobal(), u, CUDTUnited::ERH_THROW); |
| 4002 |
k.group->getOpt(optname, (pw_optval), (*pw_optlen)); |
| 4003 |
return 0; |
| 4004 |
} |
| 4005 |
#endif |
| 4006 |
|
| 4007 |
CUDT& udt = uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core(); |
| 4008 |
udt.getOpt(optname, (pw_optval), (*pw_optlen)); |
| 4009 |
return 0; |
| 4010 |
} |
| 4011 |
catch (const CUDTException& e) |
| 4012 |
{ |
| 4013 |
return APIError(e); |
| 4014 |
} |
| 4015 |
catch (const std::exception& ee) |
| 4016 |
{ |
| 4017 |
LOGC(aclog.Fatal, log << "getsockopt: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4018 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4019 |
} |
| 4020 |
} |
| 4021 |
|
| 4022 |
int srt::CUDT::setsockopt(SRTSOCKET u, int, SRT_SOCKOPT optname, const void* optval, int optlen) |
| 4023 |
{ |
| 4024 |
if (!optval || optlen < 0) |
| 4025 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 4026 |
|
| 4027 |
try |
| 4028 |
{ |
| 4029 |
#if ENABLE_BONDING |
| 4030 |
if (u & SRTGROUP_MASK) |
| 4031 |
{ |
| 4032 |
CUDTUnited::GroupKeeper k(uglobal(), u, CUDTUnited::ERH_THROW); |
| 4033 |
k.group->setOpt(optname, optval, optlen); |
| 4034 |
return 0; |
| 4035 |
} |
| 4036 |
#endif |
| 4037 |
|
| 4038 |
CUDT& udt = uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core(); |
| 4039 |
udt.setOpt(optname, optval, optlen); |
| 4040 |
return 0; |
| 4041 |
} |
| 4042 |
catch (const CUDTException& e) |
| 4043 |
{ |
| 4044 |
return APIError(e); |
| 4045 |
} |
| 4046 |
catch (const std::exception& ee) |
| 4047 |
{ |
| 4048 |
LOGC(aclog.Fatal, log << "setsockopt: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4049 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4050 |
} |
| 4051 |
} |
| 4052 |
|
| 4053 |
int srt::CUDT::send(SRTSOCKET u, const char* buf, int len, int) |
| 4054 |
{ |
| 4055 |
SRT_MSGCTRL mctrl = srt_msgctrl_default; |
| 4056 |
return sendmsg2(u, buf, len, (mctrl)); |
| 4057 |
} |
| 4058 |
|
| 4059 |
// --> CUDT::recv moved down |
| 4060 |
|
| 4061 |
int srt::CUDT::sendmsg(SRTSOCKET u, const char* buf, int len, int ttl, bool inorder, int64_t srctime) |
| 4062 |
{ |
| 4063 |
SRT_MSGCTRL mctrl = srt_msgctrl_default; |
| 4064 |
mctrl.msgttl = ttl; |
| 4065 |
mctrl.inorder = inorder; |
| 4066 |
mctrl.srctime = srctime; |
| 4067 |
return sendmsg2(u, buf, len, (mctrl)); |
| 4068 |
} |
| 4069 |
|
| 4070 |
int srt::CUDT::sendmsg2(SRTSOCKET u, const char* buf, int len, SRT_MSGCTRL& w_m) |
| 4071 |
{ |
| 4072 |
try |
| 4073 |
{ |
| 4074 |
#if ENABLE_BONDING |
| 4075 |
if (u & SRTGROUP_MASK) |
| 4076 |
{ |
| 4077 |
CUDTUnited::GroupKeeper k(uglobal(), u, CUDTUnited::ERH_THROW); |
| 4078 |
return k.group->send(buf, len, (w_m)); |
| 4079 |
} |
| 4080 |
#endif |
| 4081 |
|
| 4082 |
return uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core().sendmsg2(buf, len, (w_m)); |
| 4083 |
} |
| 4084 |
catch (const CUDTException& e) |
| 4085 |
{ |
| 4086 |
return APIError(e); |
| 4087 |
} |
| 4088 |
catch (bad_alloc&) |
| 4089 |
{ |
| 4090 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 4091 |
} |
| 4092 |
catch (const std::exception& ee) |
| 4093 |
{ |
| 4094 |
LOGC(aclog.Fatal, log << "sendmsg: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4095 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4096 |
} |
| 4097 |
} |
| 4098 |
|
| 4099 |
int srt::CUDT::recv(SRTSOCKET u, char* buf, int len, int) |
| 4100 |
{ |
| 4101 |
SRT_MSGCTRL mctrl = srt_msgctrl_default; |
| 4102 |
int ret = recvmsg2(u, buf, len, (mctrl)); |
| 4103 |
return ret; |
| 4104 |
} |
| 4105 |
|
| 4106 |
int srt::CUDT::recvmsg(SRTSOCKET u, char* buf, int len, int64_t& srctime) |
| 4107 |
{ |
| 4108 |
SRT_MSGCTRL mctrl = srt_msgctrl_default; |
| 4109 |
int ret = recvmsg2(u, buf, len, (mctrl)); |
| 4110 |
srctime = mctrl.srctime; |
| 4111 |
return ret; |
| 4112 |
} |
| 4113 |
|
| 4114 |
int srt::CUDT::recvmsg2(SRTSOCKET u, char* buf, int len, SRT_MSGCTRL& w_m) |
| 4115 |
{ |
| 4116 |
try |
| 4117 |
{ |
| 4118 |
#if ENABLE_BONDING |
| 4119 |
if (u & SRTGROUP_MASK) |
| 4120 |
{ |
| 4121 |
CUDTUnited::GroupKeeper k(uglobal(), u, CUDTUnited::ERH_THROW); |
| 4122 |
return k.group->recv(buf, len, (w_m)); |
| 4123 |
} |
| 4124 |
#endif |
| 4125 |
|
| 4126 |
return uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core().recvmsg2(buf, len, (w_m)); |
| 4127 |
} |
| 4128 |
catch (const CUDTException& e) |
| 4129 |
{ |
| 4130 |
return APIError(e); |
| 4131 |
} |
| 4132 |
catch (const std::exception& ee) |
| 4133 |
{ |
| 4134 |
LOGC(aclog.Fatal, log << "recvmsg: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4135 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4136 |
} |
| 4137 |
} |
| 4138 |
|
| 4139 |
int64_t srt::CUDT::sendfile(SRTSOCKET u, fstream& ifs, int64_t& offset, int64_t size, int block) |
| 4140 |
{ |
| 4141 |
try |
| 4142 |
{ |
| 4143 |
CUDT& udt = uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core(); |
| 4144 |
return udt.sendfile(ifs, offset, size, block); |
| 4145 |
} |
| 4146 |
catch (const CUDTException& e) |
| 4147 |
{ |
| 4148 |
return APIError(e); |
| 4149 |
} |
| 4150 |
catch (bad_alloc&) |
| 4151 |
{ |
| 4152 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 4153 |
} |
| 4154 |
catch (const std::exception& ee) |
| 4155 |
{ |
| 4156 |
LOGC(aclog.Fatal, log << "sendfile: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4157 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4158 |
} |
| 4159 |
} |
| 4160 |
|
| 4161 |
int64_t srt::CUDT::recvfile(SRTSOCKET u, fstream& ofs, int64_t& offset, int64_t size, int block) |
| 4162 |
{ |
| 4163 |
try |
| 4164 |
{ |
| 4165 |
return uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core().recvfile(ofs, offset, size, block); |
| 4166 |
} |
| 4167 |
catch (const CUDTException& e) |
| 4168 |
{ |
| 4169 |
return APIError(e); |
| 4170 |
} |
| 4171 |
catch (const std::exception& ee) |
| 4172 |
{ |
| 4173 |
LOGC(aclog.Fatal, log << "recvfile: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4174 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4175 |
} |
| 4176 |
} |
| 4177 |
|
| 4178 |
int srt::CUDT::select(int, UDT::UDSET* readfds, UDT::UDSET* writefds, UDT::UDSET* exceptfds, const timeval* timeout) |
| 4179 |
{ |
| 4180 |
if ((!readfds) && (!writefds) && (!exceptfds)) |
| 4181 |
{ |
| 4182 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 4183 |
} |
| 4184 |
|
| 4185 |
try |
| 4186 |
{ |
| 4187 |
return uglobal().select(readfds, writefds, exceptfds, timeout); |
| 4188 |
} |
| 4189 |
catch (const CUDTException& e) |
| 4190 |
{ |
| 4191 |
return APIError(e); |
| 4192 |
} |
| 4193 |
catch (bad_alloc&) |
| 4194 |
{ |
| 4195 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 4196 |
} |
| 4197 |
catch (const std::exception& ee) |
| 4198 |
{ |
| 4199 |
LOGC(aclog.Fatal, log << "select: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4200 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4201 |
} |
| 4202 |
} |
| 4203 |
|
| 4204 |
int srt::CUDT::selectEx(const vector<SRTSOCKET>& fds, |
| 4205 |
vector<SRTSOCKET>* readfds, |
| 4206 |
vector<SRTSOCKET>* writefds, |
| 4207 |
vector<SRTSOCKET>* exceptfds, |
| 4208 |
int64_t msTimeOut) |
| 4209 |
{ |
| 4210 |
if ((!readfds) && (!writefds) && (!exceptfds)) |
| 4211 |
{ |
| 4212 |
return APIError(MJ_NOTSUP, MN_INVAL, 0); |
| 4213 |
} |
| 4214 |
|
| 4215 |
try |
| 4216 |
{ |
| 4217 |
return uglobal().selectEx(fds, readfds, writefds, exceptfds, msTimeOut); |
| 4218 |
} |
| 4219 |
catch (const CUDTException& e) |
| 4220 |
{ |
| 4221 |
return APIError(e); |
| 4222 |
} |
| 4223 |
catch (bad_alloc&) |
| 4224 |
{ |
| 4225 |
return APIError(MJ_SYSTEMRES, MN_MEMORY, 0); |
| 4226 |
} |
| 4227 |
catch (const std::exception& ee) |
| 4228 |
{ |
| 4229 |
LOGC(aclog.Fatal, log << "selectEx: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4230 |
return APIError(MJ_UNKNOWN); |
| 4231 |
} |
| 4232 |
} |
| 4233 |
|
| 4234 |
int srt::CUDT::epoll_create() |
| 4235 |
{ |
| 4236 |
try |
| 4237 |
{ |
| 4238 |
return uglobal().epoll_create(); |
| 4239 |
} |
| 4240 |
catch (const CUDTException& e) |
| 4241 |
{ |
| 4242 |
return APIError(e); |
| 4243 |
} |
| 4244 |
catch (const std::exception& ee) |
| 4245 |
{ |
| 4246 |
LOGC(aclog.Fatal, log << "epoll_create: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4247 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4248 |
} |
| 4249 |
} |
| 4250 |
|
| 4251 |
int srt::CUDT::epoll_clear_usocks(int eid) |
| 4252 |
{ |
| 4253 |
try |
| 4254 |
{ |
| 4255 |
return uglobal().epoll_clear_usocks(eid); |
| 4256 |
} |
| 4257 |
catch (const CUDTException& e) |
| 4258 |
{ |
| 4259 |
return APIError(e); |
| 4260 |
} |
| 4261 |
catch (std::exception& ee) |
| 4262 |
{ |
| 4263 |
LOGC(aclog.Fatal, |
| 4264 |
log << "epoll_clear_usocks: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4265 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4266 |
} |
| 4267 |
} |
| 4268 |
|
| 4269 |
int srt::CUDT::epoll_add_usock(const int eid, const SRTSOCKET u, const int* events) |
| 4270 |
{ |
| 4271 |
try |
| 4272 |
{ |
| 4273 |
return uglobal().epoll_add_usock(eid, u, events); |
| 4274 |
} |
| 4275 |
catch (const CUDTException& e) |
| 4276 |
{ |
| 4277 |
return APIError(e); |
| 4278 |
} |
| 4279 |
catch (const std::exception& ee) |
| 4280 |
{ |
| 4281 |
LOGC(aclog.Fatal, log << "epoll_add_usock: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4282 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4283 |
} |
| 4284 |
} |
| 4285 |
|
| 4286 |
int srt::CUDT::epoll_add_ssock(const int eid, const SYSSOCKET s, const int* events) |
| 4287 |
{ |
| 4288 |
try |
| 4289 |
{ |
| 4290 |
return uglobal().epoll_add_ssock(eid, s, events); |
| 4291 |
} |
| 4292 |
catch (const CUDTException& e) |
| 4293 |
{ |
| 4294 |
return APIError(e); |
| 4295 |
} |
| 4296 |
catch (const std::exception& ee) |
| 4297 |
{ |
| 4298 |
LOGC(aclog.Fatal, log << "epoll_add_ssock: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4299 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4300 |
} |
| 4301 |
} |
| 4302 |
|
| 4303 |
int srt::CUDT::epoll_update_usock(const int eid, const SRTSOCKET u, const int* events) |
| 4304 |
{ |
| 4305 |
try |
| 4306 |
{ |
| 4307 |
return uglobal().epoll_add_usock(eid, u, events); |
| 4308 |
} |
| 4309 |
catch (const CUDTException& e) |
| 4310 |
{ |
| 4311 |
return APIError(e); |
| 4312 |
} |
| 4313 |
catch (const std::exception& ee) |
| 4314 |
{ |
| 4315 |
LOGC(aclog.Fatal, |
| 4316 |
log << "epoll_update_usock: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4317 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4318 |
} |
| 4319 |
} |
| 4320 |
|
| 4321 |
int srt::CUDT::epoll_update_ssock(const int eid, const SYSSOCKET s, const int* events) |
| 4322 |
{ |
| 4323 |
try |
| 4324 |
{ |
| 4325 |
return uglobal().epoll_update_ssock(eid, s, events); |
| 4326 |
} |
| 4327 |
catch (const CUDTException& e) |
| 4328 |
{ |
| 4329 |
return APIError(e); |
| 4330 |
} |
| 4331 |
catch (const std::exception& ee) |
| 4332 |
{ |
| 4333 |
LOGC(aclog.Fatal, |
| 4334 |
log << "epoll_update_ssock: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4335 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4336 |
} |
| 4337 |
} |
| 4338 |
|
| 4339 |
int srt::CUDT::epoll_remove_usock(const int eid, const SRTSOCKET u) |
| 4340 |
{ |
| 4341 |
try |
| 4342 |
{ |
| 4343 |
return uglobal().epoll_remove_usock(eid, u); |
| 4344 |
} |
| 4345 |
catch (const CUDTException& e) |
| 4346 |
{ |
| 4347 |
return APIError(e); |
| 4348 |
} |
| 4349 |
catch (const std::exception& ee) |
| 4350 |
{ |
| 4351 |
LOGC(aclog.Fatal, |
| 4352 |
log << "epoll_remove_usock: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4353 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4354 |
} |
| 4355 |
} |
| 4356 |
|
| 4357 |
int srt::CUDT::epoll_remove_ssock(const int eid, const SYSSOCKET s) |
| 4358 |
{ |
| 4359 |
try |
| 4360 |
{ |
| 4361 |
return uglobal().epoll_remove_ssock(eid, s); |
| 4362 |
} |
| 4363 |
catch (const CUDTException& e) |
| 4364 |
{ |
| 4365 |
return APIError(e); |
| 4366 |
} |
| 4367 |
catch (const std::exception& ee) |
| 4368 |
{ |
| 4369 |
LOGC(aclog.Fatal, |
| 4370 |
log << "epoll_remove_ssock: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4371 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4372 |
} |
| 4373 |
} |
| 4374 |
|
| 4375 |
int srt::CUDT::epoll_wait(const int eid, |
| 4376 |
set<SRTSOCKET>* readfds, |
| 4377 |
set<SRTSOCKET>* writefds, |
| 4378 |
int64_t msTimeOut, |
| 4379 |
set<SYSSOCKET>* lrfds, |
| 4380 |
set<SYSSOCKET>* lwfds) |
| 4381 |
{ |
| 4382 |
try |
| 4383 |
{ |
| 4384 |
return uglobal().epoll_ref().wait(eid, readfds, writefds, msTimeOut, lrfds, lwfds); |
| 4385 |
} |
| 4386 |
catch (const CUDTException& e) |
| 4387 |
{ |
| 4388 |
return APIError(e); |
| 4389 |
} |
| 4390 |
catch (const std::exception& ee) |
| 4391 |
{ |
| 4392 |
LOGC(aclog.Fatal, log << "epoll_wait: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4393 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4394 |
} |
| 4395 |
} |
| 4396 |
|
| 4397 |
int srt::CUDT::epoll_uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut) |
| 4398 |
{ |
| 4399 |
try |
| 4400 |
{ |
| 4401 |
return uglobal().epoll_uwait(eid, fdsSet, fdsSize, msTimeOut); |
| 4402 |
} |
| 4403 |
catch (const CUDTException& e) |
| 4404 |
{ |
| 4405 |
return APIError(e); |
| 4406 |
} |
| 4407 |
catch (const std::exception& ee) |
| 4408 |
{ |
| 4409 |
LOGC(aclog.Fatal, log << "epoll_uwait: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4410 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4411 |
} |
| 4412 |
} |
| 4413 |
|
| 4414 |
int32_t srt::CUDT::epoll_set(const int eid, int32_t flags) |
| 4415 |
{ |
| 4416 |
try |
| 4417 |
{ |
| 4418 |
return uglobal().epoll_set(eid, flags); |
| 4419 |
} |
| 4420 |
catch (const CUDTException& e) |
| 4421 |
{ |
| 4422 |
return APIError(e); |
| 4423 |
} |
| 4424 |
catch (const std::exception& ee) |
| 4425 |
{ |
| 4426 |
LOGC(aclog.Fatal, log << "epoll_set: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4427 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4428 |
} |
| 4429 |
} |
| 4430 |
|
| 4431 |
int srt::CUDT::epoll_release(const int eid) |
| 4432 |
{ |
| 4433 |
try |
| 4434 |
{ |
| 4435 |
return uglobal().epoll_release(eid); |
| 4436 |
} |
| 4437 |
catch (const CUDTException& e) |
| 4438 |
{ |
| 4439 |
return APIError(e); |
| 4440 |
} |
| 4441 |
catch (const std::exception& ee) |
| 4442 |
{ |
| 4443 |
LOGC(aclog.Fatal, log << "epoll_release: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4444 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4445 |
} |
| 4446 |
} |
| 4447 |
|
| 4448 |
srt::CUDTException& srt::CUDT::getlasterror() |
| 4449 |
{ |
| 4450 |
return GetThreadLocalError(); |
| 4451 |
} |
| 4452 |
|
| 4453 |
int srt::CUDT::bstats(SRTSOCKET u, CBytePerfMon* perf, bool clear, bool instantaneous) |
| 4454 |
{ |
| 4455 |
#if ENABLE_BONDING |
| 4456 |
if (u & SRTGROUP_MASK) |
| 4457 |
return groupsockbstats(u, perf, clear); |
| 4458 |
#endif |
| 4459 |
|
| 4460 |
try |
| 4461 |
{ |
| 4462 |
CUDT& udt = uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core(); |
| 4463 |
udt.bstats(perf, clear, instantaneous); |
| 4464 |
return 0; |
| 4465 |
} |
| 4466 |
catch (const CUDTException& e) |
| 4467 |
{ |
| 4468 |
return APIError(e); |
| 4469 |
} |
| 4470 |
catch (const std::exception& ee) |
| 4471 |
{ |
| 4472 |
LOGC(aclog.Fatal, log << "bstats: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4473 |
return APIError(MJ_UNKNOWN, MN_NONE, 0); |
| 4474 |
} |
| 4475 |
} |
| 4476 |
|
| 4477 |
#if ENABLE_BONDING |
| 4478 |
int srt::CUDT::groupsockbstats(SRTSOCKET u, CBytePerfMon* perf, bool clear) |
| 4479 |
{ |
| 4480 |
try |
| 4481 |
{ |
| 4482 |
CUDTUnited::GroupKeeper k(uglobal(), u, CUDTUnited::ERH_THROW); |
| 4483 |
k.group->bstatsSocket(perf, clear); |
| 4484 |
return 0; |
| 4485 |
} |
| 4486 |
catch (const CUDTException& e) |
| 4487 |
{ |
| 4488 |
SetThreadLocalError(e); |
| 4489 |
return ERROR; |
| 4490 |
} |
| 4491 |
catch (const std::exception& ee) |
| 4492 |
{ |
| 4493 |
LOGC(aclog.Fatal, log << "bstats: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4494 |
SetThreadLocalError(CUDTException(MJ_UNKNOWN, MN_NONE, 0)); |
| 4495 |
return ERROR; |
| 4496 |
} |
| 4497 |
} |
| 4498 |
#endif |
| 4499 |
|
| 4500 |
srt::CUDT* srt::CUDT::getUDTHandle(SRTSOCKET u) |
| 4501 |
{ |
| 4502 |
try |
| 4503 |
{ |
| 4504 |
return &uglobal().locateSocket(u, CUDTUnited::ERH_THROW)->core(); |
| 4505 |
} |
| 4506 |
catch (const CUDTException& e) |
| 4507 |
{ |
| 4508 |
SetThreadLocalError(e); |
| 4509 |
return NULL; |
| 4510 |
} |
| 4511 |
catch (const std::exception& ee) |
| 4512 |
{ |
| 4513 |
LOGC(aclog.Fatal, log << "getUDTHandle: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4514 |
SetThreadLocalError(CUDTException(MJ_UNKNOWN, MN_NONE, 0)); |
| 4515 |
return NULL; |
| 4516 |
} |
| 4517 |
} |
| 4518 |
|
| 4519 |
vector<SRTSOCKET> srt::CUDT::existingSockets() |
| 4520 |
{ |
| 4521 |
vector<SRTSOCKET> out; |
| 4522 |
for (CUDTUnited::sockets_t::iterator i = uglobal().m_Sockets.begin(); i != uglobal().m_Sockets.end(); ++i) |
| 4523 |
{ |
| 4524 |
out.push_back(i->first); |
| 4525 |
} |
| 4526 |
return out; |
| 4527 |
} |
| 4528 |
|
| 4529 |
SRT_SOCKSTATUS srt::CUDT::getsockstate(SRTSOCKET u) |
| 4530 |
{ |
| 4531 |
try |
| 4532 |
{ |
| 4533 |
#if ENABLE_BONDING |
| 4534 |
if (isgroup(u)) |
| 4535 |
{ |
| 4536 |
CUDTUnited::GroupKeeper k(uglobal(), u, CUDTUnited::ERH_THROW); |
| 4537 |
return k.group->getStatus(); |
| 4538 |
} |
| 4539 |
#endif |
| 4540 |
return uglobal().getStatus(u); |
| 4541 |
} |
| 4542 |
catch (const CUDTException& e) |
| 4543 |
{ |
| 4544 |
SetThreadLocalError(e); |
| 4545 |
return SRTS_NONEXIST; |
| 4546 |
} |
| 4547 |
catch (const std::exception& ee) |
| 4548 |
{ |
| 4549 |
LOGC(aclog.Fatal, log << "getsockstate: UNEXPECTED EXCEPTION: " << typeid(ee).name() << ": " << ee.what()); |
| 4550 |
SetThreadLocalError(CUDTException(MJ_UNKNOWN, MN_NONE, 0)); |
| 4551 |
return SRTS_NONEXIST; |
| 4552 |
} |
| 4553 |
} |
| 4554 |
|
| 4555 |
//////////////////////////////////////////////////////////////////////////////// |
| 4556 |
|
| 4557 |
namespace UDT |
| 4558 |
{ |
| 4559 |
|
| 4560 |
int startup() |
| 4561 |
{ |
| 4562 |
return srt::CUDT::startup(); |
| 4563 |
} |
| 4564 |
|
| 4565 |
int cleanup() |
| 4566 |
{ |
| 4567 |
return srt::CUDT::cleanup(); |
| 4568 |
} |
| 4569 |
|
| 4570 |
int bind(SRTSOCKET u, const struct sockaddr* name, int namelen) |
| 4571 |
{ |
| 4572 |
return srt::CUDT::bind(u, name, namelen); |
| 4573 |
} |
| 4574 |
|
| 4575 |
int bind2(SRTSOCKET u, UDPSOCKET udpsock) |
| 4576 |
{ |
| 4577 |
return srt::CUDT::bind(u, udpsock); |
| 4578 |
} |
| 4579 |
|
| 4580 |
int listen(SRTSOCKET u, int backlog) |
| 4581 |
{ |
| 4582 |
return srt::CUDT::listen(u, backlog); |
| 4583 |
} |
| 4584 |
|
| 4585 |
SRTSOCKET accept(SRTSOCKET u, struct sockaddr* addr, int* addrlen) |
| 4586 |
{ |
| 4587 |
return srt::CUDT::accept(u, addr, addrlen); |
| 4588 |
} |
| 4589 |
|
| 4590 |
int connect(SRTSOCKET u, const struct sockaddr* name, int namelen) |
| 4591 |
{ |
| 4592 |
return srt::CUDT::connect(u, name, namelen, SRT_SEQNO_NONE); |
| 4593 |
} |
| 4594 |
|
| 4595 |
int close(SRTSOCKET u) |
| 4596 |
{ |
| 4597 |
return srt::CUDT::close(u); |
| 4598 |
} |
| 4599 |
|
| 4600 |
int getpeername(SRTSOCKET u, struct sockaddr* name, int* namelen) |
| 4601 |
{ |
| 4602 |
return srt::CUDT::getpeername(u, name, namelen); |
| 4603 |
} |
| 4604 |
|
| 4605 |
int getsockname(SRTSOCKET u, struct sockaddr* name, int* namelen) |
| 4606 |
{ |
| 4607 |
return srt::CUDT::getsockname(u, name, namelen); |
| 4608 |
} |
| 4609 |
|
| 4610 |
int getsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, void* optval, int* optlen) |
| 4611 |
{ |
| 4612 |
return srt::CUDT::getsockopt(u, level, optname, optval, optlen); |
| 4613 |
} |
| 4614 |
|
| 4615 |
int setsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, const void* optval, int optlen) |
| 4616 |
{ |
| 4617 |
return srt::CUDT::setsockopt(u, level, optname, optval, optlen); |
| 4618 |
} |
| 4619 |
|
| 4620 |
// DEVELOPER API |
| 4621 |
|
| 4622 |
int connect_debug(SRTSOCKET u, const struct sockaddr* name, int namelen, int32_t forced_isn) |
| 4623 |
{ |
| 4624 |
return srt::CUDT::connect(u, name, namelen, forced_isn); |
| 4625 |
} |
| 4626 |
|
| 4627 |
int send(SRTSOCKET u, const char* buf, int len, int flags) |
| 4628 |
{ |
| 4629 |
return srt::CUDT::send(u, buf, len, flags); |
| 4630 |
} |
| 4631 |
|
| 4632 |
int recv(SRTSOCKET u, char* buf, int len, int flags) |
| 4633 |
{ |
| 4634 |
return srt::CUDT::recv(u, buf, len, flags); |
| 4635 |
} |
| 4636 |
|
| 4637 |
int sendmsg(SRTSOCKET u, const char* buf, int len, int ttl, bool inorder, int64_t srctime) |
| 4638 |
{ |
| 4639 |
return srt::CUDT::sendmsg(u, buf, len, ttl, inorder, srctime); |
| 4640 |
} |
| 4641 |
|
| 4642 |
int recvmsg(SRTSOCKET u, char* buf, int len, int64_t& srctime) |
| 4643 |
{ |
| 4644 |
return srt::CUDT::recvmsg(u, buf, len, srctime); |
| 4645 |
} |
| 4646 |
|
| 4647 |
int recvmsg(SRTSOCKET u, char* buf, int len) |
| 4648 |
{ |
| 4649 |
int64_t srctime; |
| 4650 |
return srt::CUDT::recvmsg(u, buf, len, srctime); |
| 4651 |
} |
| 4652 |
|
| 4653 |
int64_t sendfile(SRTSOCKET u, fstream& ifs, int64_t& offset, int64_t size, int block) |
| 4654 |
{ |
| 4655 |
return srt::CUDT::sendfile(u, ifs, offset, size, block); |
| 4656 |
} |
| 4657 |
|
| 4658 |
int64_t recvfile(SRTSOCKET u, fstream& ofs, int64_t& offset, int64_t size, int block) |
| 4659 |
{ |
| 4660 |
return srt::CUDT::recvfile(u, ofs, offset, size, block); |
| 4661 |
} |
| 4662 |
|
| 4663 |
int64_t sendfile2(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block) |
| 4664 |
{ |
| 4665 |
fstream ifs(path, ios::binary | ios::in); |
| 4666 |
int64_t ret = srt::CUDT::sendfile(u, ifs, *offset, size, block); |
| 4667 |
ifs.close(); |
| 4668 |
return ret; |
| 4669 |
} |
| 4670 |
|
| 4671 |
int64_t recvfile2(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block) |
| 4672 |
{ |
| 4673 |
fstream ofs(path, ios::binary | ios::out); |
| 4674 |
int64_t ret = srt::CUDT::recvfile(u, ofs, *offset, size, block); |
| 4675 |
ofs.close(); |
| 4676 |
return ret; |
| 4677 |
} |
| 4678 |
|
| 4679 |
int select(int nfds, UDSET* readfds, UDSET* writefds, UDSET* exceptfds, const struct timeval* timeout) |
| 4680 |
{ |
| 4681 |
return srt::CUDT::select(nfds, readfds, writefds, exceptfds, timeout); |
| 4682 |
} |
| 4683 |
|
| 4684 |
int selectEx(const vector<SRTSOCKET>& fds, |
| 4685 |
vector<SRTSOCKET>* readfds, |
| 4686 |
vector<SRTSOCKET>* writefds, |
| 4687 |
vector<SRTSOCKET>* exceptfds, |
| 4688 |
int64_t msTimeOut) |
| 4689 |
{ |
| 4690 |
return srt::CUDT::selectEx(fds, readfds, writefds, exceptfds, msTimeOut); |
| 4691 |
} |
| 4692 |
|
| 4693 |
int epoll_create() |
| 4694 |
{ |
| 4695 |
return srt::CUDT::epoll_create(); |
| 4696 |
} |
| 4697 |
|
| 4698 |
int epoll_clear_usocks(int eid) |
| 4699 |
{ |
| 4700 |
return srt::CUDT::epoll_clear_usocks(eid); |
| 4701 |
} |
| 4702 |
|
| 4703 |
int epoll_add_usock(int eid, SRTSOCKET u, const int* events) |
| 4704 |
{ |
| 4705 |
return srt::CUDT::epoll_add_usock(eid, u, events); |
| 4706 |
} |
| 4707 |
|
| 4708 |
int epoll_add_ssock(int eid, SYSSOCKET s, const int* events) |
| 4709 |
{ |
| 4710 |
return srt::CUDT::epoll_add_ssock(eid, s, events); |
| 4711 |
} |
| 4712 |
|
| 4713 |
int epoll_update_usock(int eid, SRTSOCKET u, const int* events) |
| 4714 |
{ |
| 4715 |
return srt::CUDT::epoll_update_usock(eid, u, events); |
| 4716 |
} |
| 4717 |
|
| 4718 |
int epoll_update_ssock(int eid, SYSSOCKET s, const int* events) |
| 4719 |
{ |
| 4720 |
return srt::CUDT::epoll_update_ssock(eid, s, events); |
| 4721 |
} |
| 4722 |
|
| 4723 |
int epoll_remove_usock(int eid, SRTSOCKET u) |
| 4724 |
{ |
| 4725 |
return srt::CUDT::epoll_remove_usock(eid, u); |
| 4726 |
} |
| 4727 |
|
| 4728 |
int epoll_remove_ssock(int eid, SYSSOCKET s) |
| 4729 |
{ |
| 4730 |
return srt::CUDT::epoll_remove_ssock(eid, s); |
| 4731 |
} |
| 4732 |
|
| 4733 |
int epoll_wait(int eid, |
| 4734 |
set<SRTSOCKET>* readfds, |
| 4735 |
set<SRTSOCKET>* writefds, |
| 4736 |
int64_t msTimeOut, |
| 4737 |
set<SYSSOCKET>* lrfds, |
| 4738 |
set<SYSSOCKET>* lwfds) |
| 4739 |
{ |
| 4740 |
return srt::CUDT::epoll_wait(eid, readfds, writefds, msTimeOut, lrfds, lwfds); |
| 4741 |
} |
| 4742 |
|
| 4743 |
template <class SOCKTYPE> |
| 4744 |
inline void set_result(set<SOCKTYPE>* val, int* num, SOCKTYPE* fds) |
| 4745 |
{ |
| 4746 |
if (!val || !num || !fds) |
| 4747 |
return; |
| 4748 |
|
| 4749 |
if (*num > int(val->size())) |
| 4750 |
*num = int(val->size()); // will get 0 if val->empty() |
| 4751 |
int count = 0; |
| 4752 |
|
| 4753 |
// This loop will run 0 times if val->empty() |
| 4754 |
for (typename set<SOCKTYPE>::const_iterator it = val->begin(); it != val->end(); ++it) |
| 4755 |
{ |
| 4756 |
if (count >= *num) |
| 4757 |
break; |
| 4758 |
fds[count++] = *it; |
| 4759 |
} |
| 4760 |
} |
| 4761 |
|
| 4762 |
int epoll_wait2(int eid, |
| 4763 |
SRTSOCKET* readfds, |
| 4764 |
int* rnum, |
| 4765 |
SRTSOCKET* writefds, |
| 4766 |
int* wnum, |
| 4767 |
int64_t msTimeOut, |
| 4768 |
SYSSOCKET* lrfds, |
| 4769 |
int* lrnum, |
| 4770 |
SYSSOCKET* lwfds, |
| 4771 |
int* lwnum) |
| 4772 |
{ |
| 4773 |
// This API is an alternative format for epoll_wait, created for |
| 4774 |
// compatibility with other languages. Users need to pass in an array |
| 4775 |
// for holding the returned sockets, with the maximum array length |
| 4776 |
// stored in *rnum, etc., which will be updated with returned number |
| 4777 |
// of sockets. |
| 4778 |
|
| 4779 |
set<SRTSOCKET> readset; |
| 4780 |
set<SRTSOCKET> writeset; |
| 4781 |
set<SYSSOCKET> lrset; |
| 4782 |
set<SYSSOCKET> lwset; |
| 4783 |
set<SRTSOCKET>* rval = NULL; |
| 4784 |
set<SRTSOCKET>* wval = NULL; |
| 4785 |
set<SYSSOCKET>* lrval = NULL; |
| 4786 |
set<SYSSOCKET>* lwval = NULL; |
| 4787 |
if ((readfds != NULL) && (rnum != NULL)) |
| 4788 |
rval = &readset; |
| 4789 |
if ((writefds != NULL) && (wnum != NULL)) |
| 4790 |
wval = &writeset; |
| 4791 |
if ((lrfds != NULL) && (lrnum != NULL)) |
| 4792 |
lrval = &lrset; |
| 4793 |
if ((lwfds != NULL) && (lwnum != NULL)) |
| 4794 |
lwval = &lwset; |
| 4795 |
|
| 4796 |
int ret = srt::CUDT::epoll_wait(eid, rval, wval, msTimeOut, lrval, lwval); |
| 4797 |
if (ret > 0) |
| 4798 |
{ |
| 4799 |
// set<SRTSOCKET>::const_iterator i; |
| 4800 |
// SET_RESULT(rval, rnum, readfds, i); |
| 4801 |
set_result(rval, rnum, readfds); |
| 4802 |
// SET_RESULT(wval, wnum, writefds, i); |
| 4803 |
set_result(wval, wnum, writefds); |
| 4804 |
|
| 4805 |
// set<SYSSOCKET>::const_iterator j; |
| 4806 |
// SET_RESULT(lrval, lrnum, lrfds, j); |
| 4807 |
set_result(lrval, lrnum, lrfds); |
| 4808 |
// SET_RESULT(lwval, lwnum, lwfds, j); |
| 4809 |
set_result(lwval, lwnum, lwfds); |
| 4810 |
} |
| 4811 |
return ret; |
| 4812 |
} |
| 4813 |
|
| 4814 |
int epoll_uwait(int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut) |
| 4815 |
{ |
| 4816 |
return srt::CUDT::epoll_uwait(eid, fdsSet, fdsSize, msTimeOut); |
| 4817 |
} |
| 4818 |
|
| 4819 |
int epoll_release(int eid) |
| 4820 |
{ |
| 4821 |
return srt::CUDT::epoll_release(eid); |
| 4822 |
} |
| 4823 |
|
| 4824 |
ERRORINFO& getlasterror() |
| 4825 |
{ |
| 4826 |
return srt::CUDT::getlasterror(); |
| 4827 |
} |
| 4828 |
|
| 4829 |
int getlasterror_code() |
| 4830 |
{ |
| 4831 |
return srt::CUDT::getlasterror().getErrorCode(); |
| 4832 |
} |
| 4833 |
|
| 4834 |
const char* getlasterror_desc() |
| 4835 |
{ |
| 4836 |
return srt::CUDT::getlasterror().getErrorMessage(); |
| 4837 |
} |
| 4838 |
|
| 4839 |
int getlasterror_errno() |
| 4840 |
{ |
| 4841 |
return srt::CUDT::getlasterror().getErrno(); |
| 4842 |
} |
| 4843 |
|
| 4844 |
// Get error string of a given error code |
| 4845 |
const char* geterror_desc(int code, int err) |
| 4846 |
{ |
| 4847 |
srt::CUDTException e(CodeMajor(code / 1000), CodeMinor(code % 1000), err); |
| 4848 |
return (e.getErrorMessage()); |
| 4849 |
} |
| 4850 |
|
| 4851 |
int bstats(SRTSOCKET u, SRT_TRACEBSTATS* perf, bool clear) |
| 4852 |
{ |
| 4853 |
return srt::CUDT::bstats(u, perf, clear); |
| 4854 |
} |
| 4855 |
|
| 4856 |
SRT_SOCKSTATUS getsockstate(SRTSOCKET u) |
| 4857 |
{ |
| 4858 |
return srt::CUDT::getsockstate(u); |
| 4859 |
} |
| 4860 |
|
| 4861 |
} // namespace UDT |
| 4862 |
|
| 4863 |
namespace srt |
| 4864 |
{ |
| 4865 |
|
| 4866 |
void setloglevel(LogLevel::type ll) |
| 4867 |
{ |
| 4868 |
ScopedLock gg(srt_logger_config.mutex); |
| 4869 |
srt_logger_config.max_level = ll; |
| 4870 |
} |
| 4871 |
|
| 4872 |
void addlogfa(LogFA fa) |
| 4873 |
{ |
| 4874 |
ScopedLock gg(srt_logger_config.mutex); |
| 4875 |
srt_logger_config.enabled_fa.set(fa, true); |
| 4876 |
} |
| 4877 |
|
| 4878 |
void dellogfa(LogFA fa) |
| 4879 |
{ |
| 4880 |
ScopedLock gg(srt_logger_config.mutex); |
| 4881 |
srt_logger_config.enabled_fa.set(fa, false); |
| 4882 |
} |
| 4883 |
|
| 4884 |
void resetlogfa(set<LogFA> fas) |
| 4885 |
{ |
| 4886 |
ScopedLock gg(srt_logger_config.mutex); |
| 4887 |
for (int i = 0; i <= SRT_LOGFA_LASTNONE; ++i) |
| 4888 |
srt_logger_config.enabled_fa.set(i, fas.count(i)); |
| 4889 |
} |
| 4890 |
|
| 4891 |
void resetlogfa(const int* fara, size_t fara_size) |
| 4892 |
{ |
| 4893 |
ScopedLock gg(srt_logger_config.mutex); |
| 4894 |
srt_logger_config.enabled_fa.reset(); |
| 4895 |
for (const int* i = fara; i != fara + fara_size; ++i) |
| 4896 |
srt_logger_config.enabled_fa.set(*i, true); |
| 4897 |
} |
| 4898 |
|
| 4899 |
void setlogstream(std::ostream& stream) |
| 4900 |
{ |
| 4901 |
ScopedLock gg(srt_logger_config.mutex); |
| 4902 |
srt_logger_config.log_stream = &stream; |
| 4903 |
} |
| 4904 |
|
| 4905 |
void setloghandler(void* opaque, SRT_LOG_HANDLER_FN* handler) |
| 4906 |
{ |
| 4907 |
ScopedLock gg(srt_logger_config.mutex); |
| 4908 |
srt_logger_config.loghandler_opaque = opaque; |
| 4909 |
srt_logger_config.loghandler_fn = handler; |
| 4910 |
} |
| 4911 |
|
| 4912 |
void setlogflags(int flags) |
| 4913 |
{ |
| 4914 |
ScopedLock gg(srt_logger_config.mutex); |
| 4915 |
srt_logger_config.flags = flags; |
| 4916 |
} |
| 4917 |
|
| 4918 |
SRT_API bool setstreamid(SRTSOCKET u, const std::string& sid) |
| 4919 |
{ |
| 4920 |
return CUDT::setstreamid(u, sid); |
| 4921 |
} |
| 4922 |
SRT_API std::string getstreamid(SRTSOCKET u) |
| 4923 |
{ |
| 4924 |
return CUDT::getstreamid(u); |
| 4925 |
} |
| 4926 |
|
| 4927 |
int getrejectreason(SRTSOCKET u) |
| 4928 |
{ |
| 4929 |
return CUDT::rejectReason(u); |
| 4930 |
} |
| 4931 |
|
| 4932 |
int setrejectreason(SRTSOCKET u, int value) |
| 4933 |
{ |
| 4934 |
return CUDT::rejectReason(u, value); |
| 4935 |
} |
| 4936 |
|
| 4937 |
} // namespace srt |