Atlas 0.7.0
Networking protocol for the Worldforge system.
Filter.h
1// This file may be redistributed and modified only under the terms of
2// the GNU Lesser General Public License (See COPYING for details).
3// Copyright (C) 2000-2001 Michael Day, Stefanus Du Toit
4
5// $Id$
6
7#ifndef ATLAS_FILTER_H
8#define ATLAS_FILTER_H
9
10#include <iostream>
11#include <string>
12#include <memory>
13
14namespace Atlas {
15
30class Filter
31{
32 public:
33
34 explicit Filter(std::unique_ptr<Filter> = nullptr);
35 Filter(const Filter &) = delete;
36 Filter & operator=(const Filter &) = delete;
37 virtual ~Filter();
38
39 virtual void begin() = 0;
40 virtual void end() = 0;
41
42 virtual std::string encode(const std::string&) = 0;
43 virtual std::string decode(const std::string&) = 0;
44
45 enum Type
46 {
47 CHECKSUM,
48 COMPRESSION,
49 ENCRYPTION
50 };
51
52 protected:
53
54 std::unique_ptr<Filter> m_next;
55};
56
57typedef int int_type;
58
59class filterbuf : public std::streambuf {
60
61public:
62
63 filterbuf(std::streambuf& buffer,
64 Filter& filter)
65 : m_streamBuffer(buffer), m_filter(filter)
66 {
67 setp(m_outBuffer, m_outBuffer + (m_outBufferSize - 1));
68 setg(m_inBuffer + m_inPutback, m_inBuffer + m_inPutback,
69 m_inBuffer + m_inPutback);
70 }
71
72 ~filterbuf() override;
73
74protected:
75 static const int m_outBufferSize = 10;
76 char m_outBuffer[m_outBufferSize];
77
78 static const int m_inBufferSize = 10;
79 static const int m_inPutback = 4;
80 char m_inBuffer[m_inBufferSize];
81
82 int flushOutBuffer()
83 {
84 int num = pptr() - pbase();
85 std::string encoded = m_filter.encode(std::string(pbase(), pptr()));
86 m_streamBuffer.sputn(encoded.c_str(), (long) encoded.size());
87 pbump(-num);
88 return num;
89 }
90
91 int_type overflow(int_type c) override;
92 int_type underflow() override;
93 int sync() override;
94
95private:
96
97 std::streambuf& m_streamBuffer;
98 Filter& m_filter;
99};
100
101} // Atlas namespace
102
103#endif
Definition: Bridge.h:20