binary_o_socket.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "io_common.h"
00044 #include "binary_o_socket.h"
00045 #include "socket.h"
00046 #include "../util/callback_0.h"
00047 #include "../util/thread_fun.h"
00048
00049 namespace lass
00050 {
00051 namespace io
00052 {
00053
00054
00055
00056 BinaryOSocket::BinaryOSocket(Socket& iSocket, size_t iBufferSize, unsigned long iFlushPeriod):
00057 BinaryOStream(),
00058 socket_(iSocket),
00059 buffer_(iBufferSize),
00060 bufferSize_(iBufferSize),
00061 current_(0),
00062 flushPeriod_(iFlushPeriod),
00063 stopFlushThread_(false)
00064 {
00065 flushThread_.reset(util::threadFun(
00066 util::makeCallback(this, &BinaryOSocket::flusher), util::threadJoinable));
00067 flushThread_->run();
00068 }
00069
00070
00071
00072 BinaryOSocket::~BinaryOSocket()
00073 {
00074 stopFlushThread_ = true;
00075 flushCondition_.signal();
00076 flushThread_->join();
00077 }
00078
00079
00080
00081
00082
00083 long BinaryOSocket::doTellp() const
00084 {
00085 LASS_THROW("no position in network streams");
00086 }
00087
00088
00089
00090 void BinaryOSocket::doSeekp(long iOffset, std::ios_base::seekdir iDirection)
00091 {
00092 LASS_THROW("no seeking in network streams");
00093 }
00094
00095
00096
00097 void BinaryOSocket::doFlush()
00098 {
00099 flushCondition_.signal();
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 void BinaryOSocket::doWrite(const void* iBegin, size_t iNumberOfBytes)
00109 {
00110 const char* begin = static_cast<const char*>(iBegin);
00111 while (iNumberOfBytes > 0)
00112 {
00113 LASS_LOCK(bufferLock_)
00114 {
00115 if (!good())
00116 {
00117 return;
00118 }
00119
00120 if (current_ < bufferSize_)
00121 {
00122 const size_t freeSize = bufferSize_ - current_;
00123 const size_t writeSize = std::min(iNumberOfBytes, freeSize);
00124
00125 ::memcpy(&buffer_[current_], begin, writeSize);
00126
00127 current_ += writeSize;
00128 if (current_ < bufferSize_)
00129 {
00130 LASS_ASSERT(writeSize == iNumberOfBytes);
00131 return;
00132 }
00133
00134 LASS_ASSERT(writeSize < iNumberOfBytes);
00135 begin += writeSize;
00136 iNumberOfBytes -= writeSize;
00137 }
00138 }
00139 flushCondition_.signal();
00140 }
00141 }
00142
00143
00144
00145 void BinaryOSocket::flusher()
00146 {
00147 while (true)
00148 {
00149 LASS_LOCK(bufferLock_)
00150 {
00151 if (current_ > 0)
00152 {
00153 const char* begin = &buffer_[0];
00154 int n = static_cast<int>(current_);
00155 LASS_ASSERT(n >= 0);
00156 while (n > 0)
00157 {
00158 try
00159 {
00160 const int sent = socket_.send(begin, static_cast<int>(current_));
00161 LASS_ASSERT(sent >= 0 && sent <= n);
00162 begin += sent;
00163 n -= sent;
00164 }
00165 catch (util::Exception&)
00166 {
00167 setstate(std::ios_base::badbit);
00168 }
00169 }
00170 current_ = 0;
00171 }
00172 if (stopFlushThread_)
00173 {
00174 return;
00175 }
00176 }
00177 flushCondition_.wait(flushPeriod_);
00178 }
00179 }
00180
00181 }
00182
00183 }
00184
00185