Home | History | Annotate | Download | only in tcp_socket
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "common/libs/tcp_socket/tcp_socket.h"
     18 
     19 #include <netinet/in.h>
     20 #include <sys/socket.h>
     21 #include <sys/types.h>
     22 
     23 #include <cerrno>
     24 
     25 #include <glog/logging.h>
     26 
     27 using cvd::ClientSocket;
     28 using cvd::ServerSocket;
     29 
     30 ClientSocket::ClientSocket(int port)
     31   : fd_(SharedFD::SocketLocalClient(port, SOCK_STREAM)) {}
     32 
     33 cvd::Message ClientSocket::RecvAny(size_t length) {
     34   Message buf(length);
     35   auto read_count = fd_->Read(buf.data(), buf.size());
     36   if (read_count < 0) {
     37     read_count = 0;
     38   }
     39   buf.resize(read_count);
     40   return buf;
     41 }
     42 
     43 bool ClientSocket::closed() const {
     44   std::lock_guard<std::mutex> guard(closed_lock_);
     45   return other_side_closed_;
     46 }
     47 
     48 cvd::Message ClientSocket::Recv(size_t length) {
     49   Message buf(length);
     50   ssize_t total_read = 0;
     51   while (total_read < static_cast<ssize_t>(length)) {
     52     auto just_read = fd_->Read(&buf[total_read], buf.size() - total_read);
     53     if (just_read <= 0) {
     54       if (just_read < 0) {
     55         LOG(ERROR) << "read() error: " << strerror(errno);
     56       }
     57       {
     58         std::lock_guard<std::mutex> guard(closed_lock_);
     59         other_side_closed_ = true;
     60       }
     61       return Message{};
     62     }
     63     total_read += just_read;
     64   }
     65   CHECK(total_read == static_cast<ssize_t>(length));
     66   return buf;
     67 }
     68 
     69 ssize_t ClientSocket::Send(const uint8_t* data, std::size_t size) {
     70   std::lock_guard<std::mutex> lock(send_lock_);
     71   ssize_t written{};
     72   while (written < static_cast<ssize_t>(size)) {
     73     if (!fd_->IsOpen()) { LOG(ERROR) << "fd_ is closed"; }
     74     auto just_written = fd_->Write(data + written, size - written);
     75     if (just_written <= 0) {
     76       LOG(INFO) << "Couldn't write to client: " << strerror(errno);
     77       {
     78         std::lock_guard<std::mutex> guard(closed_lock_);
     79         other_side_closed_ = true;
     80       }
     81       return just_written;
     82     }
     83     written += just_written;
     84   }
     85   return written;
     86 }
     87 
     88 ssize_t ClientSocket::Send(const Message& message) {
     89   return Send(&message[0], message.size());
     90 }
     91 
     92 ServerSocket::ServerSocket(int port)
     93     : fd_{SharedFD::SocketLocalServer(port, SOCK_STREAM)} {
     94   if (!fd_->IsOpen()) {
     95     LOG(FATAL) << "Couldn't open streaming server on port " << port;
     96   }
     97 }
     98 
     99 ClientSocket ServerSocket::Accept() {
    100   SharedFD client = SharedFD::Accept(*fd_);
    101   if (!client->IsOpen()) {
    102     LOG(FATAL) << "Error attemping to accept: " << strerror(errno);
    103   }
    104   return ClientSocket{client};
    105 }
    106