| 403 | } |
| 404 | |
| 405 | int InputMessenger::AddHandler(const InputMessageHandler& handler) { |
| 406 | if (handler.parse == NULL || handler.process == NULL |
| 407 | || handler.name == NULL) { |
| 408 | CHECK(false) << "Invalid argument"; |
| 409 | return -1; |
| 410 | } |
| 411 | BAIDU_SCOPED_LOCK(_add_handler_mutex); |
| 412 | if (NULL == _handlers) { |
| 413 | _handlers = new (std::nothrow) InputMessageHandler[_capacity]; |
| 414 | if (NULL == _handlers) { |
| 415 | LOG(FATAL) << "Fail to new array of InputMessageHandler"; |
| 416 | return -1; |
| 417 | } |
| 418 | memset(_handlers, 0, sizeof(*_handlers) * _capacity); |
| 419 | _non_protocol = false; |
| 420 | } |
| 421 | if (_non_protocol) { |
| 422 | CHECK(false) << "AddNonProtocolHandler was invoked"; |
| 423 | return -1; |
| 424 | } |
| 425 | ProtocolType type = FindProtocolOfHandler(handler); |
| 426 | if (type == PROTOCOL_UNKNOWN) { |
| 427 | CHECK(false) << "Adding a handler which doesn't belong to any protocol"; |
| 428 | return -1; |
| 429 | } |
| 430 | const int index = type; |
| 431 | if (index >= (int)_capacity) { |
| 432 | LOG(FATAL) << "Can't add more handlers than " << _capacity; |
| 433 | return -1; |
| 434 | } |
| 435 | if (_handlers[index].parse == NULL) { |
| 436 | // The same protocol might be added more than twice |
| 437 | _handlers[index] = handler; |
| 438 | } else if (_handlers[index].parse != handler.parse |
| 439 | || _handlers[index].process != handler.process) { |
| 440 | CHECK(_handlers[index].parse == handler.parse); |
| 441 | CHECK(_handlers[index].process == handler.process); |
| 442 | return -1; |
| 443 | } |
| 444 | if (index > _max_index.load(butil::memory_order_relaxed)) { |
| 445 | _max_index.store(index, butil::memory_order_release); |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int InputMessenger::AddNonProtocolHandler(const InputMessageHandler& handler) { |
| 451 | if (handler.parse == NULL || handler.process == NULL |