1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#ifndef TALK_P2P_BASE_STUNREQUEST_H_
29#define TALK_P2P_BASE_STUNREQUEST_H_
30
31#include "talk/base/sigslot.h"
32#include "talk/base/thread.h"
33#include "talk/p2p/base/stun.h"
34#include <map>
35#include <string>
36
37namespace cricket {
38
39class StunRequest;
40
41// Manages a set of STUN requests, sending and resending until we receive a
42// response or determine that the request has timed out.
43class StunRequestManager {
44public:
45  StunRequestManager(talk_base::Thread* thread);
46  ~StunRequestManager();
47
48  // Starts sending the given request (perhaps after a delay).
49  void Send(StunRequest* request);
50  void SendDelayed(StunRequest* request, int delay);
51
52  // Removes a stun request that was added previously.  This will happen
53  // automatically when a request succeeds, fails, or times out.
54  void Remove(StunRequest* request);
55
56  // Removes all stun requests that were added previously.
57  void Clear();
58
59  // Determines whether the given message is a response to one of the
60  // outstanding requests, and if so, processes it appropriately.
61  bool CheckResponse(StunMessage* msg);
62  bool CheckResponse(const char* data, size_t size);
63
64  // Raised when there are bytes to be sent.
65  sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
66
67private:
68  typedef std::map<std::string, StunRequest*> RequestMap;
69
70  talk_base::Thread* thread_;
71  RequestMap requests_;
72
73  friend class StunRequest;
74};
75
76// Represents an individual request to be sent.  The STUN message can either be
77// constructed beforehand or built on demand.
78class StunRequest : public talk_base::MessageHandler {
79public:
80  StunRequest();
81  StunRequest(StunMessage* request);
82  virtual ~StunRequest();
83
84  // Causes our wrapped StunMessage to be Prepared
85  void Construct();
86
87  // The manager handling this request (if it has been scheduled for sending).
88  StunRequestManager* manager() { return manager_; }
89
90  // Returns the transaction ID of this request.
91  const std::string& id() { return id_; }
92
93  // Returns the STUN type of the request message.
94  StunMessageType type();
95
96  // Handles messages for sending and timeout.
97  void OnMessage(talk_base::Message* pmsg);
98
99  // Time elapsed since last send (in ms)
100  uint32 Elapsed() const;
101
102protected:
103  int count_;
104  bool timeout_;
105
106  // Fills in a request object to be sent.  Note that request's transaction ID
107  // will already be set and cannot be changed.
108  virtual void Prepare(StunMessage* request) {}
109
110  // Called when the message receives a response or times out.
111  virtual void OnResponse(StunMessage* response) {}
112  virtual void OnErrorResponse(StunMessage* response) {}
113  virtual void OnTimeout() {}
114  virtual int GetNextDelay();
115
116private:
117  StunRequestManager* manager_;
118  std::string id_;
119  StunMessage* msg_;
120  uint32 tstamp_;
121
122  void set_manager(StunRequestManager* manager);
123
124  friend class StunRequestManager;
125};
126
127}  // namespace cricket
128
129#endif  // TALK_P2P_BASE_STUNREQUEST_H_
130