• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors
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 //     https://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 
15 #ifndef TOOLS_SRC_CMD_REMOTE_COMPILE_SOCKET_H_
16 #define TOOLS_SRC_CMD_REMOTE_COMPILE_SOCKET_H_
17 
18 #include <atomic>
19 #include <memory>
20 
21 /// Socket provides an OS abstraction to a TCP socket.
22 class Socket {
23  public:
24   /// Connects to the given TCP address and port.
25   /// @param address the target socket address
26   /// @param port the target socket port
27   /// @param timeoutMillis the timeout for the connection attempt.
28   ///        If timeoutMillis is non-zero and no connection was made before
29   ///        timeoutMillis milliseconds, then nullptr is returned.
30   /// @returns the connected Socket, or nullptr on failure
31   static std::shared_ptr<Socket> Connect(const char* address,
32                                          const char* port,
33                                          uint32_t timeoutMillis);
34 
35   /// Begins listening for connections on the given TCP address and port.
36   /// Call Accept() on the returned Socket to block and wait for a connection.
37   /// @param address the socket address to listen on. Use "localhost" for
38   ///        connections from only this machine, or an empty string to allow
39   ///        connections from any incoming address.
40   /// @param port the socket port to listen on
41   /// @returns the Socket that listens for connections
42   static std::shared_ptr<Socket> Listen(const char* address, const char* port);
43 
44   /// Attempts to read at most `n` bytes into buffer, returning the actual
45   /// number of bytes read.
46   /// read() will block until the socket is closed or at least one byte is read.
47   /// @param buffer the output buffer. Must be at least `n` bytes in size.
48   /// @param n the maximum number of bytes to read
49   /// @return the number of bytes read, or 0 if the socket was closed
50   virtual size_t Read(void* buffer, size_t n) = 0;
51 
52   /// Writes `n` bytes from buffer into the socket.
53   /// @param buffer the source data buffer. Must be at least `n` bytes in size.
54   /// @param n the number of bytes to read from `buffer`
55   /// @returns true on success, or false if there was an error or the socket was
56   /// closed.
57   virtual bool Write(const void* buffer, size_t n) = 0;
58 
59   /// @returns true if the socket has not been closed.
60   virtual bool IsOpen() = 0;
61 
62   /// Closes the socket.
63   virtual void Close() = 0;
64 
65   /// Blocks for a connection to be made to the listening port, or for the
66   /// Socket to be closed.
67   /// @returns a pointer to the next established incoming connection
68   virtual std::shared_ptr<Socket> Accept() = 0;
69 };
70 
71 #endif  // TOOLS_SRC_CMD_REMOTE_COMPILE_SOCKET_H_
72