library of assembled shared sources

http://lass.cocamware.com

binary_o_socket.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
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 // --- public --------------------------------------------------------------------------------------
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 // --- private -------------------------------------------------------------------------------------
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 /** write a buffer of bytes to the stream
00105  *  @par iIn pointer to buffer.
00106  *  @par iBufferLength length of buffer in bytes.
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 // EOF

Generated on Mon Nov 10 14:20:00 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo