Android-cuttlefish cvd tool
tcp_socket.h
Go to the documentation of this file.
1#pragma once
2
3/*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
20
21#include <unistd.h>
22
23#include <cstddef>
24#include <cstdint>
25#include <mutex>
26#include <string>
27#include <vector>
28
29namespace cuttlefish {
30using Message = std::vector<std::uint8_t>;
31
32// Recv and Send wait until all data has been received or sent.
33// Send is thread safe in this regard, Recv is not.
35 public:
36 ClientSocket(ClientSocket&& other) : fd_{other.fd_} {}
37
39 fd_ = other.fd_;
40 return *this;
41 }
42
43 ClientSocket(int port);
44
45 ClientSocket(const ClientSocket&) = delete;
47
48 Message Recv(std::size_t length);
49 // RecvAny will receive whatever is available.
50 // An empty message returned indicates error or close.
51 Message RecvAny(std::size_t length);
52 // Sends are called with MSG_NOSIGNAL to suppress SIGPIPE
53 ssize_t SendNoSignal(const std::uint8_t* data, std::size_t size);
54 ssize_t SendNoSignal(const Message& message);
55
56 template <std::size_t N>
57 ssize_t SendNoSignal(const std::uint8_t (&data)[N]) {
58 return SendNoSignal(data, N);
59 }
60
61 bool closed() const;
62
63 private:
64 friend class ServerSocket;
65 explicit ClientSocket(SharedFD fd) : fd_(fd) {}
66
69 mutable std::mutex closed_lock_;
70 std::mutex send_lock_;
71};
72
74 public:
75 explicit ServerSocket(int port);
76
77 ServerSocket(const ServerSocket&) = delete;
79
81
82 private:
84};
85
86void AppendInNetworkByteOrder(Message* msg, std::uint8_t b);
87void AppendInNetworkByteOrder(Message* msg, std::uint16_t s);
88void AppendInNetworkByteOrder(Message* msg, std::uint32_t w);
89void AppendInNetworkByteOrder(Message* msg, std::int32_t w);
90void AppendInNetworkByteOrder(Message* msg, const std::string& str);
91
92inline void AppendToMessage(Message*) {}
93
94template <typename T, typename... Ts>
95void AppendToMessage(Message* msg, T v, Ts... vals) {
97 AppendToMessage(msg, vals...);
98}
99
100template <typename... Ts>
102 Message m;
103 AppendToMessage(&m, vals...);
104 return m;
105}
106
107} // namespace cuttlefish
Definition: tcp_socket.h:34
ClientSocket(const ClientSocket &)=delete
Message Recv(std::size_t length)
Definition: tcp_socket.cpp:51
ClientSocket(SharedFD fd)
Definition: tcp_socket.h:65
bool other_side_closed_
Definition: tcp_socket.h:68
std::mutex closed_lock_
Definition: tcp_socket.h:69
Message RecvAny(std::size_t length)
Definition: tcp_socket.cpp:36
bool closed() const
Definition: tcp_socket.cpp:46
ClientSocket & operator=(const ClientSocket &)=delete
ClientSocket & operator=(ClientSocket &&other)
Definition: tcp_socket.h:38
ClientSocket(ClientSocket &&other)
Definition: tcp_socket.h:36
std::mutex send_lock_
Definition: tcp_socket.h:70
ssize_t SendNoSignal(const std::uint8_t(&data)[N])
Definition: tcp_socket.h:57
SharedFD fd_
Definition: tcp_socket.h:67
ssize_t SendNoSignal(const std::uint8_t *data, std::size_t size)
Definition: tcp_socket.h:73
ServerSocket(int port)
Definition: tcp_socket.cpp:97
ClientSocket Accept()
Definition: tcp_socket.cpp:104
ServerSocket(const ServerSocket &)=delete
ServerSocket & operator=(const ServerSocket &)=delete
SharedFD fd_
Definition: tcp_socket.h:83
Definition: shared_fd.h:129
char data[Size]
Definition: incremental_server.cpp:1
uint32_t size
Definition: io.h:2
Definition: alloc_utils.cpp:23
Message CreateMessage(Ts... vals)
Definition: tcp_socket.h:101
void AppendInNetworkByteOrder(Message *msg, const std::uint8_t b)
Definition: tcp_socket.cpp:112
void AppendToMessage(Message *)
Definition: tcp_socket.h:92
std::vector< std::uint8_t > Message
Definition: tcp_socket.h:30
constexpr const char * str
Definition: utils.h:180