Example #1
0
void register_server(char *test_listen)
{
    if (!test_mode) {
        ev_status = (zev_t *) zcalloc(1, sizeof(zev_t));
        zclose_on_exec(ZMASTER_MASTER_STATUS_FD, 1);
        znonblocking(ZMASTER_MASTER_STATUS_FD, 1);

        zev_init(ev_status, zvar_evbase, ZMASTER_MASTER_STATUS_FD);
        zev_read(ev_status, on_master_reload);

        zvar_master_server_listen_fd = ZMASTER_LISTEN_FD;
    } else {
        if (test_listen == 0) {
            test_listen = zconfig_get_str(zvar_config, "zlisten", 0);
        }
        if (ZEMPTY(test_listen)) {
            zfatal("no listen");
        }
        if (strchr(test_listen, ':')) {
            zvar_master_server_listen_type = ZMASTER_LISTEN_INET;
        } else {
            zvar_master_server_listen_type = ZMASTER_LISTEN_UNIX;
        }
        zvar_master_server_listen_fd = zlisten(test_listen, 10);

        if (zvar_master_server_listen_fd < 0) {
            zfatal("open %s(%m)", test_listen);
        }
    }
    zclose_on_exec(zvar_master_server_listen_fd, 1);

    znonblocking(zvar_master_server_listen_fd, 1);

    zevtimer_init(&reload_timer, zvar_evbase);

    if (!zmaster_server_service) {
        return;
    }

    ev_listen = (zev_t *) zcalloc(1, sizeof(zev_t));
    zev_init(ev_listen, zvar_evbase, zvar_master_server_listen_fd);

    if (zvar_master_server_listen_type == ZMASTER_LISTEN_INET) {
        zev_read(ev_listen, inet_server_accept);
    } else if (zvar_master_server_listen_type == ZMASTER_LISTEN_UNIX) {
        zev_read(ev_listen, unix_server_accept);
    }
}
Example #2
0
int zconnectx(zsock_t sock, const char *host, uint16_t port, int listenq, int timeout_ms){
  int ret;
  zsockaddr_in addr;

  ret = zinet_addr(&addr, host, port);
  if(ret != ZOK){
    return(ret);
  }

  if(listenq > 0){
    int reuse = 1;
    zsock_nonblock(sock, 0);
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    zbind(sock, (ZSA*)&addr, sizeof(addr));
    ret = zlisten(sock, listenq);
  }else{
    if(-1 == timeout_ms){
      // block connect
#if ZTRACE_SOCKET
      ZDBG("start block connect...");
#endif
      zsock_nonblock(sock, 0);
      ret = zconnect(sock, (ZSA*)&addr, sizeof(addr));
      zsock_nonblock(sock, 1);
#if ZTRACE_SOCKET
      ZDBG("block connect end.");
#endif
      return ret;
    }
    // nonblock connect
    if(ZOK != (ret = zconnect(sock, (ZSA*)&addr, sizeof(addr)))){
      struct timeval tv;
      fd_set rset, wset;
      int error;
      socklen_t len;
      if(timeout_ms < 1000 || timeout_ms > 15000){
	timeout_ms = 4000;
      }
      tv.tv_sec = timeout_ms/1000;
      tv.tv_usec = (timeout_ms%1000)*1000;
      FD_ZERO(&rset);
      FD_SET(sock, &rset);
      FD_ZERO(&wset);
      FD_SET(sock, &wset);
#if ZTRACE_SOCKET
      ZDBG("conect timeout<sec:%d, usec:%d>", tv.tv_sec, tv.tv_usec);
#endif
      if((ret = zselect(sock+1, &rset, &wset, NULL, &tv)) > 0){
	ZDBG("select<%d>", ret);
	len = sizeof(error);
	getsockopt(sock, SOL_SOCKET, SO_ERROR, &error,&len);
	if(0 == error){
	  ret = ZOK;
	  ZDBG("connect ok");
	}else{
	  ret = ZFAIL;
	  ZDBG("connect fail");
	}
      }else{
	ret = ZTIMEOUT;
      }
    }
  }
  ZERRC(ret);
  return(ret);
}