1 // Copyright (c) 2011 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.
5 #ifndef NET_CURVECP_MESSENGER_H_
6 #define NET_CURVECP_MESSENGER_H_
12 #include "base/basictypes.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/timer.h"
15 #include "net/base/completion_callback.h"
16 #include "net/curvecp/circular_buffer.h"
17 #include "net/curvecp/packetizer.h"
18 #include "net/curvecp/received_block_list.h"
19 #include "net/curvecp/rtt_and_send_rate_calculator.h"
20 #include "net/curvecp/sent_block_list.h"
21 #include "net/socket/socket.h"
25 class IOBufferWithSize
;
28 // The messenger provides the reliable CurveCP transport.
29 class Messenger
: public base::NonThreadSafe
,
30 public Packetizer::Listener
{
32 explicit Messenger(Packetizer
* packetizer
);
35 int Read(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
36 int Write(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
38 // Packetizer::Listener implementation.
39 virtual void OnConnection(ConnectionKey key
) OVERRIDE
;
40 virtual void OnClose(Packetizer
* packetizer
, ConnectionKey key
);
41 virtual void OnMessage(Packetizer
* packetizer
,
44 size_t length
) OVERRIDE
;
50 // Handle reading data from the read queue.
51 int InternalRead(IOBuffer
* buffer
, int buffer_length
);
53 // Extracts data from send queue to create a new buffer of data to send.
54 IOBufferWithSize
* CreateBufferFromSendQueue();
56 // Continuation function after a SendMessage() call was blocked.
57 void OnSendMessageComplete(int result
);
59 // Protocol handling routines
62 void SendMessage(int64 position
);
64 void SendAck(uint32 last_message_received
);
66 RttAndSendRateCalculator rtt_
;
67 Packetizer
* packetizer_
;
69 // The send_buffer is a list of pending data to pack into messages and send
71 CircularBuffer send_buffer_
;
72 CompletionCallback send_complete_callback_
;
73 scoped_refptr
<IOBuffer
> pending_send_
;
74 int pending_send_length_
;
76 // The read_buffer is a list of pending data which has been unpacked from
77 // messages and is awaiting delivery to the application.
78 CompletionCallback receive_complete_callback_
;
79 scoped_refptr
<IOBuffer
> pending_receive_
;
80 int pending_receive_length_
;
82 // The list of received but unprocessed messages.
83 std::list
<scoped_refptr
<IOBufferWithSize
> > read_queue_
;
85 ReceivedBlockList received_list_
;
86 SentBlockList sent_list_
;
87 bool send_message_in_progress_
;
89 // A timer to fire when a timeout has occurred.
90 base::OneShotTimer
<Messenger
> send_timeout_timer_
;
91 // A timer to fire when we can send data.
92 base::OneShotTimer
<Messenger
> send_timer_
;
94 DISALLOW_COPY_AND_ASSIGN(Messenger
);
99 #endif // NET_CURVECP_MESSENGER_H_