Ejemplo 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;
}
Ejemplo n.º 2
0
// Connect this connection/socket
void clientRequestConnect(JsNetwork *net, JsVar *httpClientReqVar) {
    SocketType socketType = socketGetType(httpClientReqVar);
    clientRequestWrite(httpClientReqVar, 0); // force sendData to be made

    JsVar *options = jsvObjectGetChild(httpClientReqVar, HTTP_NAME_OPTIONS_VAR, false);
    unsigned short port = (unsigned short)jsvGetIntegerAndUnLock(jsvObjectGetChild(options, "port", 0));
    if (port==0) port=80;

    char hostName[128];
    JsVar *hostNameVar = jsvObjectGetChild(options, "host", 0);
    if (jsvIsUndefined(hostNameVar))
        strncpy(hostName, "localhost", sizeof(hostName));
    else
        jsvGetString(hostNameVar, hostName, sizeof(hostName));
    jsvUnLock(hostNameVar);

    uint32_t host_addr = 0;
    networkGetHostByName(net, hostName, &host_addr);

    if(!host_addr) {
        jsError("Unable to locate host");
        jsvUnLock(jsvObjectSetChild(httpClientReqVar, HTTP_NAME_CLOSENOW, jsvNewFromBool(true)));
        jsvUnLock(options);
        net->checkError(net);
        return;
    }

    int sckt =  net->createsocket(net, host_addr, port);
    if (sckt<0) {
        jsError("Unable to create socket\n");
        jsvUnLock(jsvObjectSetChild(httpClientReqVar, HTTP_NAME_CLOSENOW, jsvNewFromBool(true)));
    } else {
        jsvUnLock(jsvObjectSetChild(httpClientReqVar, HTTP_NAME_SOCKET, jsvNewFromInteger(sckt+1)));

        // For HTTP we get the connection callback when we've got a header back
        // Otherwise we just call back on success
        if (socketType != ST_HTTP) {
            jsiQueueObjectCallbacks(httpClientReqVar, HTTP_NAME_ON_CONNECT, &httpClientReqVar, 1);
        }
    }

    jsvUnLock(options);

    net->checkError(net);
}
Ejemplo n.º 3
0
// 'end' this connection
void clientRequestEnd(JsNetwork *net, JsVar *httpClientReqVar) {
    SocketType socketType = socketGetType(httpClientReqVar);
    if ((socketType&ST_TYPE_MASK) == ST_HTTP) {
        JsVar *finalData = 0;
        if (jsvGetBoolAndUnLock(jsvObjectGetChild(httpClientReqVar, HTTP_NAME_CHUNKED, 0))) {
            // If we were asked to send 'chunked' data, we need to finish up
            finalData = jsvNewFromString("");
        }
        // on HTTP, this actually means we connect
        // force sendData to be made
        clientRequestWrite(net, httpClientReqVar, finalData);
        jsvUnLock(finalData);
    } else {
        // on normal sockets, we actually request close after all data sent
        jsvObjectSetChildAndUnLock(httpClientReqVar, HTTP_NAME_CLOSE, jsvNewFromBool(true));
        // if we never sent any data, make sure we close 'now'
        JsVar *sendData = jsvObjectGetChild(httpClientReqVar, HTTP_NAME_SEND_DATA, 0);
        if (!sendData || jsvIsEmptyString(sendData))
            jsvObjectSetChildAndUnLock(httpClientReqVar, HTTP_NAME_CLOSENOW, jsvNewFromBool(true));
        jsvUnLock(sendData);
    }
}