3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef TALK_P2P_BASE_PSEUDOTCP_H_
29
#define TALK_P2P_BASE_PSEUDOTCP_H_
33
#include "talk/base/basictypes.h"
34
#include "talk/base/stream.h"
38
//////////////////////////////////////////////////////////////////////
40
//////////////////////////////////////////////////////////////////////
44
class IPseudoTcpNotify {
46
virtual ~IPseudoTcpNotify() {}
47
// Notification of tcp events
48
virtual void OnTcpOpen(PseudoTcp* tcp) = 0;
49
virtual void OnTcpReadable(PseudoTcp* tcp) = 0;
50
virtual void OnTcpWriteable(PseudoTcp* tcp) = 0;
51
virtual void OnTcpClosed(PseudoTcp* tcp, uint32 error) = 0;
53
// Write the packet onto the network
54
enum WriteResult { WR_SUCCESS, WR_TOO_LARGE, WR_FAIL };
55
virtual WriteResult TcpWritePacket(PseudoTcp* tcp,
56
const char* buffer, size_t len) = 0;
59
//////////////////////////////////////////////////////////////////////
61
//////////////////////////////////////////////////////////////////////
67
PseudoTcp(IPseudoTcpNotify* notify, uint32 conv);
71
int Recv(char* buffer, size_t len);
72
int Send(const char* buffer, size_t len);
73
void Close(bool force);
77
TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECEIVED, TCP_ESTABLISHED, TCP_CLOSED
79
TcpState State() const { return m_state; }
81
// Call this when the PMTU changes.
82
void NotifyMTU(uint16 mtu);
84
// Call this based on timeout value returned from GetNextClock.
85
// It's ok to call this too frequently.
86
void NotifyClock(uint32 now);
88
// Call this whenever a packet arrives.
89
// Returns true if the packet was processed successfully.
90
bool NotifyPacket(const char * buffer, size_t len);
92
// Call this to determine the next time NotifyClock should be called.
93
// Returns false if the socket is ready to be destroyed.
94
bool GetNextClock(uint32 now, long& timeout);
96
// Call these to get/set option values to tailor this PseudoTcp
97
// instance's behaviour for the kind of data it will carry.
98
// If an unrecognized option is set or got, an assertion will fire.
100
// Setting options for OPT_RCVBUF or OPT_SNDBUF after Connect() is called
101
// will result in an assertion.
103
OPT_NODELAY, // Whether to enable Nagle's algorithm (0 == off)
104
OPT_ACKDELAY, // The Delayed ACK timeout (0 == off).
105
OPT_RCVBUF, // Set the receive buffer size, in bytes.
106
OPT_SNDBUF, // Set the send buffer size, in bytes.
108
void GetOption(Option opt, int* value);
109
void SetOption(Option opt, int value);
111
// Returns current congestion window in bytes.
112
uint32 GetCongestionWindow() const;
114
// Returns amount of data in bytes that has been sent, but haven't
115
// been acknowledged.
116
uint32 GetBytesInFlight() const;
118
// Returns number of bytes that were written in buffer and haven't
120
uint32 GetBytesBufferedNotSent() const;
122
// Returns current round-trip time estimate in milliseconds.
123
uint32 GetRoundTripTimeEstimateMs() const;
126
enum SendFlags { sfNone, sfDelayedAck, sfImmediateAck };
129
uint32 conv, seq, ack;
138
SSegment(uint32 s, uint32 l, bool c)
139
: seq(s), len(l), /*tstamp(0),*/ xmit(0), bCtrl(c) {
146
typedef std::list<SSegment> SList;
152
uint32 queue(const char* data, uint32 len, bool bCtrl);
154
// Creates a packet and submits it to the network. This method can either
155
// send payload or just an ACK packet.
157
// |seq| is the sequence number of this packet.
158
// |flags| is the flags for sending this packet.
159
// |offset| is the offset to read from |m_sbuf|.
160
// |len| is the number of bytes to read from |m_sbuf| as payload. If this
161
// value is 0 then this is an ACK packet, otherwise this packet has payload.
162
IPseudoTcpNotify::WriteResult packet(uint32 seq, uint8 flags,
163
uint32 offset, uint32 len);
164
bool parse(const uint8* buffer, uint32 size);
166
void attemptSend(SendFlags sflags = sfNone);
168
void closedown(uint32 err = 0);
170
bool clock_check(uint32 now, long& nTimeout);
172
bool process(Segment& seg);
173
bool transmit(const SList::iterator& seg, uint32 now);
178
// This method is used in test only to query receive buffer state.
179
bool isReceiveBufferFull() const;
181
// This method is only used in tests, to disable window scaling
182
// support for testing backward compatibility.
183
void disableWindowScale();
186
// Queue the connect message with TCP options.
187
void queueConnectMessage();
189
// Parse TCP options in the header.
190
void parseOptions(const char* data, uint32 len);
192
// Apply a TCP option that has been read from the header.
193
void applyOption(char kind, const char* data, uint32 len);
195
// Apply window scale option.
196
void applyWindowScaleOption(uint8 scale_factor);
198
// Resize the send buffer with |new_size| in bytes.
199
void resizeSendBuffer(uint32 new_size);
201
// Resize the receive buffer with |new_size| in bytes. This call adjusts
202
// window scale factor |m_swnd_scale| accordingly.
203
void resizeReceiveBuffer(uint32 new_size);
205
IPseudoTcpNotify* m_notify;
206
enum Shutdown { SD_NONE, SD_GRACEFUL, SD_FORCEFUL } m_shutdown;
212
bool m_bReadEnable, m_bWriteEnable, m_bOutgoing;
213
uint32 m_lasttraffic;
216
typedef std::list<RSegment> RList;
218
uint32 m_rbuf_len, m_rcv_nxt, m_rcv_wnd, m_lastrecv;
219
uint8 m_rwnd_scale; // Window scale factor.
220
talk_base::FifoBuffer m_rbuf;
224
uint32 m_sbuf_len, m_snd_nxt, m_snd_wnd, m_lastsend, m_snd_una;
225
uint8 m_swnd_scale; // Window scale factor.
226
talk_base::FifoBuffer m_sbuf;
228
// Maximum segment size, estimated protocol level, largest segment sent
229
uint32 m_mss, m_msslevel, m_largest, m_mtu_advise;
233
// Timestamp tracking
234
uint32 m_ts_recent, m_ts_lastack;
236
// Round-trip calculation
237
uint32 m_rx_rttvar, m_rx_srtt, m_rx_rto;
239
// Congestion avoidance, Fast retransmit/recovery, Delayed ACKs
240
uint32 m_ssthresh, m_cwnd;
245
// Configuration options
249
// This is used by unit tests to test backward compatibility of
250
// PseudoTcp implementations that don't support window scaling.
251
bool m_support_wnd_scale;
254
} // namespace cricket
256
#endif // TALK_P2P_BASE_PSEUDOTCP_H_