Esempio n. 1
0
int TcpClient::run(Request& req, std::ostream& out) {
  int returnCode = -1;
  try {
    std::ostringstream request_stream;
    write_request(req, request_stream);
    this->send(request_stream.str());
    this->receive(rcvBuffer_);

    std::stringstream response_stream;
    response_stream.write((char*)rcvBuffer_.data(), rcvBuffer_.size());
    string line;
    while (response_stream) {
      std::getline(response_stream, line);
      if(endsWith(line,"__JANOSH_EOF")) {
        out << line.substr(0, line.size() - string("__JANOSH_EOF").size());
        std::getline(response_stream, line);
        returnCode = std::stoi(line);

        if (returnCode == 0) {
          LOG_DEBUG_STR("Successful");
        } else {
          LOG_INFO_MSG("Failed", returnCode);
        }

        break;
      }
      out << line << '\n';
    }
  } catch (std::exception& ex) {
    LOG_ERR_MSG("Caught in tcp_client run", ex.what());
  }
  return returnCode;
}
Esempio n. 2
0
int main(int argc, char *argv[]) {
    if (!parseAppArgs(argc, argv)) return 1;

    ProactorService service(g_pollerType, g_blocking);

    ProactorFile *input = service.attachFd(::dup(STDIN_FILENO));
    ON_EXIT_SCOPE([input](){ input->destroy(); });
    bool shouldExit = false;
    input->readLine('\n', [&shouldExit](bool eof, const char *buf, int n){ shouldExit = true; });

    ProactorFile *listenSocket = service.createListenSocket(HostAddress::fromLocal(g_port), 32);
    ON_EXIT_SCOPE([listenSocket](){ listenSocket->destroy(); });
    function<void(ProactorFile*,const HostAddress&)> onAccept =
    [listenSocket, &onAccept](ProactorFile *socket, const HostAddress &addr) {
        Connection *c = new Connection(socket, addr);
        socket->setDestroyCallback([c](){ delete c; });

        listenSocket->accept(onAccept);
    };
    listenSocket->accept(onAccept);

    LOG_ERR("Listen on port %d", g_port);
    LOG_ERR_MSG("Press any key to exit...");

    while (!shouldExit) {
        try {
            service.wait(300);
        } catch(const RuntimeException &e) {
            LOG_ERR("Found runtime exception %s", e.what());
        }
    }
}
Esempio n. 3
0
HRESULT Worker_RegisterClient(ISimENG* pISimENG, Base_WrapperErrorLogger* pIlog)
{ 
    USHORT ushClientID = 0;     // Client ID issued from the simulation engine
    BSTR PipeName;              // Name of the conduit
    BSTR EventName;             // Name of the message notifying event
    HRESULT hResult = S_OK;

    // Register to the simulation engine
    hResult = pISimENG->RegisterClient(CAN, sizeof (STCAN_MSG), &ushClientID, &PipeName, &EventName);
    if (hResult != S_OK)
    {
        pIlog->vLogAMessage(__FILE__, __LINE__, 
                    ("unable to register to the simulation engine"));
        return S_FALSE;
    }

    sg_ushTempClientID = ushClientID;

    // Extract the pipe and event name in normal string
    char acPipeName[64] = {'\0'};
    char acEventName[32] = {'\0'};
    if (BSTR_2_PCHAR(PipeName, acPipeName, 64) == false)
    {
        hResult = S_FALSE;
    }

    if (BSTR_2_PCHAR(EventName, acEventName, 32) == false)
    {
        hResult = S_FALSE;
    }

    if (S_FALSE == hResult)
    {
        // Unregister from the simulation engine
        pISimENG->UnregisterClient(ushClientID);
        pIlog->vLogAMessage(__FILE__, __LINE__,
                    ("Can't convert from BSTR to ASCII string"));
        return S_FALSE;
    }

    // Get the pipe handle
    sg_hTmpPipeHandle = CreateFile(
        acPipeName,     // pipe name 
        GENERIC_READ,   // read access 
        0,              // no sharing 
        NULL,           // no security attributes
        OPEN_EXISTING,  // opens existing pipe 
        0,              // default attributes 
        NULL);          // no template file 

    if (sg_hTmpPipeHandle == INVALID_HANDLE_VALUE) 
    {
        // Unregister from the simulation engine
        pISimENG->UnregisterClient(ushClientID);
        GetSystemErrorString();
        LOG_ERR_MSG();
        return S_FALSE;
    }

    sg_hTmpClientHandle = OpenEvent(EVENT_ALL_ACCESS, FALSE, acEventName);
    if (sg_hTmpClientHandle == NULL)
    {
        CloseHandle(sg_hTmpPipeHandle);
        sg_hTmpPipeHandle = NULL;
        // Unregister from the simulation engine
        pISimENG->UnregisterClient(ushClientID);
        GetSystemErrorString();
        LOG_ERR_MSG();
        return S_FALSE;
    }
    return hResult;
}