| 1 |
#include "protoSocket.h" |
| 2 |
#include "protoSimAgent.h" |
| 3 |
#include "protoDebug.h" |
| 4 |
|
| 5 |
#include <errno.h> // for errno |
| 6 |
|
| 7 |
#ifdef SIMULATE |
| 8 |
const ProtoSocket::Handle ProtoSocket::INVALID_HANDLE = NULL; |
| 9 |
#endif // SIMULATE |
| 10 |
|
| 11 |
ProtoSocket::ProtoSocket(ProtoSocket::Protocol theProtocol) |
| 12 |
: domain(SIM), protocol(theProtocol), state(CLOSED), handle(INVALID_HANDLE), |
| 13 |
port(-1), |
| 14 |
#ifdef HAVE_IPV6 |
| 15 |
flow_label(0), |
| 16 |
#endif // HAVE_IPV6 |
| 17 |
notifier(NULL), notify_output(false), |
| 18 |
listener(NULL), user_data(NULL) |
| 19 |
{ |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
ProtoSocket::~ProtoSocket() |
| 24 |
{ |
| 25 |
Close(); |
| 26 |
if (listener) |
| 27 |
{ |
| 28 |
delete listener; |
| 29 |
listener = NULL; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
bool ProtoSocket::SetNotifier(ProtoSocket::Notifier* theNotifier) |
| 34 |
{ |
| 35 |
ASSERT(!IsOpen()); |
| 36 |
notifier = theNotifier; |
| 37 |
return true; |
| 38 |
} // end ProtoSocket::SetNotifier() |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* WIN32 needs the address type determine IPv6 _or_ IPv4 socket domain |
| 43 |
* @note WIN32 can't do IPv4 on an IPV6 socket! |
| 44 |
*/ |
| 45 |
bool ProtoSocket::Open(UINT16 thePort, |
| 46 |
ProtoAddress::Type /*addrType*/, |
| 47 |
bool bindOnOpen) |
| 48 |
{ |
| 49 |
if (IsOpen()) Close(); |
| 50 |
ProtoSimAgent* simAgent = static_cast<ProtoSimAgent*>(notifier); |
| 51 |
ASSERT(simAgent); |
| 52 |
if ((handle = simAgent->OpenSocket(*this))) |
| 53 |
{ |
| 54 |
state = IDLE; |
| 55 |
if (bindOnOpen) |
| 56 |
{ |
| 57 |
if (!Bind(thePort)) |
| 58 |
{ |
| 59 |
Close(); |
| 60 |
return false; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
fprintf(stderr, "ProtoSocket::Open() error creating socketAgent\n"); |
| 69 |
return false; |
| 70 |
} |
| 71 |
} // end ProtoSocket::Open() |
| 72 |
|
| 73 |
void ProtoSocket::Close() |
| 74 |
{ |
| 75 |
PLOG(PL_DETAIL, "ProtoSocket::Destructor Entering ...\n"); |
| 76 |
|
| 77 |
if (IsOpen()) |
| 78 |
{ |
| 79 |
ProtoSimAgent* simAgent = dynamic_cast<ProtoSimAgent*>(notifier); |
| 80 |
ASSERT(simAgent); |
| 81 |
simAgent->CloseSocket(*this); |
| 82 |
handle = INVALID_HANDLE; |
| 83 |
state = CLOSED; |
| 84 |
port = -1; |
| 85 |
} |
| 86 |
PLOG(PL_DETAIL, "ProtoSocket::Destructor Leaving ...\n"); |
| 87 |
} // end Close() |
| 88 |
|
| 89 |
bool ProtoSocket::Shutdown() |
| 90 |
{ |
| 91 |
if (!IsOpen()) return false; |
| 92 |
|
| 93 |
PLOG(PL_DETAIL, "ProtoSocket::Shutting socket down ... \n"); |
| 94 |
|
| 95 |
if (static_cast<ProtoSimAgent::SocketProxy*>(handle)->Shutdown()) |
| 96 |
{ |
| 97 |
return true; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
return false; |
| 102 |
} |
| 103 |
} // end ProtoSocket::Shutdown() |
| 104 |
|
| 105 |
bool ProtoSocket::Bind(UINT16 thePort, const ProtoAddress* /*localAddress*/) |
| 106 |
{ |
| 107 |
|
| 108 |
if (!IsOpen()) Open(thePort, ProtoAddress::SIM, FALSE); // I.T. Added 24/3/07 |
| 109 |
|
| 110 |
// if (IsOpen() && (port < 0)) |
| 111 |
// { |
| 112 |
if (static_cast<ProtoSimAgent::SocketProxy*>(handle)->Bind(thePort)) |
| 113 |
{ |
| 114 |
port = thePort; |
| 115 |
return true; |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
return false; |
| 120 |
} |
| 121 |
// } |
| 122 |
// else // I.T. Taken out, logic replaced by above |
| 123 |
// { |
| 124 |
// return Open(thePort); |
| 125 |
// } |
| 126 |
} // end ProtoSocket::Bind() |
| 127 |
|
| 128 |
bool ProtoSocket::Connect(const ProtoAddress& theAddress) |
| 129 |
{ |
| 130 |
if (!IsOpen()) Open(0, ProtoAddress::SIM, TRUE); // I.T. Added 24/3/07 - use 0, as default port if not set |
| 131 |
|
| 132 |
state = CONNECTING; // the CONNECT is generated from the CONNECT Event |
| 133 |
|
| 134 |
PLOG(PL_DETAIL, "ProtoSocket::Connect Connecting ... \n"); |
| 135 |
|
| 136 |
if (static_cast<ProtoSimAgent::SocketProxy*>(handle)->Connect(theAddress)) |
| 137 |
{ |
| 138 |
destination=theAddress; |
| 139 |
return true; |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
// I.T. Fix for UDP connect method invocations resulting in invalid state for OnNotify() 3/15/09 |
| 144 |
// If there is not connect method for the protocol then assume connection is not |
| 145 |
// necessary and reset flag |
| 146 |
state=IDLE; |
| 147 |
return false; |
| 148 |
} |
| 149 |
} // end bool ProtoSocket::Connect() |
| 150 |
|
| 151 |
void ProtoSocket::Disconnect() |
| 152 |
{ |
| 153 |
state = CLOSED; // I.T. Added 24/3/07 |
| 154 |
|
| 155 |
} // end ProtoSocket::Disconnect() |
| 156 |
|
| 157 |
bool ProtoSocket::Listen(UINT16 thePort) |
| 158 |
{ |
| 159 |
if (!IsOpen()) Open(thePort, ProtoAddress::SIM, TRUE); // I.T. Added 24/3/07 |
| 160 |
|
| 161 |
state = LISTENING; // I.T. Added 27/3/07 |
| 162 |
|
| 163 |
if (static_cast<ProtoSimAgent::SocketProxy*>(handle)->Listen(thePort)) |
| 164 |
{ |
| 165 |
return true; |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
return false; |
| 170 |
} |
| 171 |
} // end ProtoSocket::Listen() |
| 172 |
|
| 173 |
bool ProtoSocket::Accept(ProtoSocket* theSocket) |
| 174 |
{ |
| 175 |
if (static_cast<ProtoSimAgent::SocketProxy*>(handle)->Accept(theSocket)) |
| 176 |
{ |
| 177 |
theSocket->state = CONNECTED; // I.T. Added - need to let the socket know that it is connected |
| 178 |
return true; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
return false; |
| 183 |
} |
| 184 |
} // end ProtoSocket::Accept() |
| 185 |
|
| 186 |
bool ProtoSocket::Send(const char* buffer, |
| 187 |
unsigned int& numBytes) |
| 188 |
{ |
| 189 |
|
| 190 |
if (IsConnected()) |
| 191 |
{ |
| 192 |
PLOG(PL_DETAIL, "ProtoSocket::Send - sending now ... \n"); |
| 193 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->SendTo(buffer, numBytes, destination); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
fprintf(stderr, "ProtoSocket::Send() error: socket not connected\n"); |
| 198 |
return false; |
| 199 |
} |
| 200 |
} // end ProtoSocket::Send() |
| 201 |
|
| 202 |
bool ProtoSocket::Recv(char* buffer, |
| 203 |
unsigned int& numBytes) |
| 204 |
{ |
| 205 |
if (IsOpen()) |
| 206 |
{ |
| 207 |
PLOG(PL_DETAIL, "ProtoSocket::Recv receiving data ...\n"); |
| 208 |
ProtoAddress srcAddr; |
| 209 |
bool ret = static_cast<ProtoSimAgent::SocketProxy*>(handle)->RecvFrom(buffer, numBytes, srcAddr); |
| 210 |
PLOG(PL_DETAIL, "ProtoSocket::Recv data received ...\n"); |
| 211 |
destination=srcAddr; // I.T. 27/3/07 - set the sender address as the destination so it interfaces with ProtoSocket ... |
| 212 |
PLOG(PL_DETAIL, "ProtoSocket::Recv leaving ...\n"); |
| 213 |
return ret; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
fprintf(stderr, "ProtoSocket::Recv() error: socket not open\n"); |
| 218 |
return false; |
| 219 |
} |
| 220 |
} // end ProtoSocket::Recv() |
| 221 |
|
| 222 |
bool ProtoSocket::SendTo(const char* buffer, |
| 223 |
unsigned int buflen, |
| 224 |
const ProtoAddress& dstAddr) |
| 225 |
{ |
| 226 |
if (!IsOpen()) |
| 227 |
{ |
| 228 |
if (!Open()) |
| 229 |
{ |
| 230 |
fprintf(stderr, "ProtoSocket::SendTo() error opening socket\n"); |
| 231 |
return false; |
| 232 |
} |
| 233 |
} |
| 234 |
else if (TCP == protocol) |
| 235 |
{ |
| 236 |
if (!IsConnected() || dstAddr.IsEqual(destination)) Connect(dstAddr); |
| 237 |
if (IsConnected()) |
| 238 |
{ |
| 239 |
unsigned int put = 0; |
| 240 |
while (put < buflen) |
| 241 |
{ |
| 242 |
unsigned int numBytes = buflen - put; |
| 243 |
if (Send(buffer+put, numBytes)) |
| 244 |
{ |
| 245 |
put += numBytes; |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
fprintf(stderr, "ProtoSocket::SendTo() error sending to connected socket\n"); |
| 250 |
return false; |
| 251 |
} |
| 252 |
} |
| 253 |
return true; |
| 254 |
} |
| 255 |
} |
| 256 |
PLOG(PL_DETAIL, "ProtoSocket::SendTo sending data to Proxy ...\n"); |
| 257 |
return (static_cast<ProtoSimAgent::SocketProxy*>(handle))->SendTo(buffer, buflen, dstAddr); |
| 258 |
} // end ProtoSocket::SendTo() |
| 259 |
|
| 260 |
bool ProtoSocket::RecvFrom(char* buffer, |
| 261 |
unsigned int& numBytes, |
| 262 |
ProtoAddress& srcAddr) |
| 263 |
{ |
| 264 |
if (IsOpen()) |
| 265 |
{ |
| 266 |
PLOG(PL_DETAIL, "ProtoSocket::RecvFrom receiving data from Proxy ...\n"); |
| 267 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->RecvFrom(buffer, numBytes, srcAddr); |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
fprintf(stderr, "ProtoSocket::RecvFrom() error: socket not open\n"); |
| 272 |
return false; |
| 273 |
} |
| 274 |
} // end ProtoSocket::RecvFrom() |
| 275 |
|
| 276 |
bool ProtoSocket::JoinGroup(const ProtoAddress& groupAddr, |
| 277 |
const char* /*interfaceName*/, |
| 278 |
const ProtoAddress* /*sourceAddr*/) |
| 279 |
{ |
| 280 |
if (!IsOpen()) |
| 281 |
{ |
| 282 |
if (!Open()) |
| 283 |
{ |
| 284 |
fprintf(stderr, "ProtoSocket::JoinGroup() error opening socket\n"); |
| 285 |
return false; |
| 286 |
} |
| 287 |
} |
| 288 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->JoinGroup(groupAddr); |
| 289 |
} // end ProtoSocket::JoinGroup() |
| 290 |
|
| 291 |
bool ProtoSocket::LeaveGroup(const ProtoAddress& groupAddr, |
| 292 |
const char* /*interfaceName*/, |
| 293 |
const ProtoAddress* /*sourceAddr*/) |
| 294 |
{ |
| 295 |
if (IsOpen()) |
| 296 |
{ |
| 297 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->LeaveGroup(groupAddr); |
| 298 |
} |
| 299 |
else |
| 300 |
{ |
| 301 |
return true; // if we weren't open, we weren't joined |
| 302 |
} |
| 303 |
} // end ProtoSocket::LeaveGroup() |
| 304 |
|
| 305 |
|
| 306 |
// (NOTE: These functions are being moved to ProtoRouteMgr |
| 307 |
// Helper functions for group joins & leaves |
| 308 |
/*bool ProtoSocket::GetInterfaceAddress(const char* interfaceName, |
| 309 |
ProtoAddress::Type addressType, |
| 310 |
ProtoAddress& theAddress) |
| 311 |
{ |
| 312 |
ProtoSimAgent* simAgent = static_cast<ProtoSimAgent*>(notifier); |
| 313 |
if (simAgent) |
| 314 |
{ |
| 315 |
return simAgent->GetLocalAddress(theAddress); |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
PLOG(PL_ERROR, "ProtoSocket::GetInterfaceAddress() Error: notifier not set\n"); |
| 320 |
return false; |
| 321 |
} |
| 322 |
} // end ProtoSocket::GetInterfaceAddress()*/ |
| 323 |
|
| 324 |
/** |
| 325 |
* Dummy get interface stuff functions |
| 326 |
* use protoRouteMgr call instead when using simulation |
| 327 |
*/ |
| 328 |
bool ProtoSocket::GetInterfaceAddressList(const char* interfaceName, |
| 329 |
ProtoAddress::Type addressType, |
| 330 |
ProtoAddressList& addrList, |
| 331 |
unsigned int* ifIndex) |
| 332 |
{ |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
unsigned int ProtoSocket::GetInterfaceIndex(const char* interfaceName) |
| 337 |
{ |
| 338 |
return 0; |
| 339 |
} |
| 340 |
bool ProtoSocket::FindLocalAddress(ProtoAddress::Type addrType, ProtoAddress& theAddress) |
| 341 |
{ |
| 342 |
return false; |
| 343 |
} |
| 344 |
|
| 345 |
bool ProtoSocket::GetInterfaceName(unsigned int index, char* buffer, unsigned int buflen) |
| 346 |
{ |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
bool ProtoSocket::GetInterfaceName(const ProtoAddress& ifAddr, char* buffer, unsigned int buflen) |
| 351 |
{ |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
bool ProtoSocket::SetTTL(unsigned char ttl) |
| 357 |
{ |
| 358 |
static_cast<ProtoSimAgent::SocketProxy*>(handle)->SetTTL(ttl); |
| 359 |
return true; |
| 360 |
} // end ProtoSocket::SetTTL() |
| 361 |
|
| 362 |
bool ProtoSocket::SetTOS(unsigned char tos) |
| 363 |
{ |
| 364 |
return false; |
| 365 |
} // end ProtoSocket::SetTOS() |
| 366 |
|
| 367 |
bool ProtoSocket::SetBroadcast(bool broadcast) |
| 368 |
{ |
| 369 |
return true; |
| 370 |
} // end ProtoSocket::SetBroadcast() |
| 371 |
|
| 372 |
|
| 373 |
bool ProtoSocket::SetLoopback(bool loopback) |
| 374 |
{ |
| 375 |
static_cast<ProtoSimAgent::SocketProxy*>(handle)->SetLoopback(loopback); |
| 376 |
return true; |
| 377 |
} // end ProtoSocket::SetLoopback() |
| 378 |
|
| 379 |
bool ProtoSocket::SetMulticastInterface(const char* /*interfaceName*/) |
| 380 |
{ |
| 381 |
return true; |
| 382 |
} // end ProtoSocket::SetMulticastInterface() |
| 383 |
|
| 384 |
bool ProtoSocket::SetReuse(bool state) |
| 385 |
{ |
| 386 |
return true; |
| 387 |
} // end ProtoSocketError::SetReuse() |
| 388 |
|
| 389 |
bool ProtoSocket::SetEcnCapable(bool state) |
| 390 |
{ |
| 391 |
static_cast<ProtoSimAgent::SocketProxy*>(handle)->SetEcnCapable(state); |
| 392 |
return true; |
| 393 |
} // end ProtoSocketError::SetReuse() |
| 394 |
|
| 395 |
bool ProtoSocket::GetEcnStatus() const |
| 396 |
{ |
| 397 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->GetEcnStatus(); |
| 398 |
} // end ProtoSocketError::GetEcnStatus() |
| 399 |
|
| 400 |
bool ProtoSocket::SetTxBufferSize(unsigned int bufferSize) |
| 401 |
{ |
| 402 |
static_cast<ProtoSimAgent::SocketProxy*>(handle)->SetTxBufferSize(bufferSize); // I.T. Added 26/3/07 |
| 403 |
return true; |
| 404 |
} // end ProtoSocket::SetTxBufferSize() |
| 405 |
|
| 406 |
unsigned int ProtoSocket::GetTxBufferSize() |
| 407 |
{ |
| 408 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->GetTxBufferSize(); // I.T. Added 26/3/07 |
| 409 |
} // end ProtoSocket::GetTxBufferSize() |
| 410 |
|
| 411 |
bool ProtoSocket::SetRxBufferSize(unsigned int bufferSize) |
| 412 |
{ |
| 413 |
static_cast<ProtoSimAgent::SocketProxy*>(handle)->SetRxBufferSize(bufferSize); // I.T. Added 26/3/07 |
| 414 |
return false; |
| 415 |
} // end ProtoSocket::SetRxBufferSize() |
| 416 |
|
| 417 |
unsigned int ProtoSocket::GetRxBufferSize() |
| 418 |
{ |
| 419 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->GetRxBufferSize(); // I.T. Added 26/3/07 |
| 420 |
} // end ProtoSocket::GetRxBufferSize() |
| 421 |
|
| 422 |
bool ProtoSocket::SetBlocking(bool /*blocking*/) |
| 423 |
{ |
| 424 |
return true; |
| 425 |
} |
| 426 |
|
| 427 |
bool ProtoSocket::SetFragmentation(bool /*status*/) |
| 428 |
{ |
| 429 |
return true; |
| 430 |
} |
| 431 |
|
| 432 |
ProtoAddress::Type ProtoSocket::GetAddressType() |
| 433 |
{ |
| 434 |
return ProtoAddress::SIM; |
| 435 |
} // end ProtoSocket::GetAddressType() |
| 436 |
|
| 437 |
|
| 438 |
bool ProtoSocket::UpdateNotification() |
| 439 |
{ |
| 440 |
if (handle==NULL) return notify_output; |
| 441 |
else |
| 442 |
return static_cast<ProtoSimAgent::SocketProxy*>(handle)->SetOutputNotification(notify_output); // I.T. Added 26/3/07 |
| 443 |
} // end ProtoSocket::UpdateNotification() |
| 444 |
|
| 445 |
void ProtoSocket::OnNotify(ProtoSocket::Flag theFlag) |
| 446 |
{ |
| 447 |
#ifndef OPNET // JPH 5/18/2007 |
| 448 |
|
| 449 |
#endif // OPNET |
| 450 |
PLOG(PL_MAX, "ProtoSimSocket::OnNotify() called with flag %i and state = %i\n", theFlag, state); |
| 451 |
|
| 452 |
Event event = INVALID_EVENT; |
| 453 |
if (NOTIFY_INPUT == theFlag) |
| 454 |
{ |
| 455 |
switch (state) |
| 456 |
{ |
| 457 |
case CLOSED: |
| 458 |
PLOG(PL_MAX, "ProtoSimSocket::OnNotify() State - Socket is closed\n"); |
| 459 |
break; |
| 460 |
case IDLE: |
| 461 |
PLOG(PL_MAX, "ProtoSimSocket::OnNotify() State - Socket is idle, ready to receive\n"); |
| 462 |
event = RECV; |
| 463 |
break; |
| 464 |
case CONNECTING: |
| 465 |
PLOG(PL_MAX, "ProtoSimSocket::OnNotify() State - Socket is connecting\n"); |
| 466 |
break; |
| 467 |
case LISTENING: |
| 468 |
PLOG(PL_MAX, "ProtoSimSocket::OnNotify() State - Socket is waiting for accept\n"); |
| 469 |
// (TBD) check for error |
| 470 |
event = ACCEPT; |
| 471 |
break; |
| 472 |
case CONNECTED: |
| 473 |
PLOG(PL_MAX, "ProtoSimSocket::OnNotify() State - Socket is connected, ready to receive\n"); |
| 474 |
event = RECV; |
| 475 |
break; |
| 476 |
} |
| 477 |
} |
| 478 |
else if (NOTIFY_OUTPUT == theFlag) |
| 479 |
{ |
| 480 |
ASSERT(NOTIFY_OUTPUT == theFlag); |
| 481 |
switch (state) |
| 482 |
{ |
| 483 |
case CLOSED: |
| 484 |
break; |
| 485 |
case IDLE: |
| 486 |
event = SEND; // I.T. Added 26/3/07 - only notify SEND if requested |
| 487 |
break; |
| 488 |
case CONNECTING: |
| 489 |
event= CONNECT; |
| 490 |
state = CONNECTED; // I.T. Added - need to let client know we are connected |
| 491 |
break; |
| 492 |
case LISTENING: |
| 493 |
break; |
| 494 |
case CONNECTED: |
| 495 |
event = SEND; // I.T. Added 26/3/07 - only notify SEND if requested |
| 496 |
break; |
| 497 |
} |
| 498 |
} |
| 499 |
else // NOTIFY_NONE (connection was ended) |
| 500 |
{ |
| 501 |
switch(state) |
| 502 |
{ |
| 503 |
case CONNECTING: |
| 504 |
case CONNECTED: |
| 505 |
event = DISCONNECT; |
| 506 |
state = IDLE; |
| 507 |
break; |
| 508 |
default: |
| 509 |
break; |
| 510 |
} |
| 511 |
} |
| 512 |
if (NULL != listener) |
| 513 |
{ |
| 514 |
ASSERT(INVALID_EVENT != event); |
| 515 |
listener->on_event(*this, event); |
| 516 |
} |
| 517 |
|
| 518 |
} // end ProtoSocket::OnNotify() |
| 519 |
|
| 520 |
|
| 521 |
ProtoSocket::List::List() |
| 522 |
: head(NULL) |
| 523 |
{ |
| 524 |
} |
| 525 |
|
| 526 |
ProtoSocket::List::~List() |
| 527 |
{ |
| 528 |
Destroy(); |
| 529 |
} |
| 530 |
|
| 531 |
void ProtoSocket::List::Destroy() |
| 532 |
{ |
| 533 |
Item* next = head; |
| 534 |
while (next) |
| 535 |
{ |
| 536 |
Item* current = next; |
| 537 |
next = next->GetNext(); |
| 538 |
delete current->GetSocket(); |
| 539 |
delete current; |
| 540 |
} |
| 541 |
} // end ProtoSocket::List::Destroy() |
| 542 |
|
| 543 |
bool ProtoSocket::List::AddSocket(ProtoSocket& theSocket) |
| 544 |
{ |
| 545 |
Item* item = new Item(&theSocket); |
| 546 |
if (item) |
| 547 |
{ |
| 548 |
item->SetNext(head); |
| 549 |
head = item; |
| 550 |
return true; |
| 551 |
} |
| 552 |
else |
| 553 |
{ |
| 554 |
PLOG(PL_ERROR, "ProtoSocket::List::AddSocket() new Item error: %s\n", strerror(errno)); |
| 555 |
return false; |
| 556 |
} |
| 557 |
} // end ProtoSocket::List::AddSocket() |
| 558 |
|
| 559 |
void ProtoSocket::List::RemoveSocket(ProtoSocket& theSocket) |
| 560 |
{ |
| 561 |
Item* item = head; |
| 562 |
while (item) |
| 563 |
{ |
| 564 |
if (&theSocket == item->GetSocket()) |
| 565 |
{ |
| 566 |
Item* prev = item->GetPrev(); |
| 567 |
Item* next = item->GetNext(); |
| 568 |
if (prev) |
| 569 |
prev->SetNext(next); |
| 570 |
else |
| 571 |
head = next; |
| 572 |
if (next) next->SetPrev(prev); |
| 573 |
delete item; |
| 574 |
break; |
| 575 |
} |
| 576 |
item = item->GetNext(); |
| 577 |
} |
| 578 |
} // end ProtoSocket::List::AddSocket() |
| 579 |
|
| 580 |
ProtoSocket::List::Item::Item(ProtoSocket* theSocket) |
| 581 |
: socket(theSocket), prev(NULL), next(NULL) |
| 582 |
{ |
| 583 |
} |
| 584 |
|
| 585 |
ProtoSocket::List::Iterator::Iterator(const ProtoSocket::List& theList) |
| 586 |
: next(theList.head) |
| 587 |
{ |
| 588 |
} |