summaryrefslogtreecommitdiff
path: root/tools/rootcanal/net/posix/posix_async_socket.h
blob: 0dbc8789da8a71ea58f6e90f31026902e234a2aa (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
// Copyright (C) 2021 The Android Open Source Project
//
// Licensed 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.
#pragma once
#include <stdint.h>     // for uint64_t, uint8_t
#include <sys/types.h>  // for ssize_t

#include <atomic>  // for atomic_bool

#include "net/async_data_channel.h"  // for AsyncDataChannel, ReadCallback

namespace rootcanal {
class AsyncManager;
}  // namespace rootcanal

namespace android {
namespace net {

using rootcanal::AsyncManager;

// A Posix compliant implementation of the AsyncDataChannel interface.
//
// Supports both Darwin and Linux.
class PosixAsyncSocket : public AsyncDataChannel {
 public:
  // The AsyncManager must support the following:
  //
  // - If a callback happens on thread t, and
  //   am->StopWatchingFileDescriptor(fd)
  //   is called from t then am will never fire an event for fd upon return.
  PosixAsyncSocket(int fd, AsyncManager* am);
  PosixAsyncSocket(const PosixAsyncSocket& other) = delete;
  PosixAsyncSocket(PosixAsyncSocket&& other);

  // Make sure to close the socket before hand.
  ~PosixAsyncSocket();

  // Receive data in the given buffer. Returns the number of bytes read,
  // or a negative number in case of failure. Check the errno variable to
  // learn why the call failed.
  ssize_t Recv(uint8_t* buffer, uint64_t bufferSize) override;

  // Send data in the given buffer. Returns the number of bytes read,
  // or a negative number in case of failure. Check the errno variable to
  // learn why the call failed. Note: This can be EAGAIN if we cannot
  // write to the socket at this time as it would block.
  ssize_t Send(const uint8_t* buffer, uint64_t bufferSize) override;

  // True if this socket is connected
  bool Connected() override;

  // Closes this socket. You must call this before deleting the socket.
  //
  // - On Linux, the socket is always closed when this function returns
  // - On OS X, whether the socket the underlying fd becomes available seems
  //   pretty much random! (You might want to drain the socket first.)
  void Close() override;

  // Registers the given callback to be invoked when a recv call can be made
  // to read data from this socket.
  // Only one callback can be registered per socket.
  bool WatchForNonBlockingRead(
      const ReadCallback& on_read_ready_callback) override;

  void StopWatching() override;

  int fd() { return fd_; }

 private:
  void OnReadCallback();

  int fd_;
  AsyncManager* am_;
  std::atomic_bool watching_;
};
}  // namespace net
}  // namespace android