/**
 * Handel incomming data from Client
 */
void WebSocketsServer::handleClientData(void) {

    WSclient_t * client;
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            int len = client->tcp->available();
            if(len > 0) {
                //DEBUG_WEBSOCKETS("[WS-Server][%d][handleClientData] len: %d\n", client->num, len);
                switch(client->status) {
                    case WSC_HEADER:
                    {
                        String headerLine = client->tcp->readStringUntil('\n');
                        handleHeader(client, &headerLine);
                    }
                        break;
                    case WSC_CONNECTED:
                        WebSockets::handleWebsocket(client);
                        break;
                    default:
                        WebSockets::clientDisconnect(client, 1002);
                        break;
                }
            }
        }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        delay(0);
#endif
    }
}
/**
 * send text data to client
 * @param num uint8_t client id
 * @param payload uint8_t *
 * @param length size_t
 * @param headerToPayload bool  (see sendFrame for more details)
 */
void WebSocketsClient::sendTXT(uint8_t * payload, size_t length, bool headerToPayload) {
    if(length == 0) {
        length = strlen((const char *) payload);
    }
    if(clientIsConnected(&_client)) {
        sendFrame(&_client, WSop_text, payload, length, true, true, headerToPayload);
    }
}
Ejemplo n.º 3
0
/**
 * send binary data to client
 * @param num uint8_t client id
 * @param payload uint8_t *
 * @param length size_t
 */
void WebSocketsServer::sendBIN(uint8_t num, uint8_t * payload, size_t length) {
    if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
        return;
    }
    WSclient_t * client = &_clients[num];
    if(clientIsConnected(client)) {
        sendFrame(client, WSop_binary, payload, length);
    }
}
Ejemplo n.º 4
0
/**
 * disconnect all clients
 */
void WebSocketsServer::disconnect(void) {
    WSclient_t * client;
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            WebSockets::clientDisconnect(client, 1000);
        }
    }
}
Ejemplo n.º 5
0
/**
 * disconnect one client
 * @param num uint8_t client id
 */
void WebSocketsServer::disconnect(uint8_t num) {
    if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
        return;
    }
    WSclient_t * client = &_clients[num];
    if(clientIsConnected(client)) {
        WebSockets::clientDisconnect(client, 1000);
    }
}
Ejemplo n.º 6
0
/**
 * get an IP for a client
 * @param num uint8_t client id
 * @return IPAddress
 */
IPAddress WebSocketsServer::remoteIP(uint8_t num) {
    if(num < WEBSOCKETS_SERVER_CLIENT_MAX) {
        WSclient_t * client = &_clients[num];
        if(clientIsConnected(client)) {
            return client->tcp.remoteIP();
        }
    }

    return IPAddress();
}
/**
 * send binary data to client
 * @param num uint8_t client id
 * @param payload uint8_t *
 * @param length size_t
 * @param headerToPayload bool  (see sendFrame for more details)
 * @return true if ok
 */
bool WebSocketsServer::sendBIN(uint8_t num, uint8_t * payload, size_t length, bool headerToPayload) {
    if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
        return false;
    }
    WSclient_t * client = &_clients[num];
    if(clientIsConnected(client)) {
        return sendFrame(client, WSop_binary, payload, length, false, true, headerToPayload);
    }
    return false;
}
/**
 * send binary data to client all
 * @param payload uint8_t *
 * @param length size_t
 * @param headerToPayload bool  (see sendFrame for more details)
 */
void WebSocketsServer::broadcastBIN(uint8_t * payload, size_t length, bool headerToPayload) {
    WSclient_t * client;
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            sendFrame(client, WSop_binary, payload, length, false, true, headerToPayload);
        }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        delay(0);
#endif
    }
}
Ejemplo n.º 9
0
/**
 * send text data to client
 * @param num uint8_t client id
 * @param payload uint8_t *
 * @param length size_t
 */
void WebSocketsServer::sendTXT(uint8_t num, uint8_t * payload, size_t length) {
    if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
        return;
    }
    if(length == 0) {
        length = strlen((const char *) payload);
    }
    WSclient_t * client = &_clients[num];
    if(clientIsConnected(client)) {
        sendFrame(client, WSop_text, payload, length);
    }
}
Ejemplo n.º 10
0
/**
 * send binary data to client all
 * @param payload uint8_t *
 * @param length size_t
 */
void WebSocketsServer::broadcastBIN(uint8_t * payload, size_t length) {
    WSclient_t * client;
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            sendFrame(client, WSop_binary, payload, length);
        }
#ifdef ESP8266
        delay(0);
#endif
    }
}
/*
 * send text data to client
 * @param num uint8_t client id
 * @param payload uint8_t *
 * @param length size_t
 * @param headerToPayload bool  (see sendFrame for more details)
 * @return true if ok
 */
bool WebSocketsServer::sendTXT(uint8_t num, uint8_t * payload, size_t length, bool headerToPayload) {
    if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
        return false;
    }
    if(length == 0) {
        length = strlen((const char *) payload);
    }
    WSclient_t * client = &_clients[num];
    if(clientIsConnected(client)) {
        return sendFrame(client, WSop_text, payload, length, false, true, headerToPayload);
    }
    return false;
}
/**
 * handle new client connection
 * @param client
 */
bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
    WSclient_t * client;
    // search free list entry for client
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];

        // state is not connected or tcp connection is lost
        if(!clientIsConnected(client)) {

            client->tcp = TCPclient;

#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
            client->isSSL = false;
            client->tcp->setNoDelay(true);
#endif
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
            // set Timeout for readBytesUntil and readStringUntil
            client->tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
#endif
            client->status = WSC_HEADER;
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
            IPAddress ip = client->tcp->remoteIP();
            DEBUG_WEBSOCKETS("[WS-Server][%d] new client from %d.%d.%d.%d\n", client->num, ip[0], ip[1], ip[2], ip[3]);
#else
            DEBUG_WEBSOCKETS("[WS-Server][%d] new client\n", client->num);
#endif


#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
            client->tcp->onDisconnect(std::bind([](WebSocketsServer * server, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
                DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);

                AsyncTCPbuffer ** sl = &server->_clients[client->num].tcp;
                if(*sl == obj) {
                    client->status = WSC_NOT_CONNECTED;
                    *sl = NULL;
                }
                return true;
            },  this, std::placeholders::_1, client));


            client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsServer::handleHeader, this, client, &(client->cHttpLine)));
#endif

            return true;
            break;
        }
    }
    return false;
}
Ejemplo n.º 13
0
/**
 * send text data to client all
 * @param payload uint8_t *
 * @param length size_t
 */
void WebSocketsServer::broadcastTXT(uint8_t * payload, size_t length) {
    WSclient_t * client;
    if(length == 0) {
        length = strlen((const char *) payload);
    }

    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            sendFrame(client, WSop_text, payload, length);
        }
#ifdef ESP8266
        delay(0);
#endif
    }
}
Ejemplo n.º 14
0
void DisplayClient::ledWrite(quint32 mask, quint32 values)
{
	if (!clientIsConnected())
		return;

	if (!clientIsServiceActive(m_controlPort))
		return;

	int length = 2 * sizeof(quint32);

	SYNTRO_EHEAD *head = clientBuildMessage(m_controlPort, length);

	quint32 *p = (quint32 *)(head + 1);

	p[0] = mask;
	p[1] = values;

	clientSendMessage(m_controlPort, head, length, SYNTROLINK_MEDPRI);
}
/**
 * send text data to client all
 * @param payload uint8_t *
 * @param length size_t
 * @param headerToPayload bool  (see sendFrame for more details)
 * @return true if ok
 */
bool WebSocketsServer::broadcastTXT(uint8_t * payload, size_t length, bool headerToPayload) {
    WSclient_t * client;
    bool ret = true;
    if(length == 0) {
        length = strlen((const char *) payload);
    }

    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            if(!sendFrame(client, WSop_text, payload, length, false, true, headerToPayload)) {
                ret = false;
            }
        }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        delay(0);
#endif
    }
    return ret;
}
Ejemplo n.º 16
0
/**
 * Handle incomming Connection Request
 */
void WebSocketsServer::handleNewClients(void) {
    WSclient_t * client;
    while(_server->hasClient()) {
        bool ok = false;
        // search free list entry for client
        for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
            client = &_clients[i];

            // state is not connected or tcp connection is lost
            if(!clientIsConnected(client)) {

                // store new connection
                client->tcp = _server->available();
#ifdef ESP8266
                client->tcp.setNoDelay(true);
#endif
                // set Timeout for readBytesUntil and readStringUntil
                client->tcp.setTimeout(WEBSOCKETS_TCP_TIMEOUT);
                client->status = WSC_HEADER;

                IPAddress ip = client->tcp.remoteIP();
                DEBUG_WEBSOCKETS("[WS-Server][%d] new client from %d.%d.%d.%d\n", client->num, ip[0], ip[1], ip[2], ip[3]);
                ok = true;
                break;
            }
        }

        if(!ok) {
            // no free space to handle client
            WiFiClient tcpClient = _server->available();
            IPAddress ip = tcpClient.remoteIP();
            DEBUG_WEBSOCKETS("[WS-Server] no free space new client from %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
            tcpClient.stop();
        }

#ifdef ESP8266
        delay(0);
#endif
    }
}
Ejemplo n.º 17
0
/**
 * called in arduino loop
 */
void WebSocketsClient::loop(void) {
    if(!clientIsConnected(&_client)) {

#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        if(_client.isSSL) {
            DEBUG_WEBSOCKETS("[WS-Client] connect wss...\n");
            if(_client.ssl) {
                delete _client.ssl;
                _client.ssl = NULL;
                _client.tcp = NULL;
            }
            _client.ssl = new WiFiClientSecure();
            _client.tcp = _client.ssl;
        } else {
            DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n");
            if(_client.tcp) {
                delete _client.tcp;
                _client.tcp = NULL;
            }
            _client.tcp = new WiFiClient();
        }
#else
        _client.tcp = new WEBSOCKETS_NETWORK_CLASS();
#endif

        if(!_client.tcp) {
            DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
            return;
        }

        if(_client.tcp->connect(_host.c_str(), _port)) {
            connectedCb();
        } else {
            connectFailedCb();
            delay(10); //some little delay to not flood the server
        }
    } else {
        handleClientData();
    }
}
Ejemplo n.º 18
0
/**
 * send binary data to client
 * @param num uint8_t client id
 * @param payload uint8_t *
 * @param length size_t
 * @param headerToPayload bool  (see sendFrame for more details)
 * @return true if ok
 */
bool WebSocketsClient::sendBIN(uint8_t * payload, size_t length, bool headerToPayload) {
    if(clientIsConnected(&_client)) {
        return sendFrame(&_client, WSop_binary, payload, length, true, true, headerToPayload);
    }
    return false;
}
Ejemplo n.º 19
0
bool NetwalkWindow::newRandomMap()
{
  int sdf = time(NULL);
//  cout << "rand: " << sdf << endl;
  srand ( sdf );
  bool found = false;
  int x;
  int y;
  for( int i = 0; i < pNumCli; i++ ) {
    while( !found ){
      x = rand() % pSizeX;
      y = rand() % pSizeY;
      if ( mapView.tiles(x, y).numObjects() == 1 ) {
        found = true;
        mapView.tiles(x, y).setNumObjects( 2 );
        mapView.tiles(x, y).objectImage( 0 ) = -1;
        mapView.tiles(x, y).objectImage( 1 ) = 30;
      //  cout << "Cli" << i << ": (" << x << ";" << y << ")" << endl;
      }
    }
    found = false;
  }

  while( !found ){
    pServX = rand() % pSizeX;
    pServY = rand() % pSizeY;
    if ( mapView.tiles(pServX, pServY).numObjects() == 1 ) {
      found = true;
      mapView.tiles(pServX, pServY).setNumObjects( 2 );
      mapView.tiles(pServX, pServY).objectImage( 0 ) = -1;
      mapView.tiles(pServX, pServY).objectImage( 1 ) = 28;
 //     cout << "Server: (" << pServX << ";" << pServY << ")" << endl;
    }
  }
  bool complete;
  do {
    complete = true;
    for( y = 0; y < pSizeY; y++ ){
      for( x = 0; x < pSizeY; x++ ){
        if ( isClient(x, y) ) {
          if ( !clientIsConnected( x, y ) ) {
  //          cout << "Conn Cli: (" << x << ";" << y << ")" << endl;
            if (!connectClient( x, y ) ) {
  //            cout << "!!! Conn Cli: (" << x << ";" << y << ")" << endl;
              return false;
/*              debugMap("Failed");
              mapView.tiles(x, y).setNumObjects( 1 );
              mapView.tiles(x, y).objectImage( 0 ) = -1;
              removeDir(x+1,y,3);
              removeDir(x-1,y,1);
              removeDir(x,y+1,0);
              removeDir(x,y-1,2);
              complete = false;
              for( int y2 = 0; y2 < pSizeX; y2++ ){
                for( int x2 = 0; x2 < pSizeX; x2++ ){
                  if ( ( mapView.tiles(x2, y2).objectImage( 0 ) > -1 ) && ( mapView.tiles(x2, y2).objectImage( 0 ) < 14 ))
                    mapView.tiles(x2, y2).objectImage( 0 ) = -1;
                }
              }*/
            } else {
              for( int y2 = 0; y2 < pSizeX; y2++ ){
                for( int x2 = 0; x2 < pSizeX; x2++ ){
                  if ( ( mapView.tiles(x2, y2).objectImage( 0 ) > -1 ) && ( mapView.tiles(x2, y2).objectImage( 0 ) < 14 ))
                    mapView.tiles(x2, y2).objectImage( 0 ) += 14;
                }
              }
            }
          }
        }
      }
    }
    if ( ! complete ) {
      found = false;
      while( !found ){
        x = rand() % pSizeX;
        y = rand() % pSizeY;
        if ( mapView.tiles(x, y).numObjects() == 1 ) {
          found = true;
          mapView.tiles(x, y).setNumObjects( 2 );
          mapView.tiles(x, y).objectImage( 0 ) = -1;
          mapView.tiles(x, y).objectImage( 1 ) = 30;
        }
      }
    }
  } while ( !complete );
  for ( int i = 0 ; i < (pSizeX*pSizeY)/2; i++ ){
    x = rand() % pSizeX;
    y = rand() % pSizeY;
    rotate(x,y,true);
    x = rand() % pSizeX;
    y = rand() % pSizeY;
    rotate(x,y,false);
  }
  checkFlow();
  return true;
}
Ejemplo n.º 20
0
void SyntroPythonClient::appClientBackground()
{
    if (clientIsConnected() && m_glue->isDirectoryRequested())
        requestDirectory();
}
Ejemplo n.º 21
0
/**
 * disconnect one client
 * @param num uint8_t client id
 */
void WebSocketsClient::disconnect(void) {
    if(clientIsConnected(&_client)) {
        WebSockets::clientDisconnect(&_client, 1000);
    }
}
/**
 * called in arduino loop
 */
void WebSocketsClient::loop(void) {
    if(!clientIsConnected(&_client)) {

#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        if(_client.isSSL) {
            DEBUG_WEBSOCKETS("[WS-Client] connect wss...\n");
            if(_client.ssl) {
                delete _client.ssl;
                _client.ssl = NULL;
                _client.tcp = NULL;
            }
            _client.ssl = new WiFiClientSecure();
            _client.tcp = _client.ssl;
        } else {
            DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n");
            if(_client.tcp) {
                delete _client.tcp;
                _client.tcp = NULL;
            }
            _client.tcp = new WiFiClient();
        }
#else
        _client.tcp = new WEBSOCKETS_NETWORK_CLASS();
#endif

        if(!_client.tcp) {
            DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
            return;
        }

        if(_client.tcp->connect(_host.c_str(), _port)) {
            DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);

            _client.status = WSC_HEADER;

            // set Timeout for readBytesUntil and readStringUntil
            _client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);

#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
            _client.tcp->setNoDelay(true);

            if(_client.isSSL && _fingerprint.length()) {
                if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
                    DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
                    WebSockets::clientDisconnect(&_client, 1000);
                    return;
                }
            }
#endif

            // send Header to Server
            sendHeader(&_client);

        } else {
            DEBUG_WEBSOCKETS("[WS-Client] connection to %s:%u Faild\n", _host.c_str(), _port);
            delay(10); //some litle delay to not flood the server
        }
    } else {
        handleClientData();
    }
}