Example #1
0
//--tested, working--//
WiFiClient WiFiServer::available(byte* status)
{
    //
    //Get a socket number from the wificlass
    //
    int clientSocketIndex = WiFiClass::getSocket();
    if (clientSocketIndex == NO_SOCKET_AVAIL) {
        return WiFiClient(255);
    }
    
    //
    //create the client address structure to be filled in by sl_accept
    //
    SlSockAddrIn_t clientAddress = {0};
    unsigned int clientAddressSize = sizeof(clientAddress);
    
    //
    //get the client handle, if there's a queued client. If no client, return 0
    //
    int socketHandle = WiFiClass::_handleArray[_socketIndex];
    int clientHandle = sl_Accept(socketHandle, (SlSockAddr_t*)&clientAddress, &clientAddressSize);
    
    //
    //We've successfully created a socket, so store everything in the wificlass
    //arrays used to keep track of the connected sockets, port #s, and types
    //
    if (clientHandle > 0) {
        WiFiClass::_handleArray[clientSocketIndex] = clientHandle;
        WiFiClass::_typeArray[clientSocketIndex] = TYPE_TCP_CONNECTED_CLIENT;
        WiFiClass::_portArray[clientSocketIndex] = sl_Htons(clientAddress.sin_port);
        WiFiClass::clients[clientSocketIndex] = WiFiClient(clientSocketIndex);
    }

    //
    //Now loop through the connected clients
    //

    uint8_t oneclient = 0;
    for(uint8_t i = 0; i < MAX_SOCK_NUM; i++) {
        if(WiFiClass::_handleArray[i] != -1 && WiFiClass::_typeArray[i] == TYPE_TCP_CONNECTED_CLIENT) {

            if( i == _lastServicedClient) {
                oneclient = 1;
                continue;
            }

            _lastServicedClient = i;
            return WiFiClass::clients[i];
        }
    }

    if(oneclient) {
        return WiFiClass::clients[_lastServicedClient];
    }

    _lastServicedClient = -1;
    return WiFiClient(255);
}
Example #2
0
void WebServer::_handleRequest() {
  RequestHandler* handler;
  for (handler = _firstHandler; handler; handler = handler->next)
  {
    if (handler->method != HTTP_ANY && handler->method != _currentMethod)
      continue;

    if (handler->uri != _currentUri)
      continue;

    handler->fn();
    break;
  }

  if (!handler){
#ifdef DEBUG
    DEBUG_OUTPUT.println("request handler not found");
#endif

    if(_notFoundHandler) {
      _notFoundHandler();
    }
    else {
      printHead(404,"text/plain");
      print(String("Not found: ") + _currentUri);
      closeClient();
    }
  }

  _currentClient   = WiFiClient();
  _currentUri      = String();
}
Example #3
0
void ESP8266WebServer::_handleRequest() {
  RequestHandler* handler;
  for (handler = _firstHandler; handler; handler = handler->next()) {
    if (handler->handle(*this, _currentMethod, _currentUri))
      break;
  }

  if (!handler){
#ifdef DEBUG
    DEBUG_OUTPUT.println("request handler not found");
#endif

    if(_notFoundHandler) {
      _notFoundHandler();
    }
    else {
      send(404, "text/plain", String("Not found: ") + _currentUri);
    }
  }

  uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
  while(_currentClient.connected() && maxWait--) {
    delay(1);
  }
  _currentClient   = WiFiClient();
  _currentUri      = String();
}
void ESP8266WebServer::_handleRequest() {
  bool handled = false;
  if (!_currentHandler){
#ifdef DEBUG_ESP_HTTP_SERVER
    DEBUG_OUTPUT.println("request handler not found");
#endif
  }
  else {
    handled = _currentHandler->handle(*this, _currentMethod, _currentUri);
#ifdef DEBUG_ESP_HTTP_SERVER
    if (!handled) {
      DEBUG_OUTPUT.println("request handler failed to handle request");
    }
#endif
  }

  if (!handled) {
    if(_notFoundHandler) {
      _notFoundHandler();
    }
    else {
      send(404, "text/plain", String("Not found: ") + _currentUri);
    }
  }

  uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
  while(_currentClient.connected() && maxWait--) {
    delay(1);
  }
  _currentClient   = WiFiClient();
  _currentUri      = String();
}
Example #5
0
WiFiClient WiFiServer::available(byte* status)
{
	static int cycle_server_down = 0;
	const int TH_SERVER_DOWN = 50;

    for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
    {
        if (WiFiClass::_server_port[sock] == _port)
        {
        	WiFiClient client(sock);
            uint8_t _status = client.status();
            uint8_t _ser_status = this->status();

            if (status != NULL)
            	*status = _status;

            //server not in listen state, restart it
            if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN))
            {
            	ServerDrv::startServer(_port, sock);
            	cycle_server_down = 0;
            }

            if (_status == ESTABLISHED)
            {                
                return client;  //TODO 
            }
        }
    }

    return WiFiClient(255);
}
void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod method) {
  _currentClient   = client;
  _currentUri      = uri;
  _currentMethod   = method;

  RequestHandler* handler;
  for (handler = _firstHandler; handler; handler = handler->next)
  {
    if (handler->method != HTTP_ANY && handler->method != method)
      continue;

    if (handler->uri != uri)
      continue;

    handler->fn();
    break;
  }

  if (!handler){
#ifdef DEBUG
    Serial.println("request handler not found");
#endif

    if(_notFoundHandler) {
      _notFoundHandler();
    }
    else {
      send(404, "text/plain", String("Not found: ") + uri);
    }
  }

  _currentClient   = WiFiClient();
  _currentUri      = String();
  
}
WiFiClient WiFiServer::available(byte* status)
{
    static uint32_t lastPollTime = 0;

    if (_unclaimed)
    {
        WiFiClient result(_unclaimed);
        _unclaimed = _unclaimed->next();
        DEBUGV("WS:av\r\n");
        return result;
    }

    if (lastPollTime > esp_micros_at_task_start())
        yield();
    lastPollTime = micros();

    return WiFiClient();
}
Example #8
0
 WiFiClient WiFiServer::available()
 {
	sockaddr clientaddr;
	socklen_t addrlen;	
	WiFi.countSocket(1);


	addrlen = sizeof(clientaddr);

    clientDescriptor = accept(serverSocket, (sockaddr *) &clientaddr, &addrlen);
	if(clientDescriptor<0) //return 0;
	return WiFiClient(255);

	WiFiClient client(clientDescriptor);

	return client;

 }
Example #9
0
void setup() {
  WiFi = WiFiClass();
  client = WiFiClient();

  delay(500); // Waiting for initialization

//  SPI.begin();
  //Initialize Serial5 and wait for port to open:
  Serial5.begin(9600); 
  while (!Serial5) {
    ; // wait for Serial5 port to connect. Needed for Leonardo only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial5.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
 // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial5.print("Attempting to connect to WPA SSID: ");
    Serial5.println(ssid);
    // Connect to WPA/WPA2 network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
   
  // you're connected now, so print out the data:
  Serial5.print("You're connected to the network");
  printCurrentNet();
  printWifiData();

}
Example #10
0
void ESP8266WebServer::handleClient() {
  if (_currentStatus == HC_NONE) {
    WiFiClient client = _server.available();
    if (!client) {
      return;
    }

#ifdef DEBUG_ESP_HTTP_SERVER
    DEBUG_OUTPUT.println("New client");
#endif

    _currentClient = client;
    _currentStatus = HC_WAIT_READ;
    _statusChange = millis();
  }

  if (!_currentClient.connected()) {
    _currentClient = WiFiClient();
    _currentStatus = HC_NONE;
    return;
  }

  // Wait for data from client to become available
  if (_currentStatus == HC_WAIT_READ) {
    if (!_currentClient.available()) {
      if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) {
        _currentClient = WiFiClient();
        _currentStatus = HC_NONE;
      }
      yield();
      return;
    }

    if (!_parseRequest(_currentClient)) {
      _currentClient = WiFiClient();
      _currentStatus = HC_NONE;
      return;
    }
    _currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
    _contentLength = CONTENT_LENGTH_NOT_SET;
    _handleRequest();

    if (!_currentClient.connected()) {
      _currentClient = WiFiClient();
      _currentStatus = HC_NONE;
      return;
    } else {
      _currentStatus = HC_WAIT_CLOSE;
      _statusChange = millis();
      return;
    }
  }

  if (_currentStatus == HC_WAIT_CLOSE) {
    if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) {
      _currentClient = WiFiClient();
      _currentStatus = HC_NONE;
    } else {
      yield();
      return;
    }
  }
}
Example #11
0
void ESP8266WebServer::handleClient() {
  if (_currentStatus == HC_NONE) {
    WiFiClient client = _server.available();
    if (!client) {
      return;
    }

#ifdef DEBUG_ESP_HTTP_SERVER
    DEBUG_OUTPUT.println("New client");
#endif

    _currentClient = client;
    _currentStatus = HC_WAIT_READ;
    _statusChange = millis();
  }

  bool keepCurrentClient = false;
  bool callYield = false;

  if (_currentClient.connected()) {
    switch (_currentStatus) {
    case HC_NONE:
        break;
    case HC_WAIT_READ:
      // Wait for data from client to become available
      if (_currentClient.available()) {
        if (_parseRequest(_currentClient)) {
          _currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
          _contentLength = CONTENT_LENGTH_NOT_SET;
          _handleRequest();

          if (_currentClient.connected()) {
            _currentStatus = HC_WAIT_CLOSE;
            _statusChange = millis();
            keepCurrentClient = true;
          }
        }
      } else { // !_currentClient.available()
        if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) {
          keepCurrentClient = true;
        }
        callYield = true;
      }
      break;
    case HC_WAIT_CLOSE:
      // Wait for client to close the connection
      if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
        keepCurrentClient = true;
        callYield = true;
      }
    }
  }

  if (!keepCurrentClient) {
    _currentClient = WiFiClient();
    _currentStatus = HC_NONE;
    _currentUpload.reset();
  }

  if (callYield) {
    yield();
  }
}