std::string SocketMultiplexer::DispatchCommand(int uid, const std::string& line) {
  std::istringstream iss(line);
  std::string command, arg;
  iss >> command >> std::ws;
  std::getline(iss, arg);

  if (command.empty()) {
    return "";
  }
  if (command == "QUIT") {
    // FIXME: should check uid? (allow only process owner?)
    return Shutdown();
  }
  if (command == "ADD") {
    return AddSocket(uid, arg);
  }
  if (command == "DELETE") {
    return DeleteSocket(uid, arg);
  }
  if (command == "LIST") {
    return ListSocket(uid);
  }
  LOG(ERROR) << "Unknwon command '" << command << "(" << arg << ")'";
  return "Unknwon command " + command + "\n";
}
void NewClient(SOCKET ClientSocket)
{
    AddActiveSocket(&ClientSocket);
    int iResult;
    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;
    static int thcount = 1;
    int thisthread = thcount;
    thcount++;
    printf("thread %d started\n", thisthread);

    // Receive until the peer shuts down the connection
    while (1)
    {
        iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
        if (iResult > 0)
        {
            printf("Bytes received: %d\n", iResult);

            if (iResult < DEFAULT_BUFLEN)
                recvbuf[iResult] = '\0';
            else
                recvbuf[DEFAULT_BUFLEN - 1] = '\0';

            SendAll(recvbuf);
        }
        else if (iResult == 0)
        {
            printf("Connection closing... th=%d\n", thisthread);
            DeleteSocket(&ClientSocket);
            closesocket(ClientSocket);
            return;
        }
        else
        {
            printf("recv failed with error: %d\n", WSAGetLastError());
            DeleteSocket(&ClientSocket);
            closesocket(ClientSocket);
            return;
        }
    }

    DeleteSocket(&ClientSocket);
    printf("thread completed\n");
}
Exemple #3
0
void Network::CloseConnection(MessageConnection *connection)
{
	LOG(LogVerbose, "Network::CloseConnection: Closing down connection %p.", connection);
	if (!connection)
		return;

	RemoveConnectionFromItsWorkerThread(connection);
	DeleteSocket(connection->socket);
	connection->socket = 0;
	connection->owner = 0;
	connection->ownerServer = 0;
	connections.erase(connection);
}