monotone

monotone Mtn Source Tree

Root/netxx/netbuf.h

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains the definition of the Netxx:Netbuf template class.
35**/
36
37#ifndef _netxx_netbuf_h_
38#define _netxx_netbuf_h_
39
40// Netxx includes
41#include <netxx/streambase.h>
42
43// standard includes
44#include <streambuf>
45#include <algorithm>
46#include <cstring>
47
48namespace
49{
50 const std::streamsize PUTBACK_SIZE = 4;
51}
52
53namespace Netxx {
54
55/**
56 * The Netxx::Netbuf template class is a IOStreams streambuf. After you
57 * create an instance of a Netxx::Netbuf class you can pass a pointer to it
58 * to a std::iostream, std::ostream or std::istream class. Then you can do
59 * basic IOStreams operations on it.
60 *
61 * This streambuf is buffered so you should call std::flush to make sure it
62 * sends the data that you inserted.
63**/
64template <typename std::streamsize bufsize, class charT=char, class traits=std::char_traits<char> >
65class Netbuf : public std::basic_streambuf<charT, traits> {
66public:
67 /// int type
68 typedef typename std::basic_streambuf<charT, traits>::int_type int_type;
69
70 /// char type
71 typedef typename std::basic_streambuf<charT, traits>::char_type char_type;
72
73 //####################################################################
74 /**
75 * Construct a Netxx::Netbuf object and link it to the given StreamBase
76 * object. The StreamBase object will be used for reading and writing to
77 * the Netbuf (std::streambuf) object.
78 *
79 * @param stream The StreamBase object to use for reading and writing.
80 * @author Peter Jones
81 **/
82 //####################################################################
83 explicit Netbuf (StreamBase &stream);
84
85 //####################################################################
86 /**
87 * Netxx::Netbuf class destructor.
88 *
89 * @author Peter Jones
90 **/
91 //####################################################################
92 ~Netbuf (void);
93protected:
94 // TODO streamsize xsputn (const char_type *s, streamsize n);
95 int_type overflow (int_type c=traits_type::eof());
96 int sync (void);
97
98 int_type underflow (void);
99 int_type pbackfail (int_type c);
100private:
101 StreamBase &stream_;
102 charT putbuf_[bufsize];
103 charT getbuf_[bufsize];
104
105 int buffer_out (void);
106 int buffer_in (void);
107
108 Netbuf (const Netbuf&);
109 Netbuf& operator= (const Netbuf&);
110
111}; // end Netxx::Netbuf class
112
113//#############################################################################
114template<std::streamsize bufsize, class charT, class traits>
115Netbuf<bufsize, charT, traits>::Netbuf (StreamBase &stream)
116 : stream_(stream)
117{
118 setp(putbuf_, putbuf_ + bufsize);
119 setg(getbuf_+PUTBACK_SIZE, getbuf_+PUTBACK_SIZE, getbuf_+PUTBACK_SIZE);
120}
121//#############################################################################
122template<std::streamsize bufsize, class charT, class traits>
123Netbuf<bufsize, charT, traits>::~Netbuf (void) {
124 sync();
125}
126//#############################################################################
127template<std::streamsize bufsize, class charT, class traits>
128typename Netbuf<bufsize, charT, traits>::int_type Netbuf<bufsize, charT, traits>::overflow (int_type c) {
129 if (buffer_out() < 0) {
130return traits_type::eof();
131 } else if (!traits_type::eq_int_type(c, traits_type::eof())) {
132return sputc(c);
133 } else {
134return traits_type::not_eof(c);
135 }
136}
137//#############################################################################
138template<std::streamsize bufsize, class charT, class traits>
139int Netbuf<bufsize, charT, traits>::sync (void) {
140 return buffer_out();
141}
142//#############################################################################
143template<std::streamsize bufsize, class charT, class traits>
144int Netbuf<bufsize, charT, traits>::buffer_out (void) {
145 int length = pptr() - pbase();
146 int rc = stream_.write(putbuf_, length);
147 pbump(-length);
148 return rc;
149}
150//#############################################################################
151template<std::streamsize bufsize, class charT, class traits>
152typename Netbuf<bufsize, charT, traits>::int_type Netbuf<bufsize, charT, traits>::underflow (void) {
153 if (gptr() < egptr()) return traits_type::to_int_type(*gptr());
154 if (buffer_in() < 0) return traits_type::eof();
155 else return traits_type::to_int_type(*gptr());
156}
157//#############################################################################
158template<std::streamsize bufsize, class charT, class traits>
159typename Netbuf<bufsize, charT, traits>::int_type Netbuf<bufsize, charT, traits>::pbackfail(int_type c) {
160 if (gptr() != eback()) {
161gbump(-1);
162
163if (!traits_type::eq_int_type(c, traits_type::eof())) {
164 *(gptr()) = traits_type::to_char_type(c);
165}
166
167return traits_type::not_eof(c);
168 } else {
169return traits_type::eof();
170 }
171}
172//#############################################################################
173template<std::streamsize bufsize, class charT, class traits>
174int Netbuf<bufsize, charT, traits>::buffer_in (void) {
175 std::streamsize number_putbacks = std::min(gptr() - eback(), PUTBACK_SIZE);
176 std::memcpy(getbuf_ + (PUTBACK_SIZE - number_putbacks) * sizeof(char_type),
177 gptr() - number_putbacks * sizeof(char_type), number_putbacks * sizeof(char_type));
178
179 int rc = stream_.read(getbuf_ + PUTBACK_SIZE * sizeof(char_type), bufsize - PUTBACK_SIZE);
180
181 if (rc <= 0) {
182setg(0, 0, 0);
183return -1;
184 } else {
185setg(getbuf_ + PUTBACK_SIZE - number_putbacks, getbuf_ + PUTBACK_SIZE, getbuf_ + PUTBACK_SIZE + rc);
186return rc;
187 }
188}
189//#############################################################################
190
191} // end Netxx namespace
192#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status