Exemplo n.º 1
0
	int LoopDeviceSvcHandler::svc()
	{
		KSG_GW_PACK_t pack;
		int ret;
		do
		{
			ACE_OS::memset(&pack,0,sizeof pack);
			if(RecvRequest(&ret,&pack))
			{
				ACE_DEBUG((LM_ERROR,"处理请求失败!"));
				break;
			}
			// 处理结果成功,并且有后续包
			if(!ret && pack.next_pack)
			{
				ACE_Time_Value tv(3);
				while((ret = ACE::handle_read_ready(peer().get_handle(),&tv)) <= 0)
				{
					if(ret != EWOULDBLOCK)
						break;
				}
				if(RecvData(peer().get_handle()))
				{
					ACE_DEBUG((LM_ERROR," 接收后续数据包失败"));
					break;
				}
			}
			else
				break;
		}while(1);
		peer().close();
		return 0;
	}
Exemplo n.º 2
0
unsigned __stdcall ClientThread(void *pVoid)
{
	int nRet;
	BYTE buf[1024];
	LPREQUEST lpReq = (LPREQUEST)pVoid;

	//
	// Count this client
	//
	IncrementClientCount();

	//
	// Recv the request data
	//
	if (!RecvRequest(lpReq, buf, sizeof(buf)))
	{
		CloseConnection(lpReq);
		free(lpReq);
		DecrementClientCount();
		return 0;
	}

	//
	// Parse the request info
	//
	nRet = ParseRequest(lpReq, buf);
	if (nRet)
	{
		SendError(lpReq, nRet);
		CloseConnection(lpReq);
		free(lpReq);
		DecrementClientCount();
		return 0;
	}

	//
	// Send the file to the client
	//
	SendFile(lpReq);

	//
	// Clean up
	CloseConnection(lpReq);
	free(pVoid);

	//
	// Subtract this client
	//
	DecrementClientCount();
	return 0;
}
/******************************************************************
Function	: ConnectClient
Date		: 2015-05-09 10:40:37
Author		: xiaoheike
Parameter	: 无
Return		: void
Desc		: 连接教师机,得到SOCKET
******************************************************************/
void CControlCenter::ConnectClient()
{
	while (true)
	{
		m_socketMsg = m_socketCenter.InitSocket(m_IP, m_port);
		if (m_socketMsg == SOCKET_ERROR)
		{
			Sleep(3000);
			continue;
		}
		else
		{
			break;
		}
	}
	RecvRequest();
}
Exemplo n.º 4
0
bool CHttpProtocol::Connect()
{
	SOCKADDR_IN	SockAddr;
	PREQUEST pReq = new REQUEST;
	if (pReq == NULL)
	{   
		// 处理错误
		printf("No memory for request\n");
		return false;
	}
	pReq->Socket = INVALID_SOCKET;
	pReq->hFile = INVALID_HANDLE_VALUE;
	pReq->dwRecv = 0;
	pReq->dwSend = 0;

	// Accept a client socket
	pReq->Socket = accept(ListenSocket, (LPSOCKADDR)&SockAddr, NULL);
	// No longer need server socket
    closesocket(ListenSocket);

	if (pReq->Socket == INVALID_SOCKET)
	{
		printf("accept failed: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return false;
	}
	// 将客户端网络地址转换为用点分割的IP地址
	printf("%s Connecting on socket:%d\n", inet_ntoa(SockAddr.sin_addr), pReq->Socket);

	char buf[DATA_BUFSIZE] = "";
	bool nRet;

	// 接收request data
	if (!RecvRequest(pReq, buf, sizeof(buf)))
	{
		Disconnect(pReq);
		delete pReq;
		return false;
	}
	 // 分析request信息
	Analyse(pReq, buf);
	if(!strcmp(HTTP_STATUS_NOTIMPLEMENTED, pReq->StatuCodeReason))
	{
		printf("Request method not implemented\n");
		return false;
	}
	// 生成并返回头部
	if(!SendHeader(pReq))
	{
		printf("Send header failed\n");
		return false;
	}

	// 向client传送数据
	if(pReq->nMethod == METHOD_GET)
		SendFile(pReq);
	Disconnect(pReq);
	delete pReq;
	return true;
}
Exemplo n.º 5
0
static DispatchReturn NaClSrpcReceiveAndDispatch(NaClSrpcChannel* channel,
                                                 NaClSrpcRpc* rpc_stack_top) {
  NaClSrpcRpc rpc;
  NaClSrpcArg* args[NACL_SRPC_MAX_ARGS + 1];
  NaClSrpcArg* rets[NACL_SRPC_MAX_ARGS + 1];
  NaClSrpcMethod method;
  ssize_t bytes_read;
  RpcCheckingClosure* closure = NULL;
  /* DISPATCH_EOF is the closest we have to an error return. */
  DispatchReturn dispatch_return = DISPATCH_EOF;

  closure = (RpcCheckingClosure*) malloc(sizeof *closure);
  if (NULL == closure) {
    NaClSrpcLog(NACL_SRPC_LOG_ERROR,
                "NaClSrpcReceiveAndDispatch(channel=%p):"
                "closure malloc failed\n",
                (void*) channel);
    dispatch_return = DISPATCH_EOF;
    goto done;
  }
  if (!NaClSrpcRpcCtor(&rpc, channel) ||
      !RpcCheckingClosureCtor(closure, &rpc)) {
    NaClSrpcLog(NACL_SRPC_LOG_ERROR,
                "NaClSrpcReceiveAndDispatch(channel=%p): constructor failed\n",
                (void*) channel);
    goto done;
  }
  rpc.rets = rets;
  /* Read a message from the channel. */
  bytes_read = SrpcPeekMessage(channel->message_channel, &rpc);
  if (bytes_read < 0) {
    goto done;
  }
  if (rpc.is_request) {
    /* This is a new request. */
    if (NULL == channel->server) {
      if (NULL == rpc_stack_top) {
        /* There is no service to dispatch requests. Abort. */
        dispatch_return = DISPATCH_EOF;
        goto done;
      } else {
        /* Inform the pending invoke that a failure happened. */
        NaClSrpcLog(NACL_SRPC_LOG_ERROR,
                    "NaClSrpcReceiveAndDispatch(channel=%p):"
                    "out of order request\n",
                    (void*) channel);
        rpc_stack_top->result = NACL_SRPC_RESULT_INTERNAL;
        dispatch_return = DISPATCH_BREAK;
        goto done;
      }
    }
    /* Fall through to request handling below. */
  } else {
    /* This is a response to a pending request. */
    if (NULL == rpc_stack_top) {
      NaClSrpcLog(NACL_SRPC_LOG_ERROR,
                  "NaClSrpcReceiveAndDispatch(channel=%p):"
                  "response, no pending request\n",
                  (void*) channel);
      /* There is no pending request. Abort. */
      dispatch_return = DISPATCH_BREAK;
      goto done;
    } else {
      if (rpc.request_id == rpc_stack_top->request_id) {
        /* Copy the serialized portion of the Rpc and process as a response. */
        memcpy(rpc_stack_top, &rpc, kRpcSize);
        dispatch_return = DISPATCH_RESPONSE;
        goto done;
      } else {
        /* Received an out-of-order response.  Abort. */
        NaClSrpcLog(NACL_SRPC_LOG_ERROR,
                    "NaClSrpcReceiveAndDispatch(channel=%p):"
                    " response for wrong request\n",
                    (void*) channel);
        dispatch_return = DISPATCH_BREAK;
        goto done;
      }
    }
  }
  bytes_read = RecvRequest(channel->message_channel, &rpc, args, rets);
  if (bytes_read < 0) {
    NaClSrpcLog(NACL_SRPC_LOG_ERROR,
                "NaClSrpcReceiveAndDispatch(channel=%p):"
                "RecvRequest failed, bytes=%"NACL_PRIdS"\n",
                (void*) channel,
                bytes_read);
    dispatch_return = DISPATCH_EOF;
  }
  if (!RequestVectorTypesConform(channel, &rpc, args, rets)) {
    NaClSrpcLog(NACL_SRPC_LOG_WARNING,
                "NaClSrpcReceiveAndDispatch(channel=%p):"
                " arg/ret type mismatch (recoverable error)\n",
                (void*) channel);
    dispatch_return = DISPATCH_CONTINUE;
    goto done;
  }
  /* Then we invoke the method, which computes a return code. */
  method = NaClSrpcServiceMethod(channel->server, rpc.rpc_number);
  if (NULL == method) {
    NaClSrpcLog(NACL_SRPC_LOG_WARNING,
                "NaClSrpcReceiveAndDispatch(channel=%p):"
                " bad rpc_number %"NACL_PRIu32" (recoverable error)\n",
                (void*) channel,
                rpc.rpc_number);
    dispatch_return = DISPATCH_CONTINUE;
    goto done;
  }
  do {
    const char* rpc_name;
    const char* arg_types;
    const char* ret_types;
    int i;
    NaClSrpcServiceMethodNameAndTypes(channel->server,
                                      rpc.rpc_number,
                                      &rpc_name,
                                      &arg_types,
                                      &ret_types);
    NaClSrpcLog(1, "NaClSrpcReceiveAndDispatch:"
                " request(channel=%p, rpc_number=%"NACL_PRIu32
                ", rpc_name=\"%s\")\n",
                (void*) channel,
                rpc.rpc_number,
                rpc_name);
    for (i = 0; args[i] != NULL; i++ ) {
      char buffer[256];
      NaClSrpcFormatArg(2, args[i], buffer, NACL_ARRAY_SIZE(buffer));
      NaClSrpcLog(2,
                  "NaClSrpcReceiveAndDispatch:"
                  " request(channel=%p, args[%d]=%s)\n",
                  (void*) channel,
                  i,
                  buffer);
    }
  } while(0);
  (*method)(&rpc, args, rets, (NaClSrpcClosure*) closure);
  FreeArgs(args);
  FreeArgs(rets);
  /* The invoked method takes ownership of the closure and deletes it. */
  closure = NULL;
  /*
   * Return code to either continue or break out of the processing loop.
   * When we separate closure invocation from the dispatch loop we will
   * have to implement a barrier to make sure that all preceding RPCs are
   * completed, and then signal the dispatcher to stop.
   */
  if (rpc.dispatch_loop_should_continue) {
    dispatch_return = DISPATCH_CONTINUE;
  } else {
    dispatch_return = DISPATCH_BREAK;
  }

 done:
  free(closure);
  return dispatch_return;
}