| 1 |
/** |
| 2 |
* @file protoChannel.cpp |
| 3 |
* |
| 4 |
* @brief This class serves as a base class for Protokit classes which require asynchronous I/O |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "protoChannel.h" |
| 8 |
#include "protoDebug.h" |
| 9 |
|
| 10 |
#ifdef WIN32 |
| 11 |
const ProtoChannel::Handle ProtoChannel::INVALID_HANDLE = INVALID_HANDLE_VALUE; |
| 12 |
#else |
| 13 |
#include <unistd.h> |
| 14 |
#include <fcntl.h> |
| 15 |
const ProtoChannel::Handle ProtoChannel::INVALID_HANDLE = -1; |
| 16 |
#endif // if/else WIN32/UNIX |
| 17 |
|
| 18 |
ProtoChannel::ProtoChannel() |
| 19 |
#ifdef WIN32 |
| 20 |
: input_handle(INVALID_HANDLE), input_event_handle(INVALID_HANDLE), input_ready(false), |
| 21 |
output_handle(INVALID_HANDLE), output_event_handle(INVALID_HANDLE), output_ready(false), |
| 22 |
#else |
| 23 |
: descriptor(INVALID_HANDLE), blocking_status(true), |
| 24 |
#endif // if/else WIN32/UNIX |
| 25 |
listener(NULL), notifier(NULL) |
| 26 |
{ |
| 27 |
#ifdef WIN32 |
| 28 |
overlapped_read_buffer = NULL; |
| 29 |
overlapped_write_buffer = NULL; |
| 30 |
#endif |
| 31 |
} |
| 32 |
|
| 33 |
ProtoChannel::~ProtoChannel() |
| 34 |
{ |
| 35 |
if (notifier) SetNotifier(NULL); |
| 36 |
if (listener) |
| 37 |
{ |
| 38 |
delete listener; |
| 39 |
listener = NULL; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
bool ProtoChannel::SetNotifier(ProtoChannel::Notifier* theNotifier) |
| 44 |
{ |
| 45 |
if (notifier != theNotifier) |
| 46 |
{ |
| 47 |
if (IsOpen()) |
| 48 |
{ |
| 49 |
// 1) Detach old notifier, if any |
| 50 |
if (NULL != notifier) |
| 51 |
{ |
| 52 |
notifier->UpdateChannelNotification(*this, 0); |
| 53 |
if (NULL == theNotifier) |
| 54 |
{ |
| 55 |
// Reset channel to "blocking" |
| 56 |
if(!SetBlocking(true)) |
| 57 |
PLOG(PL_ERROR, "ProtoChannel::SetNotifier() SetBlocking(true) error\n", GetErrorString()); |
| 58 |
} |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
// Set channel to "non-blocking" |
| 63 |
if(!SetBlocking(false)) |
| 64 |
{ |
| 65 |
PLOG(PL_ERROR, "ProtoChannel::SetNotifier() SetBlocking(false) error\n", GetErrorString()); |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
// 2) Set and update new notifier (if applicable) |
| 70 |
notifier = theNotifier; |
| 71 |
if (!UpdateNotification()) |
| 72 |
{ |
| 73 |
notifier = NULL; |
| 74 |
return false; |
| 75 |
} |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
notifier = theNotifier; |
| 80 |
} |
| 81 |
} |
| 82 |
return true; |
| 83 |
} // end ProtoChannel::SetNotifier() |
| 84 |
|
| 85 |
void ProtoChannel::Close() |
| 86 |
{ |
| 87 |
{ |
| 88 |
if (IsOpen()) |
| 89 |
{ |
| 90 |
StopInputNotification(); |
| 91 |
StopOutputNotification(); |
| 92 |
} |
| 93 |
} |
| 94 |
#ifdef WIN32 |
| 95 |
// This cleans up things if ProtoChannel overlapped i/o was used |
| 96 |
if (NULL != overlapped_write_buffer) |
| 97 |
{ |
| 98 |
CloseHandle(input_event_handle); |
| 99 |
input_event_handle = NULL; |
| 100 |
CloseHandle(output_event_handle); |
| 101 |
output_event_handle = NULL; |
| 102 |
delete[] overlapped_read_buffer; |
| 103 |
overlapped_read_buffer = NULL; |
| 104 |
delete[] overlapped_write_buffer; |
| 105 |
overlapped_write_buffer = NULL; |
| 106 |
} |
| 107 |
#endif // WIN32 |
| 108 |
|
| 109 |
} // end ProtoChannel::Close() |
| 110 |
|
| 111 |
|
| 112 |
bool ProtoChannel::StartInputNotification() |
| 113 |
{ |
| 114 |
if (!InputNotification()) |
| 115 |
{ |
| 116 |
#ifdef WIN32 |
| 117 |
// See if we're using overlapped i/o and kickstart if applicable |
| 118 |
if ((NULL != overlapped_read_buffer) && (NULL != notifier)) |
| 119 |
{ |
| 120 |
SetNotifyFlag(NOTIFY_INPUT); |
| 121 |
if (!StartOverlappedRead()) // note it calls UpdateNotification() for us |
| 122 |
{ |
| 123 |
UnsetNotifyFlag(NOTIFY_INPUT); |
| 124 |
PLOG(PL_ERROR, "ProtoChannel::StartInputNotification() error: overlapped read startup failure!\n"); |
| 125 |
return false; |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
#endif // WIN32 |
| 130 |
SetNotifyFlag(NOTIFY_INPUT); |
| 131 |
if (!UpdateNotification()) |
| 132 |
{ |
| 133 |
PLOG(PL_ERROR, "ProtoChannel::StartInputNotification() error: notification update failure!\n"); |
| 134 |
UnsetNotifyFlag(NOTIFY_INPUT); |
| 135 |
return false; |
| 136 |
} |
| 137 |
} |
| 138 |
return true; |
| 139 |
} // end ProtoChannel::StartInputNotification() |
| 140 |
|
| 141 |
void ProtoChannel::StopInputNotification() |
| 142 |
{ |
| 143 |
if (InputNotification()) |
| 144 |
{ |
| 145 |
UnsetNotifyFlag(NOTIFY_INPUT); |
| 146 |
UpdateNotification(); |
| 147 |
} |
| 148 |
} // end ProtoChannel::StopInputNotification() |
| 149 |
|
| 150 |
bool ProtoChannel::StartOutputNotification() |
| 151 |
{ |
| 152 |
if (!OutputNotification()) |
| 153 |
{ |
| 154 |
#ifdef WIN32 |
| 155 |
output_ready = true; |
| 156 |
#endif // WIN32 |
| 157 |
SetNotifyFlag(NOTIFY_OUTPUT); |
| 158 |
if (!UpdateNotification()) |
| 159 |
{ |
| 160 |
UnsetNotifyFlag(NOTIFY_OUTPUT); |
| 161 |
PLOG(PL_ERROR, "ProtoChannel::StartOutputNotification() error: notification update failure!\n"); |
| 162 |
return false; |
| 163 |
} |
| 164 |
} |
| 165 |
return true; |
| 166 |
} // end ProtoChannel::StartInputNotification() |
| 167 |
|
| 168 |
void ProtoChannel::StopOutputNotification() |
| 169 |
{ |
| 170 |
if (OutputNotification()) |
| 171 |
{ |
| 172 |
UnsetNotifyFlag(NOTIFY_OUTPUT); |
| 173 |
UpdateNotification(); |
| 174 |
} |
| 175 |
} // end ProtoChannel::StopOutputNotification() |
| 176 |
|
| 177 |
bool ProtoChannel::UpdateNotification() |
| 178 |
{ |
| 179 |
if (NULL != notifier) |
| 180 |
{ |
| 181 |
if (IsOpen()) |
| 182 |
{ |
| 183 |
if (!SetBlocking(false)) |
| 184 |
{ |
| 185 |
PLOG(PL_ERROR, "ProtoChannel::UpdateNotification() SetBlocking() error\n"); |
| 186 |
return false; |
| 187 |
} |
| 188 |
return notifier->UpdateChannelNotification(*this, GetNotifyFlags()); |
| 189 |
} |
| 190 |
return true; |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
return SetBlocking(true); |
| 195 |
} |
| 196 |
} // end ProtoChannel::UpdateNotification() |
| 197 |
|
| 198 |
bool ProtoChannel::SetBlocking(bool blocking) |
| 199 |
{ |
| 200 |
#ifdef UNIX |
| 201 |
if (IsOpen() && (blocking_status != blocking)) |
| 202 |
{ |
| 203 |
if (blocking) |
| 204 |
{ |
| 205 |
if(-1 == fcntl(descriptor, F_SETFL, fcntl(descriptor, F_GETFL, 0) & ~O_NONBLOCK)) |
| 206 |
{ |
| 207 |
PLOG(PL_ERROR, "ProtoChannel::SetBlocking() fcntl(F_SETFL(~O_NONBLOCK)) error: %s\n", GetErrorString()); |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
if(-1 == fcntl(descriptor, F_SETFL, fcntl(descriptor, F_GETFL, 0) | O_NONBLOCK)) |
| 214 |
{ |
| 215 |
PLOG(PL_ERROR, "ProtoChannel::SetBlocking() fcntl(F_SETFL(O_NONBLOCK)) error: %s\n", GetErrorString()); |
| 216 |
return false; |
| 217 |
} |
| 218 |
} |
| 219 |
blocking_status = blocking; |
| 220 |
} |
| 221 |
#endif // UNIX |
| 222 |
return true; //Note: taken care automatically under Win32 by WSAAsyncSelect(), overlapped i/o, etc??? |
| 223 |
} // end ProtoChannel::SetBlocking() |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#ifdef WIN32 |
| 228 |
|
| 229 |
bool ProtoChannel::InitOverlappedIO() |
| 230 |
{ |
| 231 |
// Set up event handles for overlapped i/o notifications |
| 232 |
if (NULL == (input_event_handle = CreateEvent(NULL, TRUE, FALSE, NULL))) |
| 233 |
{ |
| 234 |
input_event_handle = INVALID_HANDLE_VALUE; |
| 235 |
PLOG(PL_ERROR, "ProtoChannel::InitOverlappedIO() CreateEvent(input_event_handle) error: %s\n", ::GetErrorString()); |
| 236 |
return false; |
| 237 |
} |
| 238 |
if (NULL == (output_event_handle = CreateEvent(NULL,TRUE,FALSE,NULL))) |
| 239 |
{ |
| 240 |
output_event_handle = INVALID_HANDLE_VALUE; |
| 241 |
PLOG(PL_ERROR, "ProtoChannel::InitOverlappedIO() CreateEvent(input_event_handle) error: %s\n", ::GetErrorString()); |
| 242 |
CloseHandle(input_event_handle); |
| 243 |
input_event_handle = NULL; |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
// Initialize overlapped i/o structs |
| 248 |
memset(&overlapped_read, 0, sizeof(overlapped_read)); |
| 249 |
overlapped_read.hEvent = input_event_handle; |
| 250 |
memset(&overlapped_write, 0, sizeof(overlapped_write)); |
| 251 |
overlapped_write.hEvent = output_event_handle; |
| 252 |
// Allocate buffers for overlapped read/write |
| 253 |
if (NULL == overlapped_read_buffer) |
| 254 |
{ |
| 255 |
if (NULL == (overlapped_read_buffer = new char[OVERLAPPED_BUFFER_SIZE])) |
| 256 |
{ |
| 257 |
PLOG(PL_ERROR, "ProtoChannel::InitOverlappedIO() new overlapped_read_buffer error: %s\n", ::GetErrorString()); |
| 258 |
CloseHandle(input_event_handle); |
| 259 |
input_event_handle = NULL; |
| 260 |
CloseHandle(output_event_handle); |
| 261 |
output_event_handle = NULL; |
| 262 |
return false; |
| 263 |
} |
| 264 |
} |
| 265 |
if (NULL == overlapped_write_buffer) |
| 266 |
{ |
| 267 |
if (NULL == (overlapped_write_buffer = new char[OVERLAPPED_BUFFER_SIZE])) |
| 268 |
{ |
| 269 |
PLOG(PL_ERROR, "ProtoChannel::InitOverlappedIO() new overlapped_write_buffer error: %s\n", ::GetErrorString()); |
| 270 |
CloseHandle(input_event_handle); |
| 271 |
input_event_handle = NULL; |
| 272 |
CloseHandle(output_event_handle); |
| 273 |
output_event_handle = NULL; |
| 274 |
delete[] overlapped_read_buffer; |
| 275 |
overlapped_read_buffer = NULL; |
| 276 |
return false; |
| 277 |
} |
| 278 |
} |
| 279 |
return true; |
| 280 |
} // end ProtoChannel::InitOverlappedIO() |
| 281 |
|
| 282 |
bool ProtoChannel::StartOverlappedRead() |
| 283 |
{ |
| 284 |
if ((NULL == overlapped_read_buffer) && !InitOverlappedIO()) |
| 285 |
{ |
| 286 |
PLOG(PL_ERROR, "ProtoChannel::StartOverlappedRead() error: buffer allocation failed!\n"); |
| 287 |
return false; |
| 288 |
} |
| 289 |
DWORD bytesRead; |
| 290 |
if (0 != ReadFile(input_handle, overlapped_read_buffer, OVERLAPPED_BUFFER_SIZE, &bytesRead, &overlapped_read)) |
| 291 |
{ |
| 292 |
// We got some data so use "input_ready" to get automatic notification |
| 293 |
overlapped_read_count = bytesRead; |
| 294 |
overlapped_read_index = 0; |
| 295 |
input_ready = true; |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
switch(GetLastError()) |
| 300 |
{ |
| 301 |
case ERROR_IO_PENDING: |
| 302 |
overlapped_read_count = overlapped_read_index = 0; |
| 303 |
input_ready = false; |
| 304 |
break; |
| 305 |
case ERROR_BROKEN_PIPE: |
| 306 |
PLOG(PL_ERROR, "ProtoPipe::Listen() ReadFile() error: %s\n", ::GetErrorString()); |
| 307 |
Close(); |
| 308 |
return false; |
| 309 |
} |
| 310 |
} |
| 311 |
return UpdateNotification(); |
| 312 |
} // end ProtoChannel::StartOverlappedRead() |
| 313 |
|
| 314 |
bool ProtoChannel::OverlappedRead(char* buffer, unsigned int& numBytes) |
| 315 |
{ |
| 316 |
// Note when "input_ready" changes state we need to do an UpdateNotification() |
| 317 |
bool wasInputReady = input_ready; |
| 318 |
LPOVERLAPPED overlapPtr = NULL; |
| 319 |
unsigned int want = numBytes; |
| 320 |
unsigned int got = 0; |
| 321 |
if (NULL != notifier) |
| 322 |
{ |
| 323 |
// Only do overlapped I/O when a async i/o "notifier" is set |
| 324 |
overlapPtr = &overlapped_read; |
| 325 |
if (!input_ready) // an overlapped read operation is in place |
| 326 |
{ |
| 327 |
DWORD bytesRead; |
| 328 |
if (FALSE != GetOverlappedResult(input_handle, overlapPtr, &bytesRead, FALSE)) |
| 329 |
{ |
| 330 |
unsigned int len = (bytesRead < want) ? bytesRead : want; |
| 331 |
memcpy(buffer, overlapped_read_buffer, len); |
| 332 |
got += len; |
| 333 |
overlapped_read_index = (len < bytesRead) ? len : 0; |
| 334 |
input_ready = true; // assume handle is ready for reading until a pending overlapped i/o |
| 335 |
} |
| 336 |
else |
| 337 |
{ |
| 338 |
numBytes = 0; |
| 339 |
switch (GetLastError()) |
| 340 |
{ |
| 341 |
case ERROR_IO_INCOMPLETE: |
| 342 |
// no data available yet |
| 343 |
return true; |
| 344 |
case ERROR_BROKEN_PIPE: |
| 345 |
OnNotify(NOTIFY_NONE); |
| 346 |
return false; |
| 347 |
default: |
| 348 |
PLOG(PL_ERROR, "ProtoPipe::Recv() GetOverlappedResult() error(%d): %s\n", |
| 349 |
GetLastError(), ::GetErrorString()); |
| 350 |
return false; |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// First, copy any data remaining in "overlapped_read_buffer"? |
| 357 |
if ((overlapped_read_count > 0) && (got < want)) |
| 358 |
{ |
| 359 |
unsigned int len = want - got; |
| 360 |
if (len > overlapped_read_count) len = overlapped_read_count; |
| 361 |
memcpy(buffer+got, overlapped_read_buffer+overlapped_read_index, len); |
| 362 |
overlapped_read_count -= len; |
| 363 |
overlapped_read_index += len; |
| 364 |
got += len; |
| 365 |
} |
| 366 |
if (got < want) |
| 367 |
{ |
| 368 |
// We need to try for more... |
| 369 |
// Note if NULL != overlapPtr (from above), more overlapped read may be triggered |
| 370 |
DWORD bytesRead; |
| 371 |
unsigned int len = want - got; |
| 372 |
if (len > OVERLAPPED_BUFFER_SIZE) len = OVERLAPPED_BUFFER_SIZE; |
| 373 |
if (0 != ReadFile(input_handle, overlapped_read_buffer, len, &bytesRead, overlapPtr)) |
| 374 |
{ |
| 375 |
memcpy(buffer+got, overlapped_read_buffer, bytesRead); |
| 376 |
got += bytesRead; |
| 377 |
input_ready = true; // assume handle is ready for reading until a pending overlapped i/o |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
switch(GetLastError()) |
| 382 |
{ |
| 383 |
case ERROR_IO_PENDING: |
| 384 |
overlapped_read_count = overlapped_read_index = 0; |
| 385 |
input_ready = false; |
| 386 |
break; |
| 387 |
case ERROR_BROKEN_PIPE: |
| 388 |
if (0 == got) |
| 389 |
{ |
| 390 |
OnNotify(NOTIFY_NONE); |
| 391 |
return false; |
| 392 |
} |
| 393 |
break; |
| 394 |
default: |
| 395 |
PLOG(PL_ERROR, "ProtoPipe::Recv() ReadFile(%d) error: %s\n", GetLastError(), ::GetErrorString()); |
| 396 |
if (0 == got) return false; |
| 397 |
break; |
| 398 |
|
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
numBytes = got; |
| 403 |
if ((NULL != notifier) && (input_ready != wasInputReady)) |
| 404 |
UpdateNotification(); |
| 405 |
return true; |
| 406 |
|
| 407 |
} // end ProtoChannel::OverlappedRead() |
| 408 |
|
| 409 |
bool ProtoChannel::OverlappedWrite(const char* buffer, unsigned int& numBytes) |
| 410 |
{ |
| 411 |
// Note when "output_ready" changes state we need to do an UpdateNotification() |
| 412 |
bool wasOutputReady = output_ready; |
| 413 |
const char* bufferPtr = buffer; |
| 414 |
LPOVERLAPPED overlapPtr = NULL; |
| 415 |
|
| 416 |
if (NULL != notifier) |
| 417 |
{ |
| 418 |
// We only do actual overlapped i/o when an async notifier has been set |
| 419 |
overlapPtr = &overlapped_write; |
| 420 |
if (!output_ready) |
| 421 |
{ |
| 422 |
DWORD bytesWritten; |
| 423 |
if (FALSE == GetOverlappedResult(output_handle, overlapPtr, &bytesWritten, FALSE)) |
| 424 |
{ |
| 425 |
switch (GetLastError()) |
| 426 |
{ |
| 427 |
case ERROR_IO_INCOMPLETE: |
| 428 |
// Still not output ready |
| 429 |
numBytes = 0; |
| 430 |
return true; |
| 431 |
default: |
| 432 |
PLOG(PL_ERROR, "ProtoChannel::OverlappedWrite() GetOverlappedResult() error: %s\n", GetErrorString()); |
| 433 |
numBytes = 0; |
| 434 |
return false; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
// Copy data to overlapped i/o buffer, limiting "numBytes" as necessary |
| 439 |
if (numBytes > OVERLAPPED_BUFFER_SIZE) numBytes = OVERLAPPED_BUFFER_SIZE; |
| 440 |
|
| 441 |
|
| 442 |
memcpy(overlapped_write_buffer, buffer, numBytes); |
| 443 |
bufferPtr = overlapped_write_buffer; |
| 444 |
} |
| 445 |
DWORD bytesWritten; |
| 446 |
if (FALSE == WriteFile(output_handle, bufferPtr, numBytes, &bytesWritten, overlapPtr)) |
| 447 |
{ |
| 448 |
switch (GetLastError()) |
| 449 |
{ |
| 450 |
case ERROR_IO_PENDING: |
| 451 |
// assume requested numBytes (trimmed) will be written |
| 452 |
output_ready = false; |
| 453 |
break; |
| 454 |
case ERROR_BROKEN_PIPE: |
| 455 |
numBytes = 0; |
| 456 |
OnNotify(NOTIFY_NONE); |
| 457 |
// no break here on purpose |
| 458 |
default: |
| 459 |
numBytes = 0; |
| 460 |
PLOG(PL_ERROR,"Win32Vif::Write() WriteFile() error(%d): %s\n",GetLastError(),::GetErrorString()); |
| 461 |
return false;; |
| 462 |
} |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
numBytes = bytesWritten; |
| 467 |
output_ready = true; |
| 468 |
} |
| 469 |
|
| 470 |
if ((NULL != notifier) && (output_ready != wasOutputReady)) |
| 471 |
UpdateNotification(); |
| 472 |
|
| 473 |
return true; |
| 474 |
} // end ProtoChannel::OverlappedWrite() |
| 475 |
|
| 476 |
#endif // WIN32 |
| 477 |
|
| 478 |
|