• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TCPPORT_H_
29 #define TALK_P2P_BASE_TCPPORT_H_
30 
31 #include <string>
32 #include <list>
33 #include "talk/base/asyncpacketsocket.h"
34 #include "talk/p2p/base/port.h"
35 
36 namespace cricket {
37 
38 class TCPConnection;
39 
40 extern const std::string LOCAL_PORT_TYPE;  // type of TCP ports
41 
42 // Communicates using a local TCP port.
43 //
44 // This class is designed to allow subclasses to take advantage of the
45 // connection management provided by this class.  A subclass should take of all
46 // packet sending and preparation, but when a packet is received, it should
47 // call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection.
48 class TCPPort : public Port {
49  public:
Create(talk_base::Thread * thread,talk_base::PacketSocketFactory * factory,talk_base::Network * network,uint32 ip,int min_port,int max_port,bool allow_listen)50   static TCPPort* Create(talk_base::Thread* thread,
51                          talk_base::PacketSocketFactory* factory,
52                          talk_base::Network* network,
53                          uint32 ip, int min_port, int max_port,
54                          bool allow_listen) {
55     TCPPort* port = new TCPPort(thread, factory, network,
56                                 ip, min_port, max_port, allow_listen);
57     if (!port->Init()) {
58       delete port;
59       port = NULL;
60     }
61     return port;
62   }
63   virtual ~TCPPort();
64 
65   virtual Connection* CreateConnection(const Candidate& address,
66                                        CandidateOrigin origin);
67 
68   virtual void PrepareAddress();
69 
70   virtual int SetOption(talk_base::Socket::Option opt, int value);
71   virtual int GetError();
72 
73  protected:
74   TCPPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
75           talk_base::Network* network, uint32 ip, int min_port, int max_port,
76           bool allow_listen);
77   bool Init();
78 
79   // Handles sending using the local TCP socket.
80   virtual int SendTo(const void* data, size_t size,
81                      const talk_base::SocketAddress& addr, bool payload);
82 
83   // Accepts incoming TCP connection.
84   void OnNewConnection(talk_base::AsyncPacketSocket* socket,
85                        talk_base::AsyncPacketSocket* new_socket);
86 
87  private:
88   struct Incoming {
89     talk_base::SocketAddress addr;
90     talk_base::AsyncPacketSocket* socket;
91   };
92 
93   talk_base::AsyncPacketSocket* GetIncoming(
94       const talk_base::SocketAddress& addr, bool remove = false);
95 
96   // Receives packet signal from the local TCP Socket.
97   void OnReadPacket(talk_base::AsyncPacketSocket* socket,
98                     const char* data, size_t size,
99                     const talk_base::SocketAddress& remote_addr);
100 
101   void OnAddresReady(talk_base::AsyncPacketSocket* socket,
102                      const talk_base::SocketAddress& address);
103 
104   // TODO: Is this still needed?
105   bool incoming_only_;
106   bool allow_listen_;
107   talk_base::AsyncPacketSocket* socket_;
108   int error_;
109   std::list<Incoming> incoming_;
110 
111   friend class TCPConnection;
112 };
113 
114 class TCPConnection : public Connection {
115  public:
116   // Connection is outgoing unless socket is specified
117   TCPConnection(TCPPort* port, const Candidate& candidate,
118                 talk_base::AsyncPacketSocket* socket = 0);
119   virtual ~TCPConnection();
120 
121   virtual int Send(const void* data, size_t size);
122   virtual int GetError();
123 
socket()124   talk_base::AsyncPacketSocket* socket() { return socket_; }
125 
126  private:
127   void OnConnect(talk_base::AsyncPacketSocket* socket);
128   void OnClose(talk_base::AsyncPacketSocket* socket, int error);
129   void OnReadPacket(talk_base::AsyncPacketSocket* socket,
130                     const char* data, size_t size,
131                     const talk_base::SocketAddress& remote_addr);
132 
133   talk_base::AsyncPacketSocket* socket_;
134   int error_;
135 
136   friend class TCPPort;
137 };
138 
139 }  // namespace cricket
140 
141 #endif  // TALK_P2P_BASE_TCPPORT_H_
142