File: udp_socket_posix.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (91 lines) | stat: -rw-r--r-- 3,255 bytes parent folder | download | duplicates (11)
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
88
89
90
91
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PLATFORM_IMPL_UDP_SOCKET_POSIX_H_
#define PLATFORM_IMPL_UDP_SOCKET_POSIX_H_

#include "absl/types/optional.h"
#include "platform/api/udp_socket.h"
#include "platform/base/macros.h"
#include "platform/impl/platform_client_posix.h"
#include "platform/impl/socket_handle_posix.h"
#include "util/weak_ptr.h"

namespace openscreen {

class UdpSocketReaderPosix;

// Threading: All public methods must be called on the same thread--the one
// executing the TaskRunner. All non-public methods, except ReceiveMessage(),
// are also assumed to be called on that thread.
class UdpSocketPosix : public UdpSocket {
 public:
  // Creates a new UdpSocketPosix. The provided client and task_runner must
  // exist for the duration of this socket's lifetime.
  UdpSocketPosix(TaskRunner* task_runner,
                 Client* client,
                 SocketHandle handle,
                 const IPEndpoint& local_endpoint,
                 PlatformClientPosix* platform_client =
                     PlatformClientPosix::GetInstance());

  ~UdpSocketPosix() override;

  // Implementations of UdpSocket methods.
  bool IsIPv4() const override;
  bool IsIPv6() const override;
  IPEndpoint GetLocalEndpoint() const override;
  void Bind() override;
  void SetMulticastOutboundInterface(NetworkInterfaceIndex ifindex) override;
  void JoinMulticastGroup(const IPAddress& address,
                          NetworkInterfaceIndex ifindex) override;
  void SendMessage(const void* data,
                   size_t length,
                   const IPEndpoint& dest) override;
  void SetDscp(DscpMode state) override;

  const SocketHandle& GetHandle() const;

 protected:
  friend class UdpSocketReaderPosix;

  // Called by UdpSocketReaderPosix to perform a non-blocking read on the socket
  // and then dispatch the packet to this socket's Client. This method is the
  // only one in this class possibly being called from another thread.
  void ReceiveMessage();

 private:
  // Helper to close the socket if |error| is fatal, in addition to dispatching
  // an Error to the |client_|.
  void OnError(Error::Code error);

  bool is_closed() const { return handle_.fd < 0; }
  void Close();

  // Task runner to use for queuing |client_| callbacks.
  TaskRunner* const task_runner_;

  // Client to use for callbacks. This can be nullptr if the user does not want
  // any callbacks (for example, in the send-only case).
  Client* const client_;

  // Holds the POSIX file descriptor, or -1 if the socket is closed.
  SocketHandle handle_;

  // Cached value of current local endpoint. This can change (e.g., when the
  // operating system auto-assigns a free local port when Bind() is called). If
  // the port is zero, getsockname() is called to try to resolve it. Once the
  // port is non-zero, it is assumed never to change again.
  mutable IPEndpoint local_endpoint_;

  WeakPtrFactory<UdpSocketPosix> weak_factory_{this};

  PlatformClientPosix* const platform_client_;

  OSP_DISALLOW_COPY_AND_ASSIGN(UdpSocketPosix);
};

}  // namespace openscreen

#endif  // PLATFORM_IMPL_UDP_SOCKET_POSIX_H_