You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef TALK_BASE_TRANSFORMADAPTER_H__
  28. #define TALK_BASE_TRANSFORMADAPTER_H__
  29. #include "talk/base/stream.h"
  30. namespace talk_base {
  31. ///////////////////////////////////////////////////////////////////////////////
  32. class TransformInterface {
  33. public:
  34. virtual ~TransformInterface() { }
  35. // Transform should convert the in_len bytes of input into the out_len-sized
  36. // output buffer. If flush is true, there will be no more data following
  37. // input.
  38. // After the transformation, in_len contains the number of bytes consumed, and
  39. // out_len contains the number of bytes ready in output.
  40. // Note: Transform should not return SR_BLOCK, as there is no asynchronous
  41. // notification available.
  42. virtual StreamResult Transform(const void * input, size_t * in_len,
  43. void * output, size_t * out_len,
  44. bool flush) = 0;
  45. };
  46. ///////////////////////////////////////////////////////////////////////////////
  47. // TransformAdapter causes all data passed through to be transformed by the
  48. // supplied TransformInterface object, which may apply compression, encryption,
  49. // etc.
  50. class TransformAdapter : public StreamAdapterInterface {
  51. public:
  52. // Note that the transformation is unidirectional, in the direction specified
  53. // by the constructor. Operations in the opposite direction result in SR_EOS.
  54. TransformAdapter(StreamInterface * stream,
  55. TransformInterface * transform,
  56. bool direction_read);
  57. virtual ~TransformAdapter();
  58. virtual StreamResult Read(void * buffer, size_t buffer_len,
  59. size_t * read, int * error);
  60. virtual StreamResult Write(const void * data, size_t data_len,
  61. size_t * written, int * error);
  62. virtual void Close();
  63. // Apriori, we can't tell what the transformation does to the stream length.
  64. virtual bool GetAvailable(size_t* size) const { return false; }
  65. virtual bool ReserveSize(size_t size) { return true; }
  66. // Transformations might not be restartable
  67. virtual bool Rewind() { return false; }
  68. private:
  69. enum State { ST_PROCESSING, ST_FLUSHING, ST_COMPLETE, ST_ERROR };
  70. enum { BUFFER_SIZE = 1024 };
  71. TransformInterface * transform_;
  72. bool direction_read_;
  73. State state_;
  74. int error_;
  75. char buffer_[BUFFER_SIZE];
  76. size_t len_;
  77. };
  78. ///////////////////////////////////////////////////////////////////////////////
  79. } // namespace talk_base
  80. #endif // TALK_BASE_TRANSFORMADAPTER_H__