summaryrefslogtreecommitdiffstats
path: root/third_party/libjingle/files/talk/base/streamutils.h
blob: 7bb07a3d26fd425f1b783ac5d918d2825449d914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * libjingle
 * Copyright 2004--2006, 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 TALK_APP_STREAMUTILS_H__
#define TALK_APP_STREAMUTILS_H__

#include "talk/base/sigslot.h"
#include "talk/base/stream.h"

///////////////////////////////////////////////////////////////////////////////
// StreamRelay - acts as an intermediary between two asynchronous streams,
//  reading from one stream and writing to the other, using a pre-specified
//  amount of buffering in both directions.
///////////////////////////////////////////////////////////////////////////////

class StreamRelay : public sigslot::has_slots<> {
public:
  StreamRelay(talk_base::StreamInterface* s1, 
              talk_base::StreamInterface* s2, size_t buffer_size);
  virtual ~StreamRelay();

  void Circulate(); // Simulate events to get things flowing
  void Close();

  sigslot::signal2<StreamRelay*, int> SignalClosed;

private:
  inline int Index(talk_base::StreamInterface* s) const 
    { return (s == dir_[1].stream); }
  inline int Complement(int index) const { return (1-index); }

  bool Flow(int read_index, int* error);
  void OnEvent(talk_base::StreamInterface* stream, int events, int error);

  struct Direction {
    talk_base::StreamInterface* stream;
    char* buffer;
    size_t data_len;
  };
  Direction dir_[2];
  size_t buffer_size_;
};

///////////////////////////////////////////////////////////////////////////////
// StreamCounter - counts the number of bytes which are transferred in either
//  direction.
///////////////////////////////////////////////////////////////////////////////

class StreamCounter : public talk_base::StreamAdapterInterface {
 public:
  explicit StreamCounter(talk_base::StreamInterface* stream);

  inline void ResetByteCount() { count_ = 0; }
  inline size_t GetByteCount() const { return count_; }

  sigslot::signal1<size_t> SignalUpdateByteCount;

  // StreamAdapterInterface
  virtual talk_base::StreamResult Read(void* buffer, size_t buffer_len,
                                       size_t* read, int* error);
  virtual talk_base::StreamResult Write(const void* data, size_t data_len,
                                        size_t* written, int* error);

 private:
  size_t count_;
};

///////////////////////////////////////////////////////////////////////////////

#endif  // TALK_APP_STREAMUTILS_H__