Ejemplo n.º 1
0
int pc__handshake_resp(pc_client_t *client,
                                const char *data, size_t len) {
  json_error_t error;
  json_t *res = json_loadb(data, len, 0, &error);

  if(res == NULL) {
    fprintf(stderr, "Fail to parse handshake package. %s\n", error.text);
    return -1;
  }

  json_int_t code = json_integer_value(json_object_get(res, "code"));
  if(code != PC_HANDSHAKE_OK) {
    fprintf(stderr, "Handshake fail, code: %d.\n", (int)code);
    return -1;
  }

  json_t *sys = json_object_get(res, "sys");
  if(sys) {
    // setup heartbeat
    json_int_t hb = json_integer_value(json_object_get(sys, "heartbeat"));
    if(hb < 0) {
      // no need heartbeat
      client->heartbeat = -1;
      client->timeout = -1;
    } else {
      client->heartbeat = hb > 0 ? hb : PC_DEFAULT_HEARTBEAT;
      json_int_t to = json_integer_value(json_object_get(sys, "timeout"));
      client->timeout = to > client->heartbeat ? to : PC_DEFAULT_TIMEOUT;
      uv_timer_set_repeat(&client->heartbeat_timer, client->heartbeat);
      uv_timer_set_repeat(&client->timeout_timer, client->timeout);
    }

    // setup route dictionary
    json_t *dics = json_object_get(sys, "dictionary");
    if(dics) {
      //TODO: dictionary name?
      client->route_to_code = NULL;
      client->code_to_route = NULL;
    }
  }

  json_t *user = json_object_get(res, "user");
  int status = 0;
  if(client->handshake_cb) {
    status = client->handshake_cb(client, user);
  }

  if(!status) {
    pc__handshake_ack(client);
  }

  return 0;
}
Ejemplo n.º 2
0
Archivo: timer.c Proyecto: iyedb/pyuv
static int
Timer_repeat_set(Timer *self, PyObject *value, void *closure)
{
    double repeat;

    UNUSED_ARG(closure);

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, -1);

    if (!value) {
        PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
        return -1;
    }

    repeat = PyFloat_AsDouble(value);
    if (repeat == -1 && PyErr_Occurred()) {
        return -1;
    }

    if (repeat < 0.0) {
        PyErr_SetString(PyExc_ValueError, "a positive float or 0.0 is required");
        return -1;
    }

    uv_timer_set_repeat(&self->timer_h, (uint64_t)(repeat * 1000));

    return 0;
}
Ejemplo n.º 3
0
static int lua_time_interval(lua_State * L)
{
	int ret = 0,t=lua_type(L,2);
	luatimer_t * tm = (luatimer_t *)luaL_checkudata(L, 1,UVLUA_TIMER_CLS);
	if(NULL != tm){
		if(LUA_TNIL != t && LUA_TNONE !=t){
			int repeat = lua_tointeger(L, 2);
			if(repeat > 0){
				uv_loop_t * loop = tm->timer.loop;
				int restart = 0;
				if(uv_is_active((uv_handle_t *)&tm->timer)){
					uv_timer_stop(&tm->timer);
					restart=1;
				}
				uv_timer_set_repeat(&tm->timer, repeat);
				if(restart)
					uv_timer_again(&tm->timer);
			}
			
		}
		lua_pushinteger(L, uv_timer_get_repeat(&tm->timer));
	}else{
		lua_pushinteger(L, 0);
	}
	return 1;
}
Ejemplo n.º 4
0
/*
 * Class:     com_oracle_libuv_handles_TimerHandle
 * Method:    _set_repeat
 * Signature: (JJ)V
 */
JNIEXPORT void JNICALL Java_com_oracle_libuv_handles_TimerHandle__1set_1repeat
  (JNIEnv *env, jobject that, jlong timer, jlong repeat) {

  assert(timer);
  uv_timer_t* handle = reinterpret_cast<uv_timer_t*>(timer);
  uv_timer_set_repeat(handle, static_cast<uint64_t>(repeat));
}
Ejemplo n.º 5
0
static int pc__handshake_sys(pc_client_t *client, json_t *sys) {
  json_int_t hb = json_integer_value(json_object_get(sys, "heartbeat"));
  if(hb < 0) {
    // no need heartbeat
    client->heartbeat = -1;
    client->timeout = -1;
  } else {
    client->heartbeat = hb > 0 ? hb : PC_DEFAULT_HEARTBEAT;
    json_int_t to = json_integer_value(json_object_get(sys, "timeout"));
    client->timeout = to > client->heartbeat ? to : PC_DEFAULT_TIMEOUT;
  }

  uv_timer_set_repeat(&client->heartbeat_timer, client->heartbeat);
  uv_timer_set_repeat(&client->timeout_timer, client->timeout);

  return 0;
}
Ejemplo n.º 6
0
int luv_timer_set_repeat(lua_State* L) {
    uv_timer_t* handle = (uv_timer_t*)luv_checkudata(L, 1, "timer");
    int64_t repeat = luaL_checklong(L, 2);

    uv_timer_set_repeat(handle, repeat);

    return 0;
}
Ejemplo n.º 7
0
int lua_uv_timer_repeat(lua_State * L) {
	uv_timer_t * t = lua_generic_object<uv_timer_t>(L, 1);
	if (lua_isnumber(L, 2)) {
		uv_timer_set_repeat(t, lua_tonumber(L, 2));
		lua_settop(L, 1);
		return 1;	// return timer
	} else {
		lua_pushnumber(L, uv_timer_get_repeat(t));
		return 1;
	}
}
Ejemplo n.º 8
0
static void repeat_2_cb(uv_timer_t* handle) {
  TUV_ASSERT(handle == &repeat_2);
  TUV_ASSERT(repeat_2_cb_allowed);

  repeat_2_cb_called++;

  if (uv_timer_get_repeat(&repeat_2) == 0) {
    TUV_ASSERT(0 == uv_is_active((uv_handle_t*) handle));
    uv_close((uv_handle_t*)handle, close_cb);
    return;
  }

  TUV_ASSERT(uv_timer_get_repeat(&repeat_2) == 100);

  /* This shouldn't take effect immediately. */
  uv_timer_set_repeat(&repeat_2, 0);
}
Ejemplo n.º 9
0
static void repeat_2_cb(uv_timer_t* handle, int status) {
  ASSERT(handle == &repeat_2);
  ASSERT(status == 0);
  ASSERT(repeat_2_cb_allowed);

  LOGF("repeat_2_cb called after %ld ms\n", (long int)(uv_now() - start_time));

  repeat_2_cb_called++;

  if (uv_timer_get_repeat(&repeat_2) == 0) {
    ASSERT(!uv_is_active((uv_handle_t*)handle));
    uv_close((uv_handle_t*)handle, close_cb);
    return;
  }

  LOGF("uv_timer_get_repeat %ld ms\n",
      (long int)uv_timer_get_repeat(&repeat_2));
  ASSERT(uv_timer_get_repeat(&repeat_2) == 100);

  /* This shouldn't take effect immediately. */
  uv_timer_set_repeat(&repeat_2, 0);
}
Ejemplo n.º 10
0
void GarbageCollector::finalize()
{
	TraceL << "Finalize" << std::endl;	
	
	// Ensure the loop is not running and that the 
	// calling thread is the main thread.
	_handle.assertTID();
	//assert(_handle.loop()->active_handles <= 1); 
	assert(!_handle.closed());
	assert(!_finalize);
	_finalize = true;
	
	// Make sure uv_stop doesn't prevent cleanup.
	_handle.loop()->stop_flag = 0;

	// Run the loop until managed pointers have been deleted,
	// and the internal timer has also been deleted.
	uv_timer_set_repeat(_handle.ptr<uv_timer_t>(), 1);
	uv_ref(_handle.ptr());
	uv_run(_handle.loop(), UV_RUN_DEFAULT);

	TraceL << "Finalize: OK" << std::endl;
}
Ejemplo n.º 11
0
static void repeat_2_cb(uv_timer_t* handle) {
  ASSERT(handle == &repeat_2);
  ASSERT(repeat_2_cb_allowed);

  fprintf(stderr, "repeat_2_cb called after %ld ms\n",
          (long int)(uv_now(uv_default_loop()) - start_time));
  fflush(stderr);

  repeat_2_cb_called++;

  if (uv_timer_get_repeat(&repeat_2) == 0) {
    ASSERT(0 == uv_is_active((uv_handle_t*) handle));
    uv_close((uv_handle_t*)handle, close_cb);
    return;
  }

  fprintf(stderr, "uv_timer_get_repeat %ld ms\n",
          (long int)uv_timer_get_repeat(&repeat_2));
  fflush(stderr);
  ASSERT(uv_timer_get_repeat(&repeat_2) == 100);

  /* This shouldn't take effect immediately. */
  uv_timer_set_repeat(&repeat_2, 0);
}
Ejemplo n.º 12
0
void Timer::setInterval(std::int64_t interval)
{
    assert(_handle.get());
    _interval = interval;
    uv_timer_set_repeat(_handle.get(), interval);
}