📄 port.h
字号:
/* * libjingle * Copyright 2004--2005, Google Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#ifndef __PORT_H__#define __PORT_H__#include "talk/base/network.h"#include "talk/base/socketaddress.h"#include "talk/base/proxyinfo.h"#include "talk/base/sigslot.h"#include "talk/base/thread.h"#include "talk/p2p/base/candidate.h"#include "talk/p2p/base/stun.h"#include "talk/p2p/base/stunrequest.h"#include <string>#include <vector>#include <map>namespace talk_base {class AsyncPacketSocket;}namespace cricket {class Connection;enum ProtocolType { PROTO_UDP, PROTO_TCP, PROTO_SSLTCP, PROTO_LAST = PROTO_SSLTCP };const char * ProtoToString(ProtocolType proto);bool StringToProto(const char * value, ProtocolType& proto);struct ProtocolAddress { talk_base::SocketAddress address; ProtocolType proto; ProtocolAddress(const talk_base::SocketAddress& a, ProtocolType p) : address(a), proto(p) { }};// Represents a local communication mechanism that can be used to create// connections to similar mechanisms of the other client. Subclasses of this// one add support for specific mechanisms like local UDP ports.class Port : public talk_base::MessageHandler, public sigslot::has_slots<> {public: Port(talk_base::Thread* thread, const std::string &type, talk_base::SocketFactory* factory, talk_base::Network* network); virtual ~Port(); // The thread on which this port performs its I/O. talk_base::Thread* thread() { return thread_; } // The factory used to create the sockets of this port. talk_base::SocketFactory* socket_factory() const { return factory_; } void set_socket_factory(talk_base::SocketFactory* factory) { factory_ = factory; } // Each port is identified by a name (for debugging purposes). const std::string& name() const { return name_; } void set_name(const std::string& name) { name_ = name; } // In order to establish a connection to this Port (so that real data can be // sent through), the other side must send us a STUN binding request that is // authenticated with this username and password. const std::string& username_fragment() const { return username_frag_; } const std::string& password() const { return password_; } // A value in [0,1] that indicates the preference for this port versus other // ports on this client. (Larger indicates more preference.) float preference() const { return preference_; } void set_preference(float preference) { preference_ = preference; } // Identifies the port type. //const std::string& protocol() const { return proto_; } const std::string& type() const { return type_; } // Identifies network that this port was allocated on. talk_base::Network* network() { return network_; } // Identifies the generation that this port was created in. uint32 generation() { return generation_; } void set_generation(uint32 generation) { generation_ = generation; } // PrepareAddress will attempt to get an address for this port that other // clients can send to. It may take some time before the address is read. // Once it is ready, we will send SignalAddressReady. If errors are // preventing the port from getting an address, it may send // SignalAddressError. virtual void PrepareAddress() = 0; sigslot::signal1<Port*> SignalAddressReady; sigslot::signal1<Port*> SignalAddressError; // Provides all of the above information in one handy object. const std::vector<Candidate>& candidates() const { return candidates_; } // Returns a map containing all of the connections of this port, keyed by the // remote address. typedef std::map<talk_base::SocketAddress, Connection*> AddressMap; const AddressMap& connections() { return connections_; } // Returns the connection to the given address or NULL if none exists. Connection* GetConnection(const talk_base::SocketAddress& remote_addr); // Creates a new connection to the given address. enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE }; virtual Connection* CreateConnection(const Candidate& remote_candidate, CandidateOrigin origin) = 0; // Called each time a connection is created. sigslot::signal2<Port*, Connection*> SignalConnectionCreated; // Sends the given packet to the given address, provided that the address is // that of a connection or an address that has sent to us already. virtual int SendTo( const void* data, size_t size, const talk_base::SocketAddress& addr, bool payload) = 0; // Indicates that we received a successful STUN binding request from an // address that doesn't correspond to any current connection. To turn this // into a real connection, call CreateConnection. sigslot::signal4<Port*, const talk_base::SocketAddress&, StunMessage*, const std::string&> SignalUnknownAddress; // Sends a response message (normal or error) to the given request. One of // these methods should be called as a response to SignalUnknownAddress. // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse. void SendBindingResponse(StunMessage* request, const talk_base::SocketAddress& addr); void SendBindingErrorResponse( StunMessage* request, const talk_base::SocketAddress& addr, int error_code, const std::string& reason); // Indicates that errors occurred when performing I/O. sigslot::signal2<Port*, int> SignalReadError; sigslot::signal2<Port*, int> SignalWriteError; // Functions on the underlying socket(s). virtual int SetOption(talk_base::Socket::Option opt, int value) = 0; virtual int GetError() = 0; static void set_proxy(const std::string& user_agent, const talk_base::ProxyInfo& proxy) { agent_ = user_agent; proxy_ = proxy; } static const std::string& user_agent() { return agent_; } static const talk_base::ProxyInfo& proxy() { return proxy_; } talk_base::AsyncPacketSocket * CreatePacketSocket(ProtocolType proto); // Normally, packets arrive through a connection (or they result signaling of // unknown address). Calling this method turns off delivery of packets // through their respective connection and instead delivers every packet // through this port. void EnablePortPackets(); sigslot::signal4<Port*, const char*, size_t, const talk_base::SocketAddress&> SignalReadPacket; // Indicates to the port that its official use has now begun. This will // start the timer that checks to see if the port is being used. void Start(); // Called if the port has no connections and is no longer useful. void Destroy(); // Signaled when this port decides to delete itself because it no longer has // any usefulness. sigslot::signal1<Port*> SignalDestroyed; virtual void OnMessage(talk_base::Message *pmsg); // Debugging description of this port std::string ToString() const;protected: talk_base::Thread* thread_; talk_base::SocketFactory* factory_; std::string type_; talk_base::Network* network_; uint32 generation_; std::string name_; std::string username_frag_; std::string password_;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -