summaryrefslogtreecommitdiffstats
path: root/Source/cmServerConnection.cxx
blob: 44af75f8259a5f4ada2dbe4246b402318cfa767a (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmServerConnection.h"

#include "cmConfigure.h"
#include "cmServer.h"
#include "cmServerDictionary.h"
#ifdef _WIN32
#include "io.h"
#else
#include <unistd.h>
#endif
#include <cassert>

cmStdIoConnection::cmStdIoConnection(
  cmConnectionBufferStrategy* bufferStrategy)
  : cmEventBasedConnection(bufferStrategy)
{
}

void cmStdIoConnection::SetupStream(uv_stream_t*& stream, int file_id)
{
  assert(stream == nullptr);
  switch (uv_guess_handle(file_id)) {
    case UV_TTY: {
      auto tty = new uv_tty_t();
      uv_tty_init(this->Server->GetLoop(), tty, file_id, file_id == 0);
      uv_tty_set_mode(tty, UV_TTY_MODE_NORMAL);
      stream = reinterpret_cast<uv_stream_t*>(tty);
      break;
    }
    case UV_FILE:
      if (file_id == 0) {
        return;
      }
      // Intentional fallthrough; stdin can _not_ be treated as a named
      // pipe, however stdout can be.
      CM_FALLTHROUGH;
    case UV_NAMED_PIPE: {
      auto pipe = new uv_pipe_t();
      uv_pipe_init(this->Server->GetLoop(), pipe, 0);
      uv_pipe_open(pipe, file_id);
      stream = reinterpret_cast<uv_stream_t*>(pipe);
      break;
    }
    default:
      assert(false && "Unable to determine stream type");
      return;
  }
  stream->data = static_cast<cmEventBasedConnection*>(this);
}

void cmStdIoConnection::SetServer(cmServerBase* s)
{
  cmConnection::SetServer(s);
  if (!s) {
    return;
  }

  SetupStream(this->ReadStream, 0);
  SetupStream(this->WriteStream, 1);
}

void shutdown_connection(uv_prepare_t* prepare)
{
  cmStdIoConnection* connection =
    reinterpret_cast<cmStdIoConnection*>(prepare->data);

  if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(prepare))) {
    uv_close(reinterpret_cast<uv_handle_t*>(prepare),
             &cmEventBasedConnection::on_close_delete<uv_prepare_t>);
  }
  connection->OnDisconnect(0);
}

bool cmStdIoConnection::OnServeStart(std::string* pString)
{
  Server->OnConnected(this);
  if (this->ReadStream) {
    uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
  } else if (uv_guess_handle(0) == UV_FILE) {
    char buffer[1024];
    while (auto len = read(0, buffer, sizeof(buffer))) {
      ReadData(std::string(buffer, buffer + len));
    }

    // We can't start the disconnect from here, add a prepare hook to do that
    // for us
    auto prepare = new uv_prepare_t();
    prepare->data = this;
    uv_prepare_init(Server->GetLoop(), prepare);
    uv_prepare_start(prepare, shutdown_connection);
  }
  return cmConnection::OnServeStart(pString);
}

void cmStdIoConnection::ShutdownStream(uv_stream_t*& stream)
{
  if (!stream) {
    return;
  }
  switch (stream->type) {
    case UV_TTY: {
      assert(!uv_is_closing(reinterpret_cast<uv_handle_t*>(stream)));
      if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(stream))) {
        uv_close(reinterpret_cast<uv_handle_t*>(stream),
                 &on_close_delete<uv_tty_t>);
      }
      break;
    }
    case UV_FILE:
    case UV_NAMED_PIPE: {
      assert(!uv_is_closing(reinterpret_cast<uv_handle_t*>(stream)));
      if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(stream))) {
        uv_close(reinterpret_cast<uv_handle_t*>(stream),
                 &on_close_delete<uv_pipe_t>);
      }
      break;
    }
    default:
      assert(false && "Unable to determine stream type");
  }

  stream = nullptr;
}

bool cmStdIoConnection::OnConnectionShuttingDown()
{
  if (ReadStream) {
    uv_read_stop(ReadStream);
  }

  ShutdownStream(ReadStream);
  ShutdownStream(WriteStream);

  cmEventBasedConnection::OnConnectionShuttingDown();

  return true;
}

cmServerPipeConnection::cmServerPipeConnection(const std::string& name)
  : cmPipeConnection(name, new cmServerBufferStrategy)
{
}

cmServerStdIoConnection::cmServerStdIoConnection()
  : cmStdIoConnection(new cmServerBufferStrategy)
{
}

cmConnectionBufferStrategy::~cmConnectionBufferStrategy()
{
}

void cmConnectionBufferStrategy::clear()
{
}

std::string cmServerBufferStrategy::BufferOutMessage(
  const std::string& rawBuffer) const
{
  return std::string("\n") + kSTART_MAGIC + std::string("\n") + rawBuffer +
    kEND_MAGIC + std::string("\n");
}

std::string cmServerBufferStrategy::BufferMessage(std::string& RawReadBuffer)
{
  for (;;) {
    auto needle = RawReadBuffer.find('\n');

    if (needle == std::string::npos) {
      return "";
    }
    std::string line = RawReadBuffer.substr(0, needle);
    const auto ls = line.size();
    if (ls > 1 && line.at(ls - 1) == '\r') {
      line.erase(ls - 1, 1);
    }
    RawReadBuffer.erase(RawReadBuffer.begin(),
                        RawReadBuffer.begin() + static_cast<long>(needle) + 1);
    if (line == kSTART_MAGIC) {
      RequestBuffer.clear();
      continue;
    }
    if (line == kEND_MAGIC) {
      std::string rtn;
      rtn.swap(this->RequestBuffer);
      return rtn;
    }

    this->RequestBuffer += line;
    this->RequestBuffer += "\n";
  }
}