1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <sys/types.h>
17 
18 #include <cstdint>
19 #include <functional>
20 #include <memory>
21 
22 #ifdef _WIN32
23 #include <BaseTsd.h>
24 typedef SSIZE_T ssize_t;
25 #else
26 #include <unistd.h>
27 #endif
28 
29 namespace android {
30 namespace net {
31 
32 class AsyncDataChannel;
33 
34 // Callback function that will be used to notify that new data
35 // can be read.
36 using ReadCallback = std::function<void(AsyncDataChannel*)>;
37 
38 // A connected asynchronous socket abstraction.
39 //
40 // This is really a simple data channel that can be used to read and write
41 // data. Async Sockets are usually non-blocking posix/win sockets, but could be
42 // other types of datachannels (gRPC, qemu pipe)
43 class AsyncDataChannel {
44  public:
45   virtual ~AsyncDataChannel() = default;
46 
47   // Receive data in the given buffer. Properly handling EINTR where
48   // applicable.
49   //
50   // Returns:
51   // - >0: The number of bytes read.
52   // -  0: The socket is closed, no further reads/write are possible
53   // - <0: An error occurred. Details can be found in errno:
54   //    -  EAGAIN: No data, try again later.
55   //
56   // Implementors should take care of translating EWOULDBLOCK into EAGAIN
57   // if needed.
58   virtual ssize_t Recv(uint8_t* buffer, uint64_t bufferSize) = 0;
59 
60   // Send data in the given buffer. Properly handling EINTR, EPIPE where
61   // applicable.
62   //
63   // Returns:
64   // - >0: The number of bytes written, this can be < bufferSize.
65   // - <0: An error occurred. Details can be found in errno:
66   //    - EAGAIN: The write would block, try again later.
67   //    - EBADF: The connection is closed.
68   //
69   // Implementors should take care of translating EWOULDBLOCK into EAGAIN
70   // if needed.
71   virtual ssize_t Send(const uint8_t* buffer, uint64_t bufferSize) = 0;
72 
73   // True if this socket is connected
74   virtual bool Connected() = 0;
75 
76   // Closes this socket. Upon return the following will hold:
77   //
78   // - No more ReadCallbacks will be invoked.
79   // - Send/Recv calls will return 0.
80   virtual void Close() = 0;
81 
82   // Registers the given callback to be invoked when a recv call can be made
83   // to read data from this socket. The expectation is that a call to Recv will
84   // not return EAGAIN. Returns false if registration of the watcher failed.
85   //
86   // Only one callback can be registered per socket.
87   virtual bool WatchForNonBlockingRead(
88       const ReadCallback& on_read_ready_callback) = 0;
89 
90   // Stops watching this socket, you will not receive any callbacks any longer.
91   virtual void StopWatching() = 0;
92 };
93 
94 }  // namespace net
95 }  // namespace android
96