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 #include "talk/p2p/base/transportchannelproxy.h"
29 #include "talk/base/common.h"
30 #include "talk/p2p/base/transport.h"
31 #include "talk/p2p/base/transportchannelimpl.h"
32
33 namespace cricket {
34
TransportChannelProxy(const std::string & name,const std::string & content_type)35 TransportChannelProxy::TransportChannelProxy(const std::string& name,
36 const std::string &content_type)
37 : TransportChannel(name, content_type), impl_(NULL) {
38 }
39
~TransportChannelProxy()40 TransportChannelProxy::~TransportChannelProxy() {
41 if (impl_)
42 impl_->GetTransport()->DestroyChannel(impl_->name());
43 }
44
SetImplementation(TransportChannelImpl * impl)45 void TransportChannelProxy::SetImplementation(TransportChannelImpl* impl) {
46 impl_ = impl;
47 impl_->SignalReadableState.connect(
48 this, &TransportChannelProxy::OnReadableState);
49 impl_->SignalWritableState.connect(
50 this, &TransportChannelProxy::OnWritableState);
51 impl_->SignalReadPacket.connect(this, &TransportChannelProxy::OnReadPacket);
52 impl_->SignalRouteChange.connect(this, &TransportChannelProxy::OnRouteChange);
53 for (OptionList::iterator it = pending_options_.begin();
54 it != pending_options_.end();
55 ++it) {
56 impl_->SetOption(it->first, it->second);
57 }
58 pending_options_.clear();
59 }
60
SendPacket(const char * data,size_t len)61 int TransportChannelProxy::SendPacket(const char *data, size_t len) {
62 // Fail if we don't have an impl yet.
63 return (impl_) ? impl_->SendPacket(data, len) : -1;
64 }
65
SetOption(talk_base::Socket::Option opt,int value)66 int TransportChannelProxy::SetOption(talk_base::Socket::Option opt, int value) {
67 if (impl_)
68 return impl_->SetOption(opt, value);
69 pending_options_.push_back(OptionPair(opt, value));
70 return 0;
71 }
72
GetError()73 int TransportChannelProxy::GetError() {
74 ASSERT(impl_ != NULL); // should not be used until channel is writable
75 return impl_->GetError();
76 }
77
GetP2PChannel()78 P2PTransportChannel* TransportChannelProxy::GetP2PChannel() {
79 if (impl_) {
80 return impl_->GetP2PChannel();
81 }
82 return NULL;
83 }
84
OnReadableState(TransportChannel * channel)85 void TransportChannelProxy::OnReadableState(TransportChannel* channel) {
86 ASSERT(channel == impl_);
87 set_readable(impl_->readable());
88 }
89
OnWritableState(TransportChannel * channel)90 void TransportChannelProxy::OnWritableState(TransportChannel* channel) {
91 ASSERT(channel == impl_);
92 set_writable(impl_->writable());
93 }
94
OnReadPacket(TransportChannel * channel,const char * data,size_t size)95 void TransportChannelProxy::OnReadPacket(TransportChannel* channel,
96 const char* data, size_t size) {
97 ASSERT(channel == impl_);
98 SignalReadPacket(this, data, size);
99 }
100
OnRouteChange(TransportChannel * channel,const talk_base::SocketAddress & address)101 void TransportChannelProxy::OnRouteChange(TransportChannel* channel,
102 const talk_base::SocketAddress& address) {
103 ASSERT(channel == impl_);
104 SignalRouteChange(this, address);
105 }
106
107 } // namespace cricket
108