1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PLATFORM_IMPL_UDP_SOCKET_READER_POSIX_H_
6 #define PLATFORM_IMPL_UDP_SOCKET_READER_POSIX_H_
7 
8 #include <map>
9 #include <mutex>
10 #include <vector>
11 
12 #include "platform/api/task_runner.h"
13 #include "platform/api/time.h"
14 #include "platform/impl/socket_handle.h"
15 #include "platform/impl/socket_handle_waiter.h"
16 #include "platform/impl/udp_socket_posix.h"
17 
18 namespace openscreen {
19 
20 // This is the class responsible for watching sockets for readable data, then
21 // calling the function associated with these sockets once that data is read.
22 // NOTE: This class will only function as intended while its RunUntilStopped
23 // method is running.
24 class UdpSocketReaderPosix : public SocketHandleWaiter::Subscriber {
25  public:
26   using SocketHandleRef = SocketHandleWaiter::SocketHandleRef;
27 
28   // Creates a new instance of this object.
29   // NOTE: The provided NetworkWaiter must outlive this object.
30   explicit UdpSocketReaderPosix(SocketHandleWaiter* waiter);
31   ~UdpSocketReaderPosix() override;
32 
33   // Waits for |socket| to be readable and then calls the socket's
34   // RecieveMessage(...) method to process the available packet.
35   // NOTE: The first read on any newly watched socket may be delayed up to 50
36   // ms.
37   void OnCreate(UdpSocket* socket);
38 
39   // Cancels any pending wait on reading |socket|. Following this call, any
40   // pending reads will proceed but their associated callbacks will not fire.
41   // NOTE: This method will block until a delete is safe.
42   // NOTE: If a socket callback is removed in the middle of a wait call, data
43   // may be read on this socket and but the callback may not be called. If a
44   // socket callback is added in the middle of a wait call, the new socket may
45   // not be watched until after this wait call ends.
46   virtual void OnDestroy(UdpSocket* socket);
47 
48   // SocketHandleWaiter::Subscriber overrides.
49   void ProcessReadyHandle(SocketHandleRef handle, uint32_t flags) override;
50 
51   OSP_DISALLOW_COPY_AND_ASSIGN(UdpSocketReaderPosix);
52 
53  protected:
54   bool IsMappedReadForTesting(UdpSocketPosix* socket) const;
55 
56  private:
57   // Helper method to allow for OnDestroy calls without blocking.
58   void OnDelete(UdpSocketPosix* socket,
59                 bool disable_locking_for_testing = false);
60 
61   // The set of all sockets that are being read from
62   // TODO(rwkeane): Change to std::vector<UdpSocketPosix*>
63   std::vector<UdpSocketPosix*> sockets_;
64 
65   // Mutex to protect against concurrent modification of socket info.
66   std::mutex mutex_;
67 
68   // NetworkWaiter watching this NetworkReader.
69   SocketHandleWaiter* const waiter_;
70 
71   friend class TestingUdpSocketReader;
72 };
73 
74 }  // namespace openscreen
75 
76 #endif  // PLATFORM_IMPL_UDP_SOCKET_READER_POSIX_H_
77