00001
00002
00003 #ifndef ACE_IOS_BIDIR_STREAM_BUFFER_CPP
00004 #define ACE_IOS_BIDIR_STREAM_BUFFER_CPP
00005
00006 #include "ace/INet/BidirStreamBuffer.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 STREAM_HANDLER, class TR>
00018 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::BasicBidirStreamBuffer (
00019 STREAM_HANDLER* sh,
00020 std::streamsize bufsz,
00021 openmode mode)
00022 : bufsize_ (bufsz),
00023 mode_ (mode),
00024 stream_ (sh),
00025 interceptor_ (0)
00026 {
00027 this->stream_->add_reference ();
00028
00029 char_type* p = 0;
00030 ACE_NEW_NORETURN (p, char_type [bufsz]);
00031 this->read_buffer_.reset (p);
00032 p = 0;
00033 ACE_NEW_NORETURN (p, char_type [bufsz]);
00034 this->write_buffer_.reset (p);
00035
00036 this->reset_buffers ();
00037 }
00038
00039 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00040 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::~BasicBidirStreamBuffer ()
00041 {
00042 this->close_stream ();
00043 }
00044
00045 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00046 typename BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::int_type
00047 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::overflow (int_type c)
00048 {
00049 if (!(this->mode_ & ios_type::out)) return char_traits::eof ();
00050
00051 if (c != char_traits::eof ())
00052 {
00053 *this->pptr () = char_traits::to_char_type (c);
00054 this->pbump (1);
00055 }
00056 if (this->flush_buffer () == std::streamsize (-1)) return char_traits::eof ();
00057
00058 return c;
00059 }
00060
00061 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00062 typename BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::int_type
00063 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::underflow ()
00064 {
00065 if (!(this->mode_ & ios_type::in)) return char_traits::eof ();
00066
00067 if (this->gptr () && (this->gptr () < this->egptr ()))
00068 return char_traits::to_int_type (*this->gptr ());
00069
00070 int putback = int (this->gptr () - this->eback ());
00071 if (putback > 4) putback = 4;
00072
00073 ACE_OS::memmove (this->read_buffer_.get () + (4 - putback),
00074 this->gptr () - putback,
00075 putback * sizeof (char_type));
00076
00077 if (this->interceptor_)
00078 this->interceptor_->before_read (this->bufsize_ - 4);
00079
00080 int n = this->read_from_stream (this->read_buffer_.get () + 4,
00081 this->bufsize_ - 4);
00082
00083 if (this->interceptor_)
00084 this->interceptor_->after_read (this->read_buffer_.get () + 4, n);
00085
00086 if (n <= 0)
00087 {
00088 if (this->interceptor_)
00089 this->interceptor_->on_eof ();
00090
00091 return char_traits::eof ();
00092 }
00093
00094 this->setg (this->read_buffer_.get () + (4 - putback),
00095 this->read_buffer_.get () + 4,
00096 this->read_buffer_.get () + 4 + n);
00097
00098
00099 return char_traits::to_int_type (*this->gptr ());
00100 }
00101
00102 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00103 int
00104 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::sync ()
00105 {
00106 if (this->pptr () && this->pptr () > this->pbase ())
00107 {
00108 if (this->flush_buffer () == -1) return -1;
00109 }
00110 return 0;
00111 }
00112
00113 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00114 const STREAM_HANDLER&
00115 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::stream () const
00116 {
00117 return *this->stream_;
00118 }
00119
00120 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00121 void
00122 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::close_stream ()
00123 {
00124 if (this->stream_ != 0)
00125 {
00126 ACE_Errno_Guard eguard (errno);
00127 this->stream_->remove_reference ();
00128 this->stream_ = 0;
00129 }
00130 }
00131
00132 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00133 void
00134 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::set_interceptor (interceptor_type& interceptor)
00135 {
00136 this->interceptor_ = &interceptor;
00137 }
00138
00139 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00140 void
00141 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::set_mode (openmode mode)
00142 {
00143 this->mode_ = mode;
00144 }
00145
00146 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00147 typename BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::openmode
00148 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::get_mode () const
00149 {
00150 return this->mode_;
00151 }
00152
00153 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00154 void
00155 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::reset_buffers()
00156 {
00157 this->setg (this->read_buffer_.get () + 4,
00158 this->read_buffer_.get () + 4,
00159 this->read_buffer_.get () + 4);
00160 this->setp (this->write_buffer_.get (),
00161 this->write_buffer_.get () + (this->bufsize_ - 1));
00162 }
00163
00164 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00165 int
00166 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::read_from_stream (char_type* buffer, std::streamsize length)
00167 {
00168 return this->stream_ == 0 ? 0 : this->stream_->read_from_stream (buffer, length, sizeof(char_type));
00169 }
00170
00171 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00172 int
00173 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::write_to_stream (const char_type* buffer, std::streamsize length)
00174 {
00175 return this->stream_ == 0 ? 0 : this->stream_->write_to_stream (buffer, length, sizeof(char_type));
00176 }
00177
00178 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
00179 int
00180 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::flush_buffer ()
00181 {
00182 int n = int (this->pptr () - this->pbase ());
00183
00184 if (this->interceptor_)
00185 this->interceptor_->before_write (this->pbase (), n);
00186
00187 int n_out = this->write_to_stream (this->pbase (), n);
00188
00189 if (this->interceptor_)
00190 this->interceptor_->after_write (n_out);
00191
00192 if (n_out == n)
00193 {
00194 this->pbump (-n);
00195 return n;
00196 }
00197 return -1;
00198 }
00199
00200 template <class STREAM_HANDLER>
00201 BidirStreamBuffer<STREAM_HANDLER>::BidirStreamBuffer (
00202 STREAM_HANDLER* sh,
00203 std::streamsize bufsz,
00204 openmode mode)
00205 : BasicBidirStreamBuffer<char, STREAM_HANDLER> (sh, bufsz, mode)
00206 {
00207 }
00208
00209 template <class STREAM_HANDLER>
00210 BidirStreamBuffer<STREAM_HANDLER>::~BidirStreamBuffer ()
00211 {
00212 }
00213
00214 }
00215 }
00216
00217 ACE_END_VERSIONED_NAMESPACE_DECL
00218
00219 #endif