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 // Destroy any existing impl_
47 if (impl_) {
48 impl_->GetTransport()->DestroyChannel(impl_->name());
49 }
50
51 impl_ = impl;
52 impl_->SignalReadableState.connect(
53 this, &TransportChannelProxy::OnReadableState);
54 impl_->SignalWritableState.connect(
55 this, &TransportChannelProxy::OnWritableState);
56 impl_->SignalReadPacket.connect(this, &TransportChannelProxy::OnReadPacket);
57 impl_->SignalRouteChange.connect(this, &TransportChannelProxy::OnRouteChange);
58 for (OptionList::iterator it = pending_options_.begin();
59 it != pending_options_.end();
60 ++it) {
61 impl_->SetOption(it->first, it->second);
62 }
63 pending_options_.clear();
64 }
65
SendPacket(const char * data,size_t len)66 int TransportChannelProxy::SendPacket(const char* data, size_t len) {
67 // Fail if we don't have an impl yet.
68 return (impl_) ? impl_->SendPacket(data, len) : -1;
69 }
70
SetOption(talk_base::Socket::Option opt,int value)71 int TransportChannelProxy::SetOption(talk_base::Socket::Option opt, int value) {
72 if (impl_)
73 return impl_->SetOption(opt, value);
74 pending_options_.push_back(OptionPair(opt, value));
75 return 0;
76 }
77
GetError()78 int TransportChannelProxy::GetError() {
79 ASSERT(impl_ != NULL); // should not be used until channel is writable
80 return impl_->GetError();
81 }
82
GetP2PChannel()83 P2PTransportChannel* TransportChannelProxy::GetP2PChannel() {
84 if (impl_) {
85 return impl_->GetP2PChannel();
86 }
87 return NULL;
88 }
89
OnReadableState(TransportChannel * channel)90 void TransportChannelProxy::OnReadableState(TransportChannel* channel) {
91 ASSERT(channel == impl_);
92 set_readable(impl_->readable());
93 // Note: SignalReadableState fired by set_readable.
94 }
95
OnWritableState(TransportChannel * channel)96 void TransportChannelProxy::OnWritableState(TransportChannel* channel) {
97 ASSERT(channel == impl_);
98 set_writable(impl_->writable());
99 // Note: SignalWritableState fired by set_writable.
100 }
101
OnReadPacket(TransportChannel * channel,const char * data,size_t size)102 void TransportChannelProxy::OnReadPacket(TransportChannel* channel,
103 const char* data, size_t size) {
104 ASSERT(channel == impl_);
105 SignalReadPacket(this, data, size);
106 }
107
OnRouteChange(TransportChannel * channel,const Candidate & candidate)108 void TransportChannelProxy::OnRouteChange(TransportChannel* channel,
109 const Candidate& candidate) {
110 ASSERT(channel == impl_);
111 SignalRouteChange(this, candidate);
112 }
113
114 } // namespace cricket
115