transportchannelproxy.cc revision f74420b3285b9fe04a7e00aa3b8c0ab07ea344bc
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
33namespace cricket {
34
35TransportChannelProxy::TransportChannelProxy(const std::string& name,
36                                             const std::string &session_type)
37    : TransportChannel(name, session_type), impl_(NULL) {
38}
39
40TransportChannelProxy::~TransportChannelProxy() {
41  if (impl_)
42    impl_->GetTransport()->DestroyChannel(impl_->name());
43}
44
45void 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
61int 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
66int 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
73int TransportChannelProxy::GetError() {
74  ASSERT(impl_ != NULL);  // should not be used until channel is writable
75  return impl_->GetError();
76}
77
78void TransportChannelProxy::OnReadableState(TransportChannel* channel) {
79  ASSERT(channel == impl_);
80  set_readable(impl_->readable());
81}
82
83void TransportChannelProxy::OnWritableState(TransportChannel* channel) {
84  ASSERT(channel == impl_);
85  set_writable(impl_->writable());
86}
87
88void TransportChannelProxy::OnReadPacket(TransportChannel* channel,
89                                         const char* data, size_t size) {
90  ASSERT(channel == impl_);
91  SignalReadPacket(this, data, size);
92}
93
94void TransportChannelProxy::OnRouteChange(TransportChannel* channel,
95                                          const talk_base::SocketAddress& address) {
96  ASSERT(channel == impl_);
97  SignalRouteChange(this, address);
98}
99
100}  // namespace cricket
101