| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 ////////////////////////////////////////////////////////////////////// | 23 ////////////////////////////////////////////////////////////////////// |
| 24 | 24 |
| 25 class PseudoTcp; | 25 class PseudoTcp; |
| 26 | 26 |
| 27 class IPseudoTcpNotify { | 27 class IPseudoTcpNotify { |
| 28 public: | 28 public: |
| 29 // Notification of tcp events | 29 // Notification of tcp events |
| 30 virtual void OnTcpOpen(PseudoTcp* tcp) = 0; | 30 virtual void OnTcpOpen(PseudoTcp* tcp) = 0; |
| 31 virtual void OnTcpReadable(PseudoTcp* tcp) = 0; | 31 virtual void OnTcpReadable(PseudoTcp* tcp) = 0; |
| 32 virtual void OnTcpWriteable(PseudoTcp* tcp) = 0; | 32 virtual void OnTcpWriteable(PseudoTcp* tcp) = 0; |
| 33 virtual void OnTcpClosed(PseudoTcp* tcp, uint32 error) = 0; | 33 virtual void OnTcpClosed(PseudoTcp* tcp, uint32_t error) = 0; |
| 34 | 34 |
| 35 // Write the packet onto the network | 35 // Write the packet onto the network |
| 36 enum WriteResult { WR_SUCCESS, WR_TOO_LARGE, WR_FAIL }; | 36 enum WriteResult { WR_SUCCESS, WR_TOO_LARGE, WR_FAIL }; |
| 37 virtual WriteResult TcpWritePacket(PseudoTcp* tcp, | 37 virtual WriteResult TcpWritePacket(PseudoTcp* tcp, |
| 38 const char* buffer, size_t len) = 0; | 38 const char* buffer, size_t len) = 0; |
| 39 | 39 |
| 40 protected: | 40 protected: |
| 41 virtual ~IPseudoTcpNotify() {} | 41 virtual ~IPseudoTcpNotify() {} |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 ////////////////////////////////////////////////////////////////////// | 44 ////////////////////////////////////////////////////////////////////// |
| 45 // PseudoTcp | 45 // PseudoTcp |
| 46 ////////////////////////////////////////////////////////////////////// | 46 ////////////////////////////////////////////////////////////////////// |
| 47 | 47 |
| 48 class PseudoTcp { | 48 class PseudoTcp { |
| 49 public: | 49 public: |
| 50 static uint32 Now(); | 50 static uint32_t Now(); |
| 51 | 51 |
| 52 PseudoTcp(IPseudoTcpNotify* notify, uint32 conv); | 52 PseudoTcp(IPseudoTcpNotify* notify, uint32_t conv); |
| 53 virtual ~PseudoTcp(); | 53 virtual ~PseudoTcp(); |
| 54 | 54 |
| 55 int Connect(); | 55 int Connect(); |
| 56 int Recv(char* buffer, size_t len); | 56 int Recv(char* buffer, size_t len); |
| 57 int Send(const char* buffer, size_t len); | 57 int Send(const char* buffer, size_t len); |
| 58 void Close(bool force); | 58 void Close(bool force); |
| 59 int GetError(); | 59 int GetError(); |
| 60 | 60 |
| 61 enum TcpState { | 61 enum TcpState { |
| 62 TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECEIVED, TCP_ESTABLISHED, TCP_CLOSED | 62 TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECEIVED, TCP_ESTABLISHED, TCP_CLOSED |
| 63 }; | 63 }; |
| 64 TcpState State() const { return m_state; } | 64 TcpState State() const { return m_state; } |
| 65 | 65 |
| 66 // Call this when the PMTU changes. | 66 // Call this when the PMTU changes. |
| 67 void NotifyMTU(uint16 mtu); | 67 void NotifyMTU(uint16_t mtu); |
| 68 | 68 |
| 69 // Call this based on timeout value returned from GetNextClock. | 69 // Call this based on timeout value returned from GetNextClock. |
| 70 // It's ok to call this too frequently. | 70 // It's ok to call this too frequently. |
| 71 void NotifyClock(uint32 now); | 71 void NotifyClock(uint32_t now); |
| 72 | 72 |
| 73 // Call this whenever a packet arrives. | 73 // Call this whenever a packet arrives. |
| 74 // Returns true if the packet was processed successfully. | 74 // Returns true if the packet was processed successfully. |
| 75 bool NotifyPacket(const char * buffer, size_t len); | 75 bool NotifyPacket(const char * buffer, size_t len); |
| 76 | 76 |
| 77 // Call this to determine the next time NotifyClock should be called. | 77 // Call this to determine the next time NotifyClock should be called. |
| 78 // Returns false if the socket is ready to be destroyed. | 78 // Returns false if the socket is ready to be destroyed. |
| 79 bool GetNextClock(uint32 now, long& timeout); | 79 bool GetNextClock(uint32_t now, long& timeout); |
| 80 | 80 |
| 81 // Call these to get/set option values to tailor this PseudoTcp | 81 // Call these to get/set option values to tailor this PseudoTcp |
| 82 // instance's behaviour for the kind of data it will carry. | 82 // instance's behaviour for the kind of data it will carry. |
| 83 // If an unrecognized option is set or got, an assertion will fire. | 83 // If an unrecognized option is set or got, an assertion will fire. |
| 84 // | 84 // |
| 85 // Setting options for OPT_RCVBUF or OPT_SNDBUF after Connect() is called | 85 // Setting options for OPT_RCVBUF or OPT_SNDBUF after Connect() is called |
| 86 // will result in an assertion. | 86 // will result in an assertion. |
| 87 enum Option { | 87 enum Option { |
| 88 OPT_NODELAY, // Whether to enable Nagle's algorithm (0 == off) | 88 OPT_NODELAY, // Whether to enable Nagle's algorithm (0 == off) |
| 89 OPT_ACKDELAY, // The Delayed ACK timeout (0 == off). | 89 OPT_ACKDELAY, // The Delayed ACK timeout (0 == off). |
| 90 OPT_RCVBUF, // Set the receive buffer size, in bytes. | 90 OPT_RCVBUF, // Set the receive buffer size, in bytes. |
| 91 OPT_SNDBUF, // Set the send buffer size, in bytes. | 91 OPT_SNDBUF, // Set the send buffer size, in bytes. |
| 92 }; | 92 }; |
| 93 void GetOption(Option opt, int* value); | 93 void GetOption(Option opt, int* value); |
| 94 void SetOption(Option opt, int value); | 94 void SetOption(Option opt, int value); |
| 95 | 95 |
| 96 // Returns current congestion window in bytes. | 96 // Returns current congestion window in bytes. |
| 97 uint32 GetCongestionWindow() const; | 97 uint32_t GetCongestionWindow() const; |
| 98 | 98 |
| 99 // Returns amount of data in bytes that has been sent, but haven't | 99 // Returns amount of data in bytes that has been sent, but haven't |
| 100 // been acknowledged. | 100 // been acknowledged. |
| 101 uint32 GetBytesInFlight() const; | 101 uint32_t GetBytesInFlight() const; |
| 102 | 102 |
| 103 // Returns number of bytes that were written in buffer and haven't | 103 // Returns number of bytes that were written in buffer and haven't |
| 104 // been sent. | 104 // been sent. |
| 105 uint32 GetBytesBufferedNotSent() const; | 105 uint32_t GetBytesBufferedNotSent() const; |
| 106 | 106 |
| 107 // Returns current round-trip time estimate in milliseconds. | 107 // Returns current round-trip time estimate in milliseconds. |
| 108 uint32 GetRoundTripTimeEstimateMs() const; | 108 uint32_t GetRoundTripTimeEstimateMs() const; |
| 109 | 109 |
| 110 protected: | 110 protected: |
| 111 enum SendFlags { sfNone, sfDelayedAck, sfImmediateAck }; | 111 enum SendFlags { sfNone, sfDelayedAck, sfImmediateAck }; |
| 112 | 112 |
| 113 struct Segment { | 113 struct Segment { |
| 114 uint32 conv, seq, ack; | 114 uint32_t conv, seq, ack; |
| 115 uint8 flags; | 115 uint8_t flags; |
| 116 uint16 wnd; | 116 uint16_t wnd; |
| 117 const char * data; | 117 const char * data; |
| 118 uint32 len; | 118 uint32_t len; |
| 119 uint32 tsval, tsecr; | 119 uint32_t tsval, tsecr; |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 struct SSegment { | 122 struct SSegment { |
| 123 SSegment(uint32 s, uint32 l, bool c) | 123 SSegment(uint32_t s, uint32_t l, bool c) |
| 124 : seq(s), len(l), /*tstamp(0),*/ xmit(0), bCtrl(c) { | 124 : seq(s), len(l), /*tstamp(0),*/ xmit(0), bCtrl(c) {} |
| 125 } | 125 uint32_t seq, len; |
| 126 uint32 seq, len; | 126 // uint32_t tstamp; |
| 127 //uint32 tstamp; | 127 uint8_t xmit; |
| 128 uint8 xmit; | |
| 129 bool bCtrl; | 128 bool bCtrl; |
| 130 }; | 129 }; |
| 131 typedef std::list<SSegment> SList; | 130 typedef std::list<SSegment> SList; |
| 132 | 131 |
| 133 struct RSegment { | 132 struct RSegment { |
| 134 uint32 seq, len; | 133 uint32_t seq, len; |
| 135 }; | 134 }; |
| 136 | 135 |
| 137 uint32 queue(const char* data, uint32 len, bool bCtrl); | 136 uint32_t queue(const char* data, uint32_t len, bool bCtrl); |
| 138 | 137 |
| 139 // Creates a packet and submits it to the network. This method can either | 138 // Creates a packet and submits it to the network. This method can either |
| 140 // send payload or just an ACK packet. | 139 // send payload or just an ACK packet. |
| 141 // | 140 // |
| 142 // |seq| is the sequence number of this packet. | 141 // |seq| is the sequence number of this packet. |
| 143 // |flags| is the flags for sending this packet. | 142 // |flags| is the flags for sending this packet. |
| 144 // |offset| is the offset to read from |m_sbuf|. | 143 // |offset| is the offset to read from |m_sbuf|. |
| 145 // |len| is the number of bytes to read from |m_sbuf| as payload. If this | 144 // |len| is the number of bytes to read from |m_sbuf| as payload. If this |
| 146 // value is 0 then this is an ACK packet, otherwise this packet has payload. | 145 // value is 0 then this is an ACK packet, otherwise this packet has payload. |
| 147 IPseudoTcpNotify::WriteResult packet(uint32 seq, uint8 flags, | 146 IPseudoTcpNotify::WriteResult packet(uint32_t seq, |
| 148 uint32 offset, uint32 len); | 147 uint8_t flags, |
| 149 bool parse(const uint8* buffer, uint32 size); | 148 uint32_t offset, |
| 149 uint32_t len); |
| 150 bool parse(const uint8_t* buffer, uint32_t size); |
| 150 | 151 |
| 151 void attemptSend(SendFlags sflags = sfNone); | 152 void attemptSend(SendFlags sflags = sfNone); |
| 152 | 153 |
| 153 void closedown(uint32 err = 0); | 154 void closedown(uint32_t err = 0); |
| 154 | 155 |
| 155 bool clock_check(uint32 now, long& nTimeout); | 156 bool clock_check(uint32_t now, long& nTimeout); |
| 156 | 157 |
| 157 bool process(Segment& seg); | 158 bool process(Segment& seg); |
| 158 bool transmit(const SList::iterator& seg, uint32 now); | 159 bool transmit(const SList::iterator& seg, uint32_t now); |
| 159 | 160 |
| 160 void adjustMTU(); | 161 void adjustMTU(); |
| 161 | 162 |
| 162 protected: | 163 protected: |
| 163 // This method is used in test only to query receive buffer state. | 164 // This method is used in test only to query receive buffer state. |
| 164 bool isReceiveBufferFull() const; | 165 bool isReceiveBufferFull() const; |
| 165 | 166 |
| 166 // This method is only used in tests, to disable window scaling | 167 // This method is only used in tests, to disable window scaling |
| 167 // support for testing backward compatibility. | 168 // support for testing backward compatibility. |
| 168 void disableWindowScale(); | 169 void disableWindowScale(); |
| 169 | 170 |
| 170 private: | 171 private: |
| 171 // Queue the connect message with TCP options. | 172 // Queue the connect message with TCP options. |
| 172 void queueConnectMessage(); | 173 void queueConnectMessage(); |
| 173 | 174 |
| 174 // Parse TCP options in the header. | 175 // Parse TCP options in the header. |
| 175 void parseOptions(const char* data, uint32 len); | 176 void parseOptions(const char* data, uint32_t len); |
| 176 | 177 |
| 177 // Apply a TCP option that has been read from the header. | 178 // Apply a TCP option that has been read from the header. |
| 178 void applyOption(char kind, const char* data, uint32 len); | 179 void applyOption(char kind, const char* data, uint32_t len); |
| 179 | 180 |
| 180 // Apply window scale option. | 181 // Apply window scale option. |
| 181 void applyWindowScaleOption(uint8 scale_factor); | 182 void applyWindowScaleOption(uint8_t scale_factor); |
| 182 | 183 |
| 183 // Resize the send buffer with |new_size| in bytes. | 184 // Resize the send buffer with |new_size| in bytes. |
| 184 void resizeSendBuffer(uint32 new_size); | 185 void resizeSendBuffer(uint32_t new_size); |
| 185 | 186 |
| 186 // Resize the receive buffer with |new_size| in bytes. This call adjusts | 187 // Resize the receive buffer with |new_size| in bytes. This call adjusts |
| 187 // window scale factor |m_swnd_scale| accordingly. | 188 // window scale factor |m_swnd_scale| accordingly. |
| 188 void resizeReceiveBuffer(uint32 new_size); | 189 void resizeReceiveBuffer(uint32_t new_size); |
| 189 | 190 |
| 190 IPseudoTcpNotify* m_notify; | 191 IPseudoTcpNotify* m_notify; |
| 191 enum Shutdown { SD_NONE, SD_GRACEFUL, SD_FORCEFUL } m_shutdown; | 192 enum Shutdown { SD_NONE, SD_GRACEFUL, SD_FORCEFUL } m_shutdown; |
| 192 int m_error; | 193 int m_error; |
| 193 | 194 |
| 194 // TCB data | 195 // TCB data |
| 195 TcpState m_state; | 196 TcpState m_state; |
| 196 uint32 m_conv; | 197 uint32_t m_conv; |
| 197 bool m_bReadEnable, m_bWriteEnable, m_bOutgoing; | 198 bool m_bReadEnable, m_bWriteEnable, m_bOutgoing; |
| 198 uint32 m_lasttraffic; | 199 uint32_t m_lasttraffic; |
| 199 | 200 |
| 200 // Incoming data | 201 // Incoming data |
| 201 typedef std::list<RSegment> RList; | 202 typedef std::list<RSegment> RList; |
| 202 RList m_rlist; | 203 RList m_rlist; |
| 203 uint32 m_rbuf_len, m_rcv_nxt, m_rcv_wnd, m_lastrecv; | 204 uint32_t m_rbuf_len, m_rcv_nxt, m_rcv_wnd, m_lastrecv; |
| 204 uint8 m_rwnd_scale; // Window scale factor. | 205 uint8_t m_rwnd_scale; // Window scale factor. |
| 205 rtc::FifoBuffer m_rbuf; | 206 rtc::FifoBuffer m_rbuf; |
| 206 | 207 |
| 207 // Outgoing data | 208 // Outgoing data |
| 208 SList m_slist; | 209 SList m_slist; |
| 209 uint32 m_sbuf_len, m_snd_nxt, m_snd_wnd, m_lastsend, m_snd_una; | 210 uint32_t m_sbuf_len, m_snd_nxt, m_snd_wnd, m_lastsend, m_snd_una; |
| 210 uint8 m_swnd_scale; // Window scale factor. | 211 uint8_t m_swnd_scale; // Window scale factor. |
| 211 rtc::FifoBuffer m_sbuf; | 212 rtc::FifoBuffer m_sbuf; |
| 212 | 213 |
| 213 // Maximum segment size, estimated protocol level, largest segment sent | 214 // Maximum segment size, estimated protocol level, largest segment sent |
| 214 uint32 m_mss, m_msslevel, m_largest, m_mtu_advise; | 215 uint32_t m_mss, m_msslevel, m_largest, m_mtu_advise; |
| 215 // Retransmit timer | 216 // Retransmit timer |
| 216 uint32 m_rto_base; | 217 uint32_t m_rto_base; |
| 217 | 218 |
| 218 // Timestamp tracking | 219 // Timestamp tracking |
| 219 uint32 m_ts_recent, m_ts_lastack; | 220 uint32_t m_ts_recent, m_ts_lastack; |
| 220 | 221 |
| 221 // Round-trip calculation | 222 // Round-trip calculation |
| 222 uint32 m_rx_rttvar, m_rx_srtt, m_rx_rto; | 223 uint32_t m_rx_rttvar, m_rx_srtt, m_rx_rto; |
| 223 | 224 |
| 224 // Congestion avoidance, Fast retransmit/recovery, Delayed ACKs | 225 // Congestion avoidance, Fast retransmit/recovery, Delayed ACKs |
| 225 uint32 m_ssthresh, m_cwnd; | 226 uint32_t m_ssthresh, m_cwnd; |
| 226 uint8 m_dup_acks; | 227 uint8_t m_dup_acks; |
| 227 uint32 m_recover; | 228 uint32_t m_recover; |
| 228 uint32 m_t_ack; | 229 uint32_t m_t_ack; |
| 229 | 230 |
| 230 // Configuration options | 231 // Configuration options |
| 231 bool m_use_nagling; | 232 bool m_use_nagling; |
| 232 uint32 m_ack_delay; | 233 uint32_t m_ack_delay; |
| 233 | 234 |
| 234 // This is used by unit tests to test backward compatibility of | 235 // This is used by unit tests to test backward compatibility of |
| 235 // PseudoTcp implementations that don't support window scaling. | 236 // PseudoTcp implementations that don't support window scaling. |
| 236 bool m_support_wnd_scale; | 237 bool m_support_wnd_scale; |
| 237 }; | 238 }; |
| 238 | 239 |
| 239 } // namespace cricket | 240 } // namespace cricket |
| 240 | 241 |
| 241 #endif // WEBRTC_P2P_BASE_PSEUDOTCP_H_ | 242 #endif // WEBRTC_P2P_BASE_PSEUDOTCP_H_ |
| OLD | NEW |