Exemple #1
0
// Socket close result handler.
static void AfterClose(uv_handle_t* handle) {
  HandleWrap* tcp_wrap = HandleWrap::FromHandle(handle);
  IOTJS_ASSERT(tcp_wrap != NULL);

  // tcp object.
  JObject jtcp = tcp_wrap->jobject();
  IOTJS_ASSERT(jtcp.IsObject());

  // callback function.
  JObject jcallback = jtcp.GetProperty("onclose");
  if (jcallback.IsFunction()) {
    MakeCallback(jcallback, JObject::Null(), JArgList::Empty());
  }
}
Exemple #2
0
void OnRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
  TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(handle->data);
  IOTJS_ASSERT(tcp_wrap != NULL);

  // tcp handle
  JObject jtcp = tcp_wrap->jobject();
  IOTJS_ASSERT(jtcp.IsObject());

  // socket object
  JObject jsocket = jtcp.GetProperty("owner");
  IOTJS_ASSERT(jsocket.IsObject());

  // onread callback
  JObject jonread = jtcp.GetProperty("onread");
  IOTJS_ASSERT(jonread.IsFunction());

  JArgList jargs(4);
  jargs.Add(jsocket);
  jargs.Add(JVal::Number((int)nread));
  jargs.Add(JVal::Bool(false));

  if (nread <= 0) {
    if (buf->base != NULL) {
      ReleaseBuffer(buf->base);
    }
    if (nread < 0) {
      if (nread == UV__EOF) {
        jargs.Set(2, JVal::Bool(true));
      }
      MakeCallback(jonread, JObject::Null(), jargs);
    }
    return;
  }

  JObject jbuffer(CreateBuffer(static_cast<size_t>(nread)));
  BufferWrap* buffer_wrap = BufferWrap::FromJBuffer(jbuffer);

  buffer_wrap->Copy(buf->base, nread);

  jargs.Add(jbuffer);
  MakeCallback(jonread, JObject::Null(), jargs);

  ReleaseBuffer(buf->base);
}
static void After(uv_fs_t* req) {
  FsReqWrap* req_wrap = static_cast<FsReqWrap*>(req->data);
  IOTJS_ASSERT(req_wrap != NULL);
  IOTJS_ASSERT(req_wrap->data() == req);

  JObject cb = req_wrap->jcallback();
  IOTJS_ASSERT(cb.IsFunction());

  JArgList jarg(2);
  if (req->result < 0) {
    JObject jerror(CreateUVException(req->result, "open"));
    jarg.Add(jerror);
  } else {
    jarg.Add(JObject::Null());
    switch (req->fs_type) {
      case UV_FS_CLOSE:
      {
        break;
      }
      case UV_FS_OPEN:
      case UV_FS_READ:
      case UV_FS_WRITE:
      {
        JObject arg1(static_cast<int>(req->result));
        jarg.Add(arg1);
        break;
      }
      case UV_FS_STAT: {
        uv_stat_t s = (req->statbuf);
        JObject ret(MakeStatObject(&s));
        jarg.Add(ret);
        break;
      }
      default:
        jarg.Add(JObject::Null());
    }
  }

  JObject res = MakeCallback(cb, JObject::Null(), jarg);

  uv_fs_req_cleanup(req);

  delete req_wrap;
}
Exemple #4
0
JObject MakeStatObject(uv_stat_t* statbuf) {
  Module* module = GetBuiltinModule(MODULE_FS);
  IOTJS_ASSERT(module != NULL);

  JObject* fs = module->module;
  IOTJS_ASSERT(fs != NULL);

  JObject createStat = fs->GetProperty("_createStat");
  IOTJS_ASSERT(createStat.IsFunction());

  JObject jstat;

#define X(statobj, name) \
  JObject name((int32_t)statbuf->st_##name); \
  statobj.SetProperty(#name, name); \

  X(jstat, dev)
  X(jstat, mode)
  X(jstat, nlink)
  X(jstat, uid)
  X(jstat, gid)
  X(jstat, rdev)

#undef X

#define X(statobj, name) \
  JObject name((double)statbuf->st_##name); \
  statobj.SetProperty(#name, name); \

  X(jstat, blksize)
  X(jstat, ino)
  X(jstat, size)
  X(jstat, blocks)

#undef X

  JArgList jargs(1);
  jargs.Add(jstat);

  JResult jstat_res(createStat.Call(JObject::Null(), jargs));
  IOTJS_ASSERT(jstat_res.IsOk());

  return jstat_res.value();
}
Exemple #5
0
// A client socket wants to connect to this server.
// Parameters:
//   * uv_stream_t* handle - server handle
//   * int status - status code
static void OnConnection(uv_stream_t* handle, int status) {
  // Server tcp wrapper.
  TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(handle->data);
  IOTJS_ASSERT(tcp_wrap->tcp_handle() == reinterpret_cast<uv_tcp_t*>(handle));

  // Tcp object
  JObject jtcp = tcp_wrap->jobject();
  IOTJS_ASSERT(jtcp.IsObject());

  // `onconnection` callback.
  JObject jonconnection = jtcp.GetProperty("onconnection");
  IOTJS_ASSERT(jonconnection.IsFunction());

  // The callback takes two parameter
  // [0] status
  // [1] client tcp object
  JArgList args(2);
  args.Add(JVal::Number(status));

  if (status == 0) {
    // Create client socket handle wrapper.
    JObject jcreate_tcp = jtcp.GetProperty("createTCP");
    IOTJS_ASSERT(jcreate_tcp.IsFunction());

    JObject jclient_tcp = jcreate_tcp.CallOk(JObject::Null(),
                                             JArgList::Empty());
    IOTJS_ASSERT(jclient_tcp.IsObject());

    TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(jclient_tcp.GetNative());

    uv_stream_t* client_handle =
        reinterpret_cast<uv_stream_t*>(tcp_wrap->tcp_handle());

    int err = uv_accept(handle, client_handle);
    if (err) {
      return;
    }

    args.Add(jclient_tcp);
  }

  MakeCallback(jonconnection, jtcp, args);
}
Exemple #6
0
// Connection request result handler.
static void AfterConnect(uv_connect_t* req, int status) {
  ConnectReqWrap* req_wrap = reinterpret_cast<ConnectReqWrap*>(req->data);
  TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(req->handle->data);
  IOTJS_ASSERT(req_wrap != NULL);
  IOTJS_ASSERT(tcp_wrap != NULL);

  // Take callback function object.
  //  function afterConnect(status)
  JObject jcallback = req_wrap->jcallback();
  IOTJS_ASSERT(jcallback.IsFunction());

  // Only parameter is status code.
  JArgList args(1);
  args.Add(JVal::Number(status));

  // Make callback.
  MakeCallback(jcallback, JObject::Null(), args);

  // Release request wrapper.
  delete req_wrap;
}