static std::string
ReadMessageFromAdb (Connection& conn, bool has_okay, Error& error)
{
    ConnectionStatus status;

    char buffer[5];
    buffer[4] = 0;

    if (has_okay)
    {
        conn.Read (buffer, 4, g_adb_timeout, status, &error);
        if (error.Fail ())
            return "";

        if (strncmp (buffer, "OKAY", 4) != 0)
        {
            error.SetErrorStringWithFormat ("\"OKAY\" expected from adb, received: \"%s\"", buffer);
            return "";
        }
    }

    conn.Read (buffer, 4, g_adb_timeout, status, &error);
    if (error.Fail())
        return "";

    size_t packet_len = 0;
    sscanf(buffer, "%zx", &packet_len);
    std::string result(packet_len, 0);
    conn.Read (&result[0], packet_len, g_adb_timeout, status, &error);
    if (error.Fail ())
        return "";

    return result;
}
    void EventLoop()
    {
        Connection *fromClient = parent.GetWriteConnection();
        Connection *toClient = parent.GetReadConnection();
        bool keepGoing = true;

        while(keepGoing)
        {
            bool haveMessage = true;
            if(par_rank == 0)
                haveMessage = fromClient->NeedsRead(true);

            if(haveMessage)
            {
                // Read the message and send it to the other ranks.
                int amountRead = 0;
                if(par_rank == 0)
                    amountRead = fromClient->Fill();
#ifdef PARALLEL
                MPI_Bcast((void*)&amountRead, 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif
                if(amountRead > 0)
                {
                    char *str = new char[amountRead + 1];
                    if(par_rank == 0)
                    {
                        for(int i = 0; i < amountRead; ++i)
                            fromClient->Read((unsigned char *)(str + i));
                        str[amountRead] = '\0';
                    }
                    
#ifdef PARALLEL
                    MPI_Bcast((void*)str, amountRead+1, MPI_CHAR, 0, MPI_COMM_WORLD);
#endif

                    if(par_rank == 0)
                        cout << "Server: received: \"" << str << "\"" << endl;
                    debug1 << "MESSAGE FROM CLIENT: " << messageNumber << ": " << str << endl;
                    messageNumber++;

                    // Send the message back to the client.
                    if(par_rank == -1)
                    {
                        debug1 << "SENDING MESSAGE TO CLIENT: " << str << endl;
                        toClient->Append((const unsigned char *)str, amountRead+1);
                        toClient->Flush();
                    }

                    if(strcmp(str, "quit") == 0)
                        keepGoing = false;

                    delete [] str;
                }
            }
        }
    }
Exemple #3
0
void * http_event_loop(void *arg){
  int event_thread_id = (*(int *)arg);
  int sock;
  
  ssize_t read_size;

  int request_counter=0;

  int event_num;
  int max_events = 10;
  int i;
  int ret;
  EpollManager *epoll_mng;

  Connection *conn = NULL;

  struct epoll_event events[max_events];
  for(i=0; i<max_events; i++){
    memset(&events[i], 0, sizeof(events[i]));    
  }

  epoll_mng = server->getEpoll(event_thread_id);
  if(epoll_mng == NULL){
    printf("Error: getEpoll returned -1\n");
    exit(-1);
  }

  while(1){

    event_num = epoll_wait(epoll_mng->epoll, events, max_events, EPOLL_WAIT_TIMEOUT_MSEC);

    if(event_num == -1){ perror("epoll_wait"); break; }
    if(IsStop()){ break; }

    //printf("##### epoll wait : event num = %d\n", event_num);

    for(i=0; i<event_num; i++){
      conn = (Connection *)events[i].data.ptr;
      if(conn == NULL){
	printf("Error: epoll event do not have connection\n");
	exit(-1);
      }
      sock = conn->sock;
      
      if(events[i].events & EPOLLIN){
	// Read
	ret = conn->Read();
	if(ret <= 0 ){ epoll_mng->CloseConnection(conn);  delete conn;  conn = NULL; continue; }

	// Process
	ret = conn->Process();
	if(ret <= 0 ){ epoll_mng->CloseConnection(conn);  delete conn;  conn = NULL; continue; }	
      }

      if(events[i].events & EPOLLOUT){
	// Send
	ret = conn->Send();
	if(ret <= 0 ){ epoll_mng->CloseConnection(conn);  delete conn;  conn = NULL; continue; }
      }

      if((events[i].events & EPOLLRDHUP) || 
	 (events[i].events & EPOLLERR)   || 
	 (events[i].events & EPOLLHUP) ){
	epoll_mng->CloseConnection(conn);  delete conn;  conn = NULL;
	continue;
      }
    }

    epoll_mng->CheckAndCloseConnections();

  }

  pthread_exit(NULL);
}
Exemple #4
0
 u32 Read()
 {
   return m_connection->Read(m_child, readBuffer, PACKETSIZE);
 }