aboutsummaryrefslogtreecommitdiff
path: root/src/stable/client.h
blob: 119e2f70672a96d3cdf3d0c9a8e88e84df498e0c (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
/*
 * Copyright (C) 2007-2019 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_H
#define BU_CLIENT_H

#include <stdint.h>

#include "bu/config.h"
#include "bu/string.h"
#include "bu/queuebuf.h"
#include "bu/mutex.h"
#include "bu/readwritemutex.h"

namespace Bu
{
    class Protocol;
    class Stream;
    class TcpSocket;
    class ClientLinkFactory;

    /**
     *@ingroup Serving
     */
    class Client : public Bu::Stream
    {
    public:
        Client( Bu::TcpSocket *pSocket, Bu::ClientLinkFactory *pfLink );
        virtual ~Client();

        void processInput();
        void processOutput();

        //Bu::String &getInput();
        //Bu::String &getOutput();
        Bu::size write( const Bu::String &sData );
        Bu::size write( const void *pData, Bu::size nBytes );
        Bu::size write( int8_t nData );
        Bu::size write( int16_t nData );
        Bu::size write( int32_t nData );
        Bu::size write( int64_t nData );
        Bu::size write( uint8_t nData );
        Bu::size write( uint16_t nData );
        Bu::size write( uint32_t nData );
        Bu::size write( uint64_t nData );
        Bu::size read( void *pData, Bu::size nBytes );
        Bu::size peek( void *pData, int nBytes, int nOffset=0 );
//      void seek( int nBytes );
        Bu::size getInputSize();
        Bu::size getOutputSize();

        void setProtocol( Protocol *pProto );
        Bu::Protocol *getProtocol();
        void clearProtocol();

        bool isOpen();
        void close();
        void tick();

        const Bu::TcpSocket *getSocket() const;

        void disconnect();
        bool wantsDisconnect();

        class ClientLink *getLink();

        void onMessage( const Bu::String &sMsg );

        bool hasOutput() { return qbWrite.getSize() > 0; }
        bool hasInput() { return qbRead.getSize() > 0; }

        template<typename filter>
        void pushFilter()
        {
            filter *pFlt = new filter( *pTopStream );
            pTopStream = pFlt;
            lFilts.prepend( pFlt );
        }
        
        template<typename filter, typename p1t>
        void pushFilter( p1t p1 )
        {
            filter *pFlt = new filter( *pTopStream, p1 );
            pTopStream = pFlt;
            lFilts.prepend( pFlt );
        }
        
        template<typename filter, typename p1t, typename p2t>
        void pushFilter( p1t p1, p2t p2 )
        {
            filter *pFlt = new filter( *pTopStream, p1, p2 );
            pTopStream = pFlt;
            lFilts.prepend( pFlt );
        }

        /*
         * These are required to qualify as a stream, I dunno how many will
         * be implemented.
         */
        virtual Bu::size tell();
        virtual void seek( Bu::size offset );
        virtual void setPos( Bu::size pos );
        virtual void setPosEnd( Bu::size pos );
        virtual bool isEos();
        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( Bu::size iSize );
        virtual size getSize() const;
        virtual size getBlockSize() const;
        virtual Bu::String getLocation() const;

    private:
        typedef Bu::List<Bu::Stream *> FilterList;
        FilterList lFilts;
        Bu::Stream *pTopStream;
        Bu::TcpSocket *pSocket;
        Bu::Protocol *pProto;
        Bu::QueueBuf qbRead;
        Bu::QueueBuf qbWrite;
        bool bWantsDisconnect;
        class Bu::ClientLinkFactory *pfLink;
        Bu::Mutex mRead;
        Bu::Mutex mWrite;
        mutable Bu::ReadWriteMutex mGlobal;
    };
}

#endif