Jabberd14-1.6.0/jabberd/mio.c/mio_new

Last-modified: 2007-03-18 (日) 23:29:41

このページを編集する際は,編集に関する方針に従ってください.

概要

* creates a new mio object from a file descriptor

引数

* @param fd the file descriptor the caller already has
* @param cb the callback function, MIO should call on events
* @param arg the argument MIO should pass to the callback function, when calling it
* @param mh which ::mio_handlers MIO should use for this connection
* @param pointer to the new MIO object, NULL on failure
  • これtypo。正しくは" * @return pointer to the new MIO object, NULL on failure"だよね。

実装

  • グローバル変数mio__dataを元に新しいmioを作成する。
mio mio_new(int fd, void *cb, void *arg, mio_handlers mh) {
    mio   new    =  NULL;
    pool  p      =  NULL;
    int   flags  =  0;
    if (fd <= 0)
	 return NULL;
    /* create the new MIO object */
    p           = pool_new();
    new         = pmalloco(p, sizeof(_mio));
    new->p      = p;
    new->type   = type_NORMAL;
    new->state  = state_ACTIVE;
    new->fd     = fd;
    new->cb     = (void*)cb;
    new->cb_arg = arg;
    mio_set_handlers(new, mh);
    /* set the default karma values */
    mio_karma2(new, mio__data->k);
    mio_rate(new, mio__data->rate_t, mio__data->rate_p);
    /* set the socket to non-blocking */
    flags =  fcntl(fd, F_GETFL, 0);
    flags |= O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);
    /* add to the select loop */
    _mio_link(new);
    /* notify the select loop */
    if (mio__data != NULL) {
	 log_debug2(ZONE, LOGT_EXECFLOW, "sending zzz notify to the select loop in mio_new()");
	 /* if there has been already sent a signal, that is not yet processed, we don't
	  * have to send this signal twice. Else we could get blocking here at the write() call
	  * if we send really many signals, what seems to be possible for large rosters as
	  * reported by Marco Balmer. I have not really tried to reproduce this, but it seems
	  * logical and I don't see where it can hurt to send only one signal.
	  * I have also considered using pth_write() here, but as I remember, there was a reason
	  * why a real write is used here. It would be really nice, if pth would be documented
	  * better ... and it would be even nicer not to use pth at all ...
	  */
	 if (mio__data->zzz_active <= 0) {
	     mio__data->zzz_active++;
	     write(mio__data->zzz[1]," ",1);
	     log_debug2(ZONE, LOGT_EXECFLOW, "notify sent");
	 }
    }
    return new;
}

呼出元

#related: relatedプラグインは廃止されました。