1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
16 #include <arpa/inet.h>
17 #include <netinet/in.h>
19 #include <sys/socket.h>
27 #include "pw_stream/stream.h"
29 namespace pw::stream {
31 static constexpr int kExitCode = -1;
32 static constexpr int kInvalidFd = -1;
34 class SocketStream : public Writer, public Reader {
36 explicit SocketStream() {}
38 // Listen to the port and return after a client is connected
39 Status Init(uint16_t port);
42 Status DoWrite(std::span<const std::byte> data) override;
44 StatusWithSize DoRead(ByteSpan dest) override;
46 uint16_t listen_port_ = 0;
47 int socket_fd_ = kInvalidFd;
48 int conn_fd_ = kInvalidFd;
49 struct sockaddr_in sockaddr_client_;
52 } // namespace pw::stream