예제 #1
0
파일: allocator.cpp 프로젝트: Fan-Luo/mafia
int SparseAllocator::ReadRequest( int in, int out ) const
{
   sRequest r;

   if ( ! ReadRequest( r, in, out ) ) {
      r.label = -1;
   }

   return r.label;
}
예제 #2
0
파일: main.c 프로젝트: nicolay-r/GL-gwbasic
/*
	Run GWBasic Shell
*/
void RunShell(GWBE_Environment* env)
{
	assert(env != NULL);

	while (1)
	{
		ReadRequest(env->input);
		/* Interpretate user request */
		gwbr_Run(env);
	}	
}
예제 #3
0
void Wavefront::AddRequest( int in, int out, int label, 
			    int in_pri, int out_pri )
{
  // count unique requests
  sRequest req;
  bool overwrite = ReadRequest(req, in, out);
  if(!overwrite || (req.in_pri < in_pri)) {
    _num_requests++;
    _last_in = in;
    _last_out = out;
  }
  DenseAllocator::AddRequest(in, out, label, in_pri, out_pri);
}
예제 #4
0
void I2PControlService::HandleAccept(
    const boost::system::error_code& ecode,
    std::shared_ptr<boost::asio::ip::tcp::socket> socket) {
  if (ecode != boost::asio::error::operation_aborted)
    Accept();
  if (!ecode) {
    LogPrint(eLogInfo,
        "New I2PControl request from ", socket->remote_endpoint());
    std::this_thread::sleep_for(std::chrono::milliseconds(5));
    ReadRequest(socket);
  } else {
    LogPrint(eLogError, "I2PControl accept error: ",  ecode.message());
  }
}
예제 #5
0
void
HandleConnection(PRFileDesc* aSocket, const UniquePRFileDesc& aModelSocket)
{
  Connection conn(aSocket);
  nsresult rv = SetupTLS(&conn, aModelSocket.get());
  if (NS_FAILED(rv)) {
    PR_SetError(PR_INVALID_STATE_ERROR, 0);
    PrintPRError("PR_Recv failed");
    exit(1);
  }

  // TODO: On tests that are expected to fail (e.g. due to a revoked
  // certificate), the client will close the connection wtihout sending us the
  // request byte. In those cases, we should keep going. But, in the cases
  // where the connection is supposed to suceed, we should verify that we
  // successfully receive the request and send the response.
  rv = ReadRequest(&conn);
  if (NS_SUCCEEDED(rv)) {
    rv = ReplyToRequest(&conn);
  }
}
예제 #6
0
void Connection::Process(bool executeAfterRead)
{
    log_info("Process: fd=" << socket_.GetFileDescriptor());
    bool newMessage = true;
    while (newMessage)
    {
        newMessage = false;
        if ((connectionState_ == READ_HEADER) && (!ReadHeader()))
        {
            connectionState_ = CLOSE_CONNECTION;
            break;
        }
        if ((connectionState_ == READ_REQUEST) && (!ReadRequest()))
        {
            connectionState_ = CLOSE_CONNECTION;
            break;
        }
        // When operating a thread pool, the main thread will read the request
        // but stop to transfer it to a worker thread for execution
        if (executeAfterRead)
        {
            if ((connectionState_ == EXECUTE_REQUEST) && (!ExecuteRequest()))
            {
                connectionState_ = CLOSE_CONNECTION;
                break;
            }
        }
        // When operating a thread pool, the main thread might need to continue
        // to write a long response to the socket after the connection is returned
        if (connectionState_ == WRITE_RESPONSE)
        {
            if (!WriteResponse())
            {
                connectionState_ = CLOSE_CONNECTION;
                break;
            }
            newMessage = (connectionState_ == READ_HEADER) && (bufferLength_ > 0);
        }
    }
}
예제 #7
0
int main() {
    int res = 0;

    //chdir(WorkDir);
    stdoutfd = dup(STDOUT_FILENO);
    stderrfd = dup(STDERR_FILENO);
    CreateConnection();

    /*** when a connection comes ***/
    for(;;) {

        clilen = sizeof(cli_addr);
        connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
        if(clientCount < LISTEN_MAX_NUM) {

            if(connfd < 0) {
                printf("error: accept error (errno: %d - %s)\n", errno, strerror(errno));fflush(stdout);
                close(connfd);
                bzero(&curClient, sizeof(curClient));
                bzero(&cli_addr, sizeof(cli_addr));
                clilen = 0;
                connfd = 0;
            }
            else {
                printf("accept\n");fflush(stdout);

                NewClient();

                /*** read request ***/
                if( (res = ReadRequest()) < 0) continue;

                /*** send status response ***/
                write(curClient.fd, contentType, strlen(contentType));
                write(curClient.fd, status200, strlen(status200));

                /*** fork a child process for client ***/
                if( (childPid = fork()) == -1) {
                    printf("error: fork() error (errno: %d - %s)\n", errno, strerror(errno));fflush(stdout);
                    Disconnect();
                }
                else if(childPid == 0) {
                    /*** child process: client work environment ***/

                    close(listenfd);
                    listenfd = -1;

                    /*** parse headers ***/
                    char method[5];
                    char target[HEADER_MAX_LEN];
                    char queryString[HEADER_MAX_LEN];
                    ParseMethod(method, target, queryString);
                    
                    /*** execute method ***/
                    ExectueMethod();

                    exit(0);
                }
                else {
                    /*** parent process ***/

                    close(connfd);
                    bzero(&curClient, sizeof(curClient));
                    bzero(&cli_addr, sizeof(cli_addr));
                    clilen = 0;
                    connfd = 0;
                    bzero(buffer, BUF_MAX_SIZE);
                    bzero(request, sizeof(request));
                }
            }
        }
        else {
            printf("error: too many clients now (errno: %d - %s)\n", errno, strerror(errno));fflush(stdout);
            close(connfd);
            bzero(&cli_addr, sizeof(cli_addr));
            clilen = 0;
            connfd = 0;
        }
    }
    return 0;
}
예제 #8
0
Dispatch()
{
    int         nready,
                result;
    int        *clientReady;
    ClientPtr   client;
    int		op;

    nextFreeClientID = MINCLIENT;
    nClients = 0;

    clientReady = (int *) ALLOCATE_LOCAL(sizeof(int) * MaxClients);
    if (!clientReady)
	return;

    while (1) {
	/* wait for something */
	nready = WaitForSomething(clientReady);

	while (!dispatchException && (--nready >= 0)) {
	    client = currentClient = clients[clientReady[nready]];

	    /* Client can be NULL if CloseDownClient() is called during
	       this dispatchException loop. */
	    if (client == (ClientPtr)NULL) continue;

	    isItTimeToYield = FALSE;

	    while (!isItTimeToYield) {
		result = ReadRequest(client);
		if (result <= 0) {
		    if (result < 0)
			CloseDownClient(client);
		    break;
		}
		client->sequence++;

		if (result > (MAX_REQUEST_SIZE << 2))
		    result = FSBadLength;
		else
		{
		    op = MAJOROP;
		    if (op >= NUM_PROC_VECTORS)
			result = ProcBadRequest (client);
		    else
			result = (*client->requestVector[op]) (client);
		}
		if (result != FSSuccess) {
		    if (client->noClientException != FSSuccess)
			CloseDownClient(client);
		    break;
		}
	    }
	    FlushAllOutput ();
	}
	/* reset if server is a drone and has run out of clients */
	if (drone_server && nClients == 0) {
	    dispatchException |= DE_RESET;
	}
	if (dispatchException) {
	    /* re-read the config file */
	    if (dispatchException & DE_RECONFIG) {
		NoticeF("Re-reading config file\n");
		if (ReadConfigFile(configfilename) != FSSuccess)
		    ErrorF("couldn't parse config file\n");
		SetConfigValues();
		dispatchException &= ~DE_RECONFIG;
	    }
	    /* flush all the caches */
	    if (dispatchException & DE_FLUSH) {
		NoticeF("flushing all caches\n");
		CacheReset();
		dispatchException &= ~DE_FLUSH;
	    }
	    /* reset */
	    if (dispatchException & DE_RESET) {
		NoticeF("resetting\n");
		break;
	    }
	    /* die *now* */
	    if (dispatchException & DE_TERMINATE) {
		NoticeF("terminating\n");
		kill_all_clients();
		exit(0);
		break;
	    }
	}
    }
    kill_all_clients();
    dispatchException = 0;
}