Method: Iodine::Connection#handler=

Defined in:
ext/iodine/iodine_connection.c

#handler=(handler) ⇒ Object

Sets the client's callback object, so future events will use the new object's callbacks.



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'ext/iodine/iodine_connection.c', line 360

static VALUE iodine_connection_handler_set(VALUE self, VALUE handler) {
  // clang-format on
  iodine_connection_data_s *data = iodine_connection_validate_data(self);
  if (!data) {
    FIO_LOG_DEBUG("(iodine) attempted to set a connection handler for "
                  "an invalid connection: %p",
                  (void *)self);
    return Qnil;
  }
  if (handler == Qnil || handler == Qfalse) {
    FIO_LOG_DEBUG(
        "(iodine) called client.handler = nil, closing connection: %p",
        (void *)self);
    iodine_connection_close(self);
    return Qnil;
  }
  if (data->info.handler != handler) {
    uint8_t answers_on_open = (rb_respond_to(handler, on_open_id) != 0);
    if (data->answers_on_close)
      IodineCaller.call2(data->info.handler, on_close_id, 1, &self);
    fio_lock(&data->lock);
    data->info.handler = handler;
    data->answers_on_open = answers_on_open,
    data->answers_on_message = (rb_respond_to(handler, on_message_id) != 0),
    data->answers_ping = (rb_respond_to(handler, ping_id) != 0),
    data->answers_on_drained = (rb_respond_to(handler, on_drained_id) != 0),
    data->answers_on_shutdown = (rb_respond_to(handler, on_shutdown_id) != 0),
    data->answers_on_close = (rb_respond_to(handler, on_close_id) != 0),
    fio_unlock(&data->lock);
    if (answers_on_open) {
      iodine_connection_fire_event(self, IODINE_CONNECTION_ON_OPEN, Qnil);
    }
    FIO_LOG_DEBUG("(iodine) switched handlers for connection: %p",
                  (void *)self);
  }
  return handler;
}