Пример #1
0
//--------------------------------------------------------------
///  Initializes webserver
/// \param port Port to listen on.
/// \return Socket to listen to.
//--------------------------------------------------------------
NetSocket* WebServerSetup(int port)
{
    NetSocket* server_socket;

    s_dwPort = port;

#ifdef DEBUG_COMMS_PERFORMANCE
    // set the performance counter frequency
    unsigned __int64 nPerfCountFreq ;
    QueryPerformanceFrequency((LARGE_INTEGER*)&nPerfCountFreq);
    CommandTimingManager::Instance()->SetPerformanceCounterFrequency(1.0 / (double)nPerfCountFreq);
#endif

    s_output_criticalSection = new osCriticalSection();

    NetSocket* s = NetSocket::Create();

    if (s == NULL)
    {
        LogConsole(logERROR, "Error creating sock()\n");
        return NULL;
    }

    // init the webserver
    server_socket = WebServerBind(s);

    if (server_socket)
    {
        if (WebServerListen(server_socket))
        {
            return server_socket;
        }
        else
        {
            LogConsole(logERROR, "can't listen\n");
        }
    }
    else
    {
        LogConsole(logERROR, "Error binding socket\n");
    }

    return NULL;
}
Пример #2
0
/**
  Entry point for the web server application.

  @param [in] Argc  The number of arguments
  @param [in] Argv  The argument value array

  @retval  0        The application exited normally.
  @retval  Other    An error occurred.
**/
int
main (
  IN int Argc,
  IN char **Argv
  )
{
  UINT16 HttpPort;
  UINTN Index;
  DT_WEB_SERVER * pWebServer;
  EFI_STATUS Status;
  UINT64 TriggerTime;

  //
  //  Get the HTTP port
  //
  HttpPort = PcdGet16 ( WebServer_HttpPort );
  DEBUG (( DEBUG_HTTP_PORT,
            "HTTP Port: %d\r\n",
            HttpPort ));

  //
  //  Create a timer event to start HTTP port
  //
  pWebServer = &mWebServer;
  Status = gBS->CreateEvent ( EVT_TIMER,
                              TPL_WEB_SERVER,
                              NULL,
                              NULL,
                              &pWebServer->TimerEvent );
  if ( !EFI_ERROR ( Status )) {
    TriggerTime = HTTP_PORT_POLL_DELAY * ( 1000 * 10 );
    Status = gBS->SetTimer ( pWebServer->TimerEvent,
                             TimerPeriodic,
                             TriggerTime );
    if ( !EFI_ERROR ( Status )) {
      //
      //  Run the web server forever
      //
      pWebServer->HttpListenPort = -1;
      pWebServer->HttpListenPort6 = -1;
      pWebServer->bRunning = TRUE;
      do {
        //
        //  Poll the network layer to create the HTTP port
        //  for the web server.  More than one attempt may
        //  be necessary since it may take some time to get
        //  the IP address and initialize the upper layers
        //  of the network stack.
        //
        if (( -1 == pWebServer->HttpListenPort )
          || ( -1 == pWebServer->HttpListenPort6 )) {
          do {
            //
            //  Wait a while before polling for a connection
            //
            if ( EFI_SUCCESS != gBS->CheckEvent ( pWebServer->TimerEvent )) {
              if ( 0 != pWebServer->Entries ) {
                  break;
              }
              gBS->WaitForEvent ( 1, &pWebServer->TimerEvent, &Index );
            }

            //
            //  Poll for a network connection
            //
            if ( -1 == pWebServer->HttpListenPort ) {
              WebServerListen ( pWebServer,
                                AF_INET,
                                IPPROTO_TCP,
                                HttpPort,
                                &pWebServer->HttpListenPort );
            }
            if ( -1 == pWebServer->HttpListenPort6 ) {
              WebServerListen ( pWebServer,
                                AF_INET6,
                                IPPROTO_TCP,
                                HttpPort,
                                &pWebServer->HttpListenPort6 );
            }

            //
            //  Continue polling while both network connections are
            //  not present
            //
          } while ( 0 == pWebServer->Entries );
        }

        //
        //  Poll the sockets for activity while both network
        //  connections are connected
        //
        do {
          SocketPoll ( pWebServer );
        } while ( pWebServer->bRunning
                && ( -1 != pWebServer->HttpListenPort )
                && ( -1 != pWebServer->HttpListenPort6 ));

        //
        //  Continue polling the network connections until both
        //  TCP4 and TCP6 are connected
        //
      } while ( pWebServer->bRunning );

      //
      //  Stop the timer
      //
      gBS->SetTimer ( pWebServer->TimerEvent,
                      TimerCancel,
                      0 );
    }

    //
    //  Done with the timer event
    //
    gBS->CloseEvent ( pWebServer->TimerEvent );
  }

  //
  //  Return the final status
  //
  DBG_EXIT_STATUS ( Status );
  return Status;
}