1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include "Channel.hpp"
#include "LSP/Message.h"
#include "file_logger.h"
#include "macros.h"
#include <array>
#include <iostream>
#include <sstream>
#include <string>
ChannelSocket::ChannelSocket(const wxString& ip, int port)
: m_ip(ip)
, m_port(port)
{
}
ChannelSocket::~ChannelSocket() {}
eReadSome ChannelSocket::read_some()
{
wxString buffer;
client->SelectRead();
switch(client->Read(buffer)) {
case clSocketBase::kSuccess:
m_buffer.Append(buffer);
return eReadSome::kSuccess;
case clSocketBase::kTimeout:
return eReadSome::kTimeout;
default:
case clSocketBase::kError:
clERROR() << "Socket read error. " << client->error() << endl;
return eReadSome::kError;
}
}
bool ChannelSocket::write_reply(const JSONItem& message) { return write_reply(message.format(false)); }
bool ChannelSocket::write_reply(const JSON& message) { return write_reply(message.toElement().format(false)); }
bool ChannelSocket::write_reply(const wxString& message)
{
auto cb = message.mb_str(wxConvUTF8);
// Build the request header
std::stringstream ss;
std::string s;
ss << "Content-Length: " << cb.length() << "\r\n";
ss << "\r\n";
s = ss.str();
// append the data
s.append(cb.data(), cb.length());
LOG_IF_TRACE { clDEBUG1() << "Sending reply:" << s << endl; }
client->Send(s);
return true;
}
std::unique_ptr<JSON> ChannelSocket::read_message()
{
while(true) {
auto msg = LSP::Message::GetJSONPayload(m_buffer);
if(msg) {
return msg;
}
// attempt to read some more data from stdin
bool cont = true;
while(cont) {
switch(read_some()) {
case eReadSome::kError:
return nullptr;
case eReadSome::kSuccess:
cont = false;
break;
case eReadSome::kTimeout:
break;
}
}
}
}
void ChannelSocket::open()
{
clSocketServer server;
wxString connection_string;
connection_string << "tcp://" << m_ip << ":" << m_port;
server.Start(connection_string);
clSYSTEM() << "Accepting connection on:" << connection_string << endl;
client = server.WaitForNewConnection();
if(!client) {
clERROR() << "failed to accept new connection. Error code:" << server.GetLastError() << endl;
exit(1);
}
clSYSTEM() << "Connection established successfully" << endl;
}
|