tcl_socket.cc 2.33 KB
Newer Older
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*!
 * \file tcl_socket.cc
 */
#include <string>

#include "tcl_socket.h"

namespace tvm {
namespace runtime {

TclSocket::TclSocket() {
  tcp_socket_.Create();
  tcp_socket_.SetKeepAlive(true);
  reply_buf_.reserve(kReplyBufSize);
}

TclSocket::~TclSocket() {
  tcp_socket_.Close();
}

40
void TclSocket::Connect(tvm::support::SockAddr addr) {
41 42 43 44
  CHECK(tcp_socket_.Connect(addr)) << "failed to connect";
}

void TclSocket::SendCommand() {
45 46
  const char terminate_token = kCommandTerminateToken;
  cmd_builder_ << terminate_token;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  std::string full_cmd = cmd_builder_.str();
  CHECK(tcp_socket_.Send(full_cmd.data(), full_cmd.length()) != -1)
    << "failed to send command";
  cmd_builder_.str(std::string());

  reply_builder_.str(std::string());
  char last_read = '\0';
  // Receive from the socket until we reach a command terminator.
  do {
    ssize_t bytes_read;
    // Recieve from the socket until it's drained.
    do {
      // Leave room at the end of `reply_buf` to tack on a null terminator.
      bytes_read = tcp_socket_.Recv(reply_buf_.data(), kReplyBufSize - 1);
      reply_buf_[bytes_read] = '\0';
      reply_builder_ << reply_buf_.data();
      // Update last read character.
      last_read = reply_buf_[bytes_read - 1];
    } while (bytes_read == kReplyBufSize - 1);
    CHECK(bytes_read != -1) << "failed to read command reply";
67
  } while (last_read != terminate_token);
68
  last_reply_ = reply_builder_.str();
69
  CHECK_EQ(last_reply_[last_reply_.length()-1], terminate_token)
70 71 72 73 74
    << "missing command terminator";
}

}  // namespace runtime
}  // namespace tvm