00001
00002
00003 #ifndef ACE_BUFFERED_STREAM_BUFFER_CPP
00004 #define ACE_BUFFERED_STREAM_BUFFER_CPP
00005
00006 #include "ace/INet/BufferedStreamBuffer.h"
00007 #include "ace/OS_Memory.h"
00008 #include "ace/OS_NS_string.h"
00009
00010 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00011
00012 namespace ACE
00013 {
00014 namespace IOS
00015 {
00016
00017 template <class ACE_CHAR_T, class TR>
00018 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::BasicBufferedStreamBuffer (
00019 std::streamsize bufsz,
00020 typename std::basic_ios<ACE_CHAR_T, TR>::openmode mode)
00021 : bufsize_ (bufsz),
00022 mode_ (mode),
00023 interceptor_ (0)
00024 {
00025 char_type* p = 0;
00026 ACE_NEW_NORETURN (p, char_type [bufsz]);
00027 this->buffer_.reset (p);
00028
00029 this->setg (this->buffer_.get () + 4,
00030 this->buffer_.get () + 4,
00031 this->buffer_.get () + 4);
00032 this->setp (this->buffer_.get (),
00033 this->buffer_.get () + (this->bufsize_ - 1));
00034 }
00035
00036 template <class ACE_CHAR_T, class TR>
00037 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::~BasicBufferedStreamBuffer ()
00038 {
00039 }
00040
00041 template <class ACE_CHAR_T, class TR>
00042 typename BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::int_type
00043 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::overflow (int_type c)
00044 {
00045 if (!(this->mode_ & ios_type::out)) return char_traits::eof ();
00046
00047 if (c != char_traits::eof ())
00048 {
00049 *this->pptr () = char_traits::to_char_type (c);
00050 this->pbump (1);
00051 }
00052 if (this->flush_buffer () == std::streamsize (-1)) return char_traits::eof ();
00053
00054 return c;
00055 }
00056
00057 template <class ACE_CHAR_T, class TR>
00058 typename BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::int_type
00059 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::underflow ()
00060 {
00061 if (!(this->mode_ & ios_type::in)) return char_traits::eof ();
00062
00063 if (this->gptr () && (this->gptr () < this->egptr ()))
00064 return char_traits::to_int_type (*this->gptr ());
00065
00066 int putback = int (this->gptr () - this->eback ());
00067 if (putback > 4) putback = 4;
00068
00069 ACE_OS::memmove (this->buffer_.get () + (4 - putback),
00070 this->gptr (),
00071 putback * sizeof (char_type));
00072
00073 if (this->interceptor_)
00074 this->interceptor_->before_read (this->bufsize_ - 4);
00075
00076 int n = this->read_from_stream (this->buffer_.get () + 4,
00077 this->bufsize_ - 4);
00078
00079 if (this->interceptor_)
00080 this->interceptor_->after_read (this->buffer_.get () + 4, n);
00081
00082 if (n <= 0)
00083 {
00084 if (this->interceptor_)
00085 this->interceptor_->on_eof ();
00086
00087 return char_traits::eof ();
00088 }
00089
00090 this->setg (this->buffer_.get () + (4 - putback),
00091 this->buffer_.get () + 4,
00092 this->buffer_.get () + 4 + n);
00093
00094
00095 return char_traits::to_int_type (*this->gptr ());
00096 }
00097
00098 template <class ACE_CHAR_T, class TR>
00099 int
00100 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::sync ()
00101 {
00102 if (this->pptr () && this->pptr () > this->pbase ())
00103 {
00104 if (this->flush_buffer () == -1) return -1;
00105 }
00106 return 0;
00107 }
00108
00109 template <class ACE_CHAR_T, class TR>
00110 void
00111 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::set_interceptor (interceptor_type& interceptor)
00112 {
00113 this->interceptor_ = &interceptor;
00114 }
00115
00116 template <class ACE_CHAR_T, class TR>
00117 void
00118 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::set_mode (
00119 typename std::basic_ios<ACE_CHAR_T, TR>::openmode mode)
00120 {
00121 this->mode_ = mode;
00122 }
00123
00124 template <class ACE_CHAR_T, class TR>
00125 typename std::basic_ios<ACE_CHAR_T, TR>::openmode
00126 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::get_mode () const
00127 {
00128 return this->mode_;
00129 }
00130
00131 template <class ACE_CHAR_T, class TR>
00132 int
00133 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::read_from_stream (char_type* , std::streamsize )
00134 {
00135
00136 return 0;
00137 }
00138
00139 template <class ACE_CHAR_T, class TR>
00140 int
00141 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::write_to_stream (const char_type* , std::streamsize )
00142 {
00143
00144 return 0;
00145 }
00146
00147 template <class ACE_CHAR_T, class TR>
00148 void
00149 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::reset_buffers()
00150 {
00151 this->setg (this->buffer_.get () + 4,
00152 this->buffer_.get () + 4,
00153 this->buffer_.get () + 4);
00154 this->setp (this->buffer_.get (),
00155 this->buffer_.get () + (this->bufsize_ - 1));
00156 }
00157
00158 template <class ACE_CHAR_T, class TR>
00159 int
00160 BasicBufferedStreamBuffer<ACE_CHAR_T, TR>::flush_buffer ()
00161 {
00162 int n = int (this->pptr () - this->pbase ());
00163
00164 if (this->interceptor_)
00165 this->interceptor_->before_write (this->pbase (), n);
00166
00167 int n_out = this->write_to_stream (this->pbase (), n);
00168
00169 if (this->interceptor_)
00170 this->interceptor_->after_write (n_out);
00171
00172 if (n_out == n)
00173 {
00174 this->pbump (-n);
00175 return n;
00176 }
00177 return -1;
00178 }
00179 }
00180 }
00181
00182 ACE_END_VERSIONED_NAMESPACE_DECL
00183
00184 #endif