File: stream_packet_socket.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,150 bytes parent folder | download | duplicates (5)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_STREAM_PACKET_SOCKET_H_
#define REMOTING_PROTOCOL_STREAM_PACKET_SOCKET_H_

#include <memory>

#include "base/containers/circular_deque.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/webrtc/api/packet_socket_factory.h"
#include "third_party/webrtc/rtc_base/async_packet_socket.h"

namespace net {

class DrainableIOBuffer;
class GrowableIOBuffer;
class StreamSocket;

}  // namespace net

namespace remoting::protocol {

class StreamPacketProcessor;

// An AsyncPacketSocket implementation that runs on top of a StreamSocket. It is
// usually used for TCP connections.
// TODO(yuweih): Write unittest
class StreamPacketSocket final : public webrtc::AsyncPacketSocket {
 public:
  StreamPacketSocket();
  ~StreamPacketSocket() override;

  StreamPacketSocket(const StreamPacketSocket&) = delete;
  StreamPacketSocket& operator=(const StreamPacketSocket&) = delete;

  // Initializes the packet socket with the underlying stream socket and packet
  // processor. Returns true if the initialization succeeds.
  bool Init(std::unique_ptr<net::StreamSocket> socket,
            StreamPacketProcessor* packet_processor);

  // Initializes the packet socket for client TCP connection. Returns true if
  // the initialization succeeds.
  bool InitClientTcp(const webrtc::SocketAddress& local_address,
                     const webrtc::SocketAddress& remote_address,
                     const webrtc::PacketSocketTcpOptions& tcp_options);

  // webrtc::AsyncPacketSocket interface.
  webrtc::SocketAddress GetLocalAddress() const override;
  webrtc::SocketAddress GetRemoteAddress() const override;
  int Send(const void* data,
           size_t data_size,
           const webrtc::AsyncSocketPacketOptions& options) override;
  int SendTo(const void* data,
             size_t data_size,
             const webrtc::SocketAddress& address,
             const webrtc::AsyncSocketPacketOptions& options) override;
  int Close() override;
  State GetState() const override;
  int GetOption(webrtc::Socket::Option option, int* value) override;
  int SetOption(webrtc::Socket::Option option, int value) override;
  int GetError() const override;
  void SetError(int error) override;

 private:
  struct PendingPacket {
    PendingPacket(scoped_refptr<net::DrainableIOBuffer> data,
                  webrtc::AsyncSocketPacketOptions options);
    PendingPacket(const PendingPacket&);
    PendingPacket(PendingPacket&&);
    ~PendingPacket();

    scoped_refptr<net::DrainableIOBuffer> data;
    webrtc::AsyncSocketPacketOptions options;
  };

  void OnConnectCompleted(int result);

  void DoWrite();
  bool HandleWriteResult(int result);
  void OnAsyncWriteCompleted(int result);

  void DoRead();
  bool HandleReadResult(int result);
  void OnAsyncReadCompleted(int result);

  // Translates net_error to errno and set it on error_, then closes the socket
  // and reports the error.
  // If you only need to set the error code (in errno), call SetError instead.
  void CloseWithNetError(int net_error);

  std::unique_ptr<net::StreamSocket> socket_;
  raw_ptr<StreamPacketProcessor> packet_processor_;

  // Note that a packet can be partially sent, where the number of bytes sent
  // is reflected in DrainableIOBuffer::BytesConsumed.
  base::circular_deque<PendingPacket> send_queue_;

  bool send_pending_ = false;

  // The offset denotes the position for new data to arrive, whereas data
  // between [0, offset) are arrived but not processed.
  scoped_refptr<net::GrowableIOBuffer> read_buffer_;

  State state_ = STATE_CLOSED;

  // This is errno, not the net error code, which is what
  // webrtc::AsyncPacketSocket accepts. Unlike //net classes,
  // webrtc::AsyncPacketSocket methods don't return the error code. For error,
  // they generally return -1 and expect the caller to call GetError() to know
  // the reason.
  int error_ = 0;
};

}  // namespace remoting::protocol

#endif  // REMOTING_PROTOCOL_STREAM_PACKET_SOCKET_H_