Vault  4.1
vsocketstream.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved.
00003 This file is part of the Code Vault version 4.1
00004 http://www.bombaydigital.com/
00005 License: MIT. See LICENSE.md in the Vault top level directory.
00006 */
00007 
00010 #include "vsocketstream.h"
00011 
00012 #include "vsocket.h"
00013 #include "vexception.h"
00014 
00015 VSocketStream::VSocketStream(const VString& name)
00016     : VStream(name)
00017     , mSocket(NULL)
00018     {
00019 }
00020 
00021 VSocketStream::VSocketStream(VSocket* socket, const VString& name)
00022     : VStream(name)
00023     , mSocket(socket)
00024     {
00025 }
00026 
00027 VSocketStream::VSocketStream(const VSocketStream& other)
00028     : VStream(VSTRING_FORMAT("%s copy", other.getName().chars()))
00029     , mSocket(other.mSocket)
00030     {
00031 }
00032 
00033 VSocketStream& VSocketStream::operator=(const VSocketStream& other) {
00034     mName = other.mName;
00035     mSocket = other.mSocket;
00036     return *this;
00037 }
00038 
00039 VSocket* VSocketStream::getSocket() const {
00040     return mSocket;
00041 }
00042 
00043 void VSocketStream::setSocket(VSocket* socket) {
00044     mSocket = socket;
00045 }
00046 
00047 Vs64 VSocketStream::read(Vu8* targetBuffer, Vs64 numBytesToRead) {
00048     return mSocket->read(targetBuffer, static_cast<int>(numBytesToRead));
00049 }
00050 
00051 Vs64 VSocketStream::write(const Vu8* buffer, Vs64 numBytesToWrite) {
00052     return mSocket->write(buffer, static_cast<int>(numBytesToWrite));
00053 }
00054 
00055 void VSocketStream::flush() {
00056     mSocket->flush();
00057 }
00058 
00059 bool VSocketStream::skip(Vs64 numBytesToSkip) {
00060     /*
00061     It seems like we could be a little more efficient either by
00062     reading in larger blocks if the skip amount is big enough.
00063     For now, just read individual bytes (it's probably being
00064     buffered anyway).
00065     */
00066     Vu8 aByte;
00067 
00068     for (Vs64 i = 0; i < numBytesToSkip; ++i) {
00069         this->read(&aByte, 1);
00070     }
00071 
00072     return true;
00073 }
00074 
00075 bool VSocketStream::seek(Vs64 offset, int whence) {
00076     if ((whence != SEEK_CUR) || (offset < 0)) {
00077         throw VStackTraceException("VSocketStream::seek received unsupported seek type.");
00078     }
00079 
00080     return this->skip(offset);
00081 }
00082 
00083 Vs64 VSocketStream::getIOOffset() const {
00084     return mSocket->numBytesRead();
00085 }
00086 
00087 Vs64 VSocketStream::available() const {
00088     return mSocket->available();
00089 }
00090 

Copyright ©1997-2014 Trygve Isaacson. All rights reserved. This documentation was generated with Doxygen.