aboutsummaryrefslogtreecommitdiff
path: root/src/stable/clientbuf.h
blob: 86d176d3f1e13fe49274e2aabb48832b37c6e637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_CLIENT_BUF_H
#define BU_CLIENT_BUF_H

#include "bu/stream.h"
#include "bu/streamstack.h"
#include "bu/queuebuf.h"
#include "bu/mutex.h"

namespace Bu
{
    /**
     * ClientBuf represents two buffered I/O channels, input and output. Each
     * is stored in its own Bu::QueueBuf. One side is client oriented and can
     * have filters applied ala Bu::StreamStack. The other is server oriented
     * and is accesesed raw. Both must be accessed via accessor objects.
     *
     * Direct access to the buffers isn't allowed, instead you must select a
     * perspective. client() provides an accessor where reading consumes data
     * coming from the socket and writing provides data to write to the socket.
     * server() works in reverse, writing is writing data from the socket that
     * we have just read, reading is reading data that should be written to the
     * socket.
     *
     * client() provides a filtered access view of the data, while server() is
     * unfiltered.
     */
    class ClientBuf
    {
    public:
        class ClientAccess : public Bu::Stream
        {
        friend class Bu::ClientBuf;
        private:
            ClientAccess( Bu::ClientBuf &rBuf );
        
        public:
            virtual ~ClientAccess();
        
            virtual void close();
            virtual size read( void *pBuf, size iBytes );
            virtual size peek( void *pData, int iBytes, int iOffset=0 );
            virtual size write( const void *pBuf, size iBytes );
            using Stream::write;
            virtual size tell();
            virtual void seek( size offset );
            virtual void setPos( size pos );
            virtual void setPosEnd( size pos );
            virtual bool isEos();
            virtual bool isOpen();
            virtual void flush();
            virtual bool canRead();
            virtual bool canWrite();
            virtual bool isReadable();
            virtual bool isWritable();
            virtual bool isSeekable();
            virtual bool isBlocking();
            virtual void setBlocking( bool bBlocking=true );
            virtual void setSize( size iSize );
            virtual size getSize() const;
            virtual size getBlockSize() const;
            virtual Bu::String getLocation() const;

        private:
            Bu::ClientBuf &rBuf;
            Bu::QueueBuf qbPeek;
            mutable Bu::Mutex mAccess;
        };

        class ServerAccess : public Bu::Stream
        {
        friend class Bu::ClientBuf;
        private:
            ServerAccess( Bu::ClientBuf &rBuf );
        
        public:
            virtual ~ServerAccess();
            
            virtual void close();
            virtual size read( void *pBuf, size iBytes );
            virtual size peek( void *pData, int iBytes, int iOffset=0 );
            virtual size write( const void *pBuf, size iBytes );
            using Stream::write;
            virtual size tell();
            virtual void seek( size offset );
            virtual void setPos( size pos );
            virtual void setPosEnd( size pos );
            virtual bool isEos();
            virtual bool isOpen();
            virtual void flush();
            virtual bool canRead();
            virtual bool canWrite();
            virtual bool isReadable();
            virtual bool isWritable();
            virtual bool isSeekable();
            virtual bool isBlocking();
            virtual void setBlocking( bool bBlocking=true );
            virtual void setSize( size iSize );
            virtual size getSize() const;
            virtual size getBlockSize() const;
            virtual Bu::String getLocation() const;

        private:
            Bu::ClientBuf &rBuf;
        };
        
    private:
        class ClientAccessRaw : public Bu::Stream
        {
        friend class Bu::ClientBuf;
        private:
            ClientAccessRaw( Bu::ClientBuf &rBuf );
        
        public:
            virtual ~ClientAccessRaw();
        
            virtual void close();
            virtual size read( void *pBuf, size iBytes );
            virtual size write( const void *pBuf, size iBytes );
            using Stream::write;
            virtual size tell();
            virtual void seek( size offset );
            virtual void setPos( size pos );
            virtual void setPosEnd( size pos );
            virtual bool isEos();
            virtual bool isOpen();
            virtual void flush();
            virtual bool canRead();
            virtual bool canWrite();
            virtual bool isReadable();
            virtual bool isWritable();
            virtual bool isSeekable();
            virtual bool isBlocking();
            virtual void setBlocking( bool bBlocking=true );
            virtual void setSize( size iSize );
            virtual size getSize() const;
            virtual size getBlockSize() const;
            virtual Bu::String getLocation() const;

        private:
            Bu::ClientBuf &rBuf;
        };

    public:
        ClientBuf();
        virtual ~ClientBuf();

        /**
         * Access the client access side. Use directly, do NOT copy or store.
         */
        ClientAccess &client();

        /**
         * Access the server access side. Use directly, do NOT copy or store.
         */
        ServerAccess &server();

    private:
        ClientAccessRaw accClientRaw;
        ServerAccess accServer;
        //Bu::StreamStack accClientFiltered;
        ClientAccess accClient;
        Bu::QueueBuf qbOutput;
        Bu::QueueBuf qbInput;
        Bu::Mutex mOutput;
        Bu::Mutex mInput;

    friend class Bu::ClientBuf::ClientAccess;
    friend class Bu::ClientBuf::ClientAccessRaw;
    friend class Bu::ClientBuf::ServerAccess;
    };
}

#endif