Esempio n. 1
0
/*JSON{
  "type" : "method",
  "class" : "Socket",
  "name" : "write",
  "generate" : "jswrap_net_socket_write",
  "params" : [
    ["data","JsVar","A string containing data to send"]
  ],
  "return" : ["bool","For note compatibility, the boolean false. When the send buffer is empty, a `drain` event will be sent"]
}*/
bool jswrap_net_socket_write(JsVar *parent, JsVar *data) {
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return false;
  clientRequestWrite(&net, parent, data);
  networkFree(&net);
  return false;
}
Esempio n. 2
0
/*JSON{
  "type"     : "idle",
  "class"    : "Telnet",
  "generate" : "jswrap_telnet_idle"
}
*/
bool jswrap_telnet_idle(void) {
  // get a handle to the network, no network -> can't do anything at all
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return false;

  // if we're supposed to be off, then make sure we're disconnected
  if (tnSrvMode == MODE_OFF) {
    if (tnSrv.sock > 0) {
      telnetStop(&net);
    }
    networkFree(&net);
    return false;
  }

  // we're supposed to be on, make sure we're listening
  if (tnSrv.sock == 0) {
    memset(&tnSrv, 0, sizeof(TelnetServer));
    telnetStart(&net);
    if (tnSrv.sock == 0) {
      networkFree(&net);
      return false; // seems like there's a problem...
    }
  }

  // looks like we are listening, now deal with actual connected sockets
  bool active = false;
  active |= telnetAccept(&net);
  active |= telnetRecv(&net);
  active |= telnetSendBuf(&net);
  //if (active) printf("tnSrv: idle=%d\n", active);

  networkFree(&net);
  return active;
}
Esempio n. 3
0
void jswrap_net_server_close(JsVar *parent) {
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return;

  serverClose(&net, parent);
  networkFree(&net);
}
Esempio n. 4
0
void jswrap_net_server_listen(JsVar *parent, int port) {
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return;

  serverListen(&net, parent, port);
  networkFree(&net);
}
Esempio n. 5
0
/*JSON{
  "type" : "method",
  "class" : "Socket",
  "name" : "end",
  "generate" : "jswrap_net_socket_end",
  "params" : [
    ["data","JsVar","A string containing data to send"]
  ]
}
Close this socket - optional data to append as an argument
*/
void jswrap_net_socket_end(JsVar *parent, JsVar *data) {
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return;

  if (!jsvIsUndefined(data)) jswrap_net_socket_write(parent, data);
  clientRequestEnd(&net, parent);
  networkFree(&net);
}
Esempio n. 6
0
/*JSON{
  "type" : "staticmethod",
  "class" : "net",
  "name" : "connect",
  "generate_full" : "jswrap_net_connect(options, callback, ST_NORMAL)",
  "params" : [
    ["options","JsVar","An object containing host,port fields"],
    ["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
  ],
  "return" : ["JsVar","Returns a new net.Socket object"],
  "return_object" : "Socket"
}
Create a socket connection
*/
JsVar *jswrap_net_connect(JsVar *options, JsVar *callback, SocketType socketType) {
  bool unlockOptions = false;
  if (jsvIsString(options)) {
    options = jswrap_url_parse(options, false);
    unlockOptions = true;
  }
  if (!jsvIsObject(options)) {
    jsError("Expecting Options to be an Object but it was %t", options);
    return 0;
  }
#ifdef USE_TLS
  if ((socketType&ST_TYPE_MASK) == ST_HTTP) {
    JsVar *protocol = jsvObjectGetChild(options, "protocol", 0);
    if (protocol && jsvIsStringEqual(protocol, "https:")) {
      socketType |= ST_TLS;
    }
    jsvUnLock(protocol);
  }
#endif

  // Make sure we have a function as callback, or nothing (which is OK too)
  JsVar *skippedCallback = jsvSkipName(callback);
  if (!jsvIsUndefined(skippedCallback)) {
    if (!jsvIsFunction(skippedCallback)) {
      jsError("Expecting Callback Function but got %t", skippedCallback);
      jsvUnLock(skippedCallback);
      return 0;
    }
    jsvUnLock(skippedCallback);
  } else {
    callback = NULL;
  }
  JsVar *rq = clientRequestNew(socketType, options, callback);
  if (unlockOptions) jsvUnLock(options);

  if ((socketType&ST_TYPE_MASK) != ST_HTTP) {
    JsNetwork net;
    if (networkGetFromVarIfOnline(&net)) {
      clientRequestConnect(&net, rq);
    }
    networkFree(&net);
  }

  return rq;
}
Esempio n. 7
0
void telnetSendChar(char ch) {
  if (tnSrv.sock == 0 || tnSrv.cliSock == 0) return;
  if (tnSrv.txBufLen >= TX_CHUNK) {
    // buffer overflow :-(
    if (!ovf) {
      printf("tnSrv: send overflow!\n");
      ovf = true;
    }
  } else {
    ovf = false;
    tnSrv.txBuf[tnSrv.txBufLen++] = ch;
  }

  // if the buffer has a bunch of chars then try to send, else it'll happen
  // at idle time.
  if (tnSrv.txBufLen < TX_CHUNK/4) return;
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return;
  telnetSendBuf(&net);
  networkFree(&net);
}
Esempio n. 8
0
/*JSON{
  "type" : "staticmethod",
  "class" : "http",
  "name" : "get",
  "generate" : "jswrap_http_get",
  "params" : [
    ["options","JsVar","An object containing host,port,path,method fields"],
    ["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
  ],
  "return" : ["JsVar","Returns a new httpCRq object"],
  "return_object" : "httpCRq"
}
Create an HTTP Request - convenience function for ```http.request()```. `options.method` is set to 'get', and end is called automatically. See [the Internet page](/Internet) for more usage examples.
*/
JsVar *jswrap_http_get(JsVar *options, JsVar *callback) {
  JsNetwork net;
  if (!networkGetFromVarIfOnline(&net)) return 0;

  if (jsvIsObject(options)) {
    // if options is a string - it will be parsed, and GET will be set automatically
    JsVar *method = jsvNewFromString("GET");
    jsvUnLock2(jsvAddNamedChild(options, method, "method"), method);
  }
  JsVar *skippedCallback = jsvSkipName(callback);
  if (!jsvIsUndefined(skippedCallback) && !jsvIsFunction(skippedCallback)) {
    jsError("Expecting Callback Function but got %t", skippedCallback);
    jsvUnLock(skippedCallback);
    return 0;
  }
  jsvUnLock(skippedCallback);
  JsVar *cliReq = jswrap_net_connect(options, callback, ST_HTTP);
  if (cliReq) clientRequestEnd(&net, cliReq);
  networkFree(&net);
  return cliReq;
}