ext/iodine/fio.c
/* managing the protocol pointer array and the `on_close` callback */
managing the protocol pointer array and the `on_close` callback
managing the protocol pointer array and the ` on_close ` callback
static int fio_attach__internal(void *uuid_, void *protocol_) {
  intptr_t uuid = (intptr_t)uuid_;
  fio_protocol_s *protocol = (fio_protocol_s *)protocol_;
  if (protocol) {
    if (!protocol->on_close) {
      protocol->on_close = mock_on_ev;
    }
    if (!protocol->on_data) {
      protocol->on_data = mock_on_data;
    }
    if (!protocol->on_ready) {
      protocol->on_ready = mock_on_ev;
    }
    if (!protocol->ping) {
      protocol->ping = mock_ping;
    }
    if (!protocol->on_shutdown) {
      protocol->on_shutdown = mock_on_shutdown;
    }
    prt_meta(protocol) = (protocol_metadata_s){.rsv = 0};
  }
  if (!uuid_is_valid(uuid))
    goto invalid_uuid_unlocked;
  fio_lock(&uuid_data(uuid).protocol_lock);
  if (!uuid_is_valid(uuid)) {
    goto invalid_uuid;
  }
  fio_protocol_s *old_pr = uuid_data(uuid).protocol;
  uuid_data(uuid).open = 1;
  uuid_data(uuid).protocol = protocol;
  touchfd(fio_uuid2fd(uuid));
  fio_unlock(&uuid_data(uuid).protocol_lock);
  if (old_pr) {
    fio_defer_push_task(deferred_on_close, (void *)uuid, old_pr);
    if (!protocol) {
      fio_poll_remove_fd(fio_uuid2fd(uuid));
      fio_poll_add_write(fio_uuid2fd(uuid));
    }
  } else if (protocol) {
    fio_poll_add(fio_uuid2fd(uuid));
  }
  return 0;
invalid_uuid:
  fio_unlock(&uuid_data(uuid).protocol_lock);
invalid_uuid_unlocked:
  if (protocol)
    fio_defer_push_task(deferred_on_close, (void *)uuid, protocol);
  if (uuid == -1)
    errno = EBADF;
  else
    errno = ENOTCONN;
  return -1;
}
static int fio_attach__internal ( void * uuid_ , void * protocol_ ) { intptr_t uuid = ( intptr_t ) uuid_ ; fio_protocol_s * protocol = ( fio_protocol_s * ) protocol_ ; if ( protocol ) { if ( ! protocol -> on_close ) { protocol -> on_close = mock_on_ev ; } if ( ! protocol -> on_data ) { protocol -> on_data = mock_on_data ; } if ( ! protocol -> on_ready ) { protocol -> on_ready = mock_on_ev ; } if ( ! protocol -> ping ) { protocol -> ping = mock_ping ; } if ( ! protocol -> on_shutdown ) { protocol -> on_shutdown = mock_on_shutdown ; } prt_meta ( protocol ) = ( protocol_metadata_s ) { . rsv = 0 } ; } if ( ! uuid_is_valid ( uuid ) ) goto invalid_uuid_unlocked ; fio_lock ( & uuid_data ( uuid ) . protocol_lock ) ; if ( ! uuid_is_valid ( uuid ) ) { goto invalid_uuid ; } fio_protocol_s * old_pr = uuid_data ( uuid ) . protocol ; uuid_data ( uuid ) . open = 1 ; uuid_data ( uuid ) . protocol = protocol ; touchfd ( fio_uuid2fd ( uuid ) ) ; fio_unlock ( & uuid_data ( uuid ) . protocol_lock ) ; if ( old_pr ) { fio_defer_push_task ( deferred_on_close , ( void * ) uuid , old_pr ) ; if ( ! protocol ) { fio_poll_remove_fd ( fio_uuid2fd ( uuid ) ) ; fio_poll_add_write ( fio_uuid2fd ( uuid ) ) ; } } else if ( protocol ) { fio_poll_add ( fio_uuid2fd ( uuid ) ) ; } return 0 ; invalid_uuid : fio_unlock ( & uuid_data ( uuid ) . protocol_lock ) ; invalid_uuid_unlocked : if ( protocol ) fio_defer_push_task ( deferred_on_close , ( void * ) uuid , protocol ) ; if ( uuid == -1 ) errno = EBADF ; else errno = ENOTCONN ; return -1 ; }
managing the protocol pointer array and the `on_close` callback
managing the protocol pointer array and the ` on_close ` callback
/* protocol replacement */ /* hijacking */ /* adding a new uuid to the reactor */ // FIO_LOG_DEBUG("fio_attach failed for invalid uuid %p", (void *)uuid);