1 /*
2  * synergy -- mouse and keyboard sharing utility
3  * Copyright (C) 2012-2016 Symless Ltd.
4  * Copyright (C) 2004 Chris Schoeneman
5  *
6  * This package is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * found in the file LICENSE that should have accompanied this file.
9  *
10  * This package is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include "io/StreamFilter.h"
22 #include "io/StreamBuffer.h"
23 #include "mt/Mutex.h"
24 
25 class IEventQueue;
26 
27 //! Packetizing stream filter
28 /*!
29 Filters a stream to read and write packets.
30 */
31 class PacketStreamFilter : public StreamFilter {
32 public:
33     PacketStreamFilter(IEventQueue* events, synergy::IStream* stream, bool adoptStream = true);
34     ~PacketStreamFilter();
35 
36     // IStream overrides
37     virtual void        close();
38     virtual UInt32        read(void* buffer, UInt32 n);
39     virtual void        write(const void* buffer, UInt32 n);
40     virtual void        shutdownInput();
41     virtual bool        isReady() const;
42     virtual UInt32        getSize() const;
43 
44 protected:
45     // StreamFilter overrides
46     virtual void        filterEvent(const Event&);
47 
48 private:
49     bool                isReadyNoLock() const;
50     void                readPacketSize();
51     bool                readMore();
52 
53 private:
54     Mutex                m_mutex;
55     UInt32                m_size;
56     StreamBuffer        m_buffer;
57     bool                m_inputShutdown;
58     IEventQueue*        m_events;
59 };
60