Moving semaphore code to inside raw_channel so that clients don't have to care about it.
[barry.git] / src / m_raw_channel.h
blob41317f0de2c69f33fbfb7ca8e9e77e5e02012d76
1 ///
2 /// \file m_raw_channel.h
3 /// Mode class for a raw channel
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
8 Portions Copyright (C) 2010 RealVNC Ltd.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #ifndef __BARRY_M_RAW_CHANNEL_H__
24 #define __BARRY_M_RAW_CHANNEL_H__
26 #include "dll.h"
27 #include "m_mode_base.h"
28 #include "socket.h"
30 #include <string>
31 #include <pthread.h>
33 namespace Barry {
35 class semaphore;
37 namespace Mode {
39 // Callback from the raw channel.
41 class BXEXPORT RawChannelDataCallback
43 public:
44 // Called when data has been received on the channel
45 virtual void DataReceived(Data& data) = 0;
46 // Called when the channel has an error
47 virtual void ChannelError(std::string msg) = 0;
48 // Called when the channel has been asked to close by the other side
49 virtual void ChannelClose() = 0;
53 // Raw channel class
55 /// The main class for creating a raw channel session.
56 ///
57 /// To use this class, use the following steps:
58 ///
59 /// - Implement RawChannelDataCallback
60 /// - Create a Controller object (see Controller class for more details)
61 /// - Create this Mode::RawChannel object, passing in the Controller
62 /// object during construction
63 /// - Call Open() to open the channel and finish constructing.
64 /// - Call GetData() to fetch data
65 /// - Call SendData() to send data
66 ///
67 class BXEXPORT RawChannel : public Mode
69 // Mutex for signalling between read and write threads
70 pthread_mutex_t m_mutex;
71 bool m_mutex_valid;
72 // Condvar for signalling between read and write threads
73 pthread_cond_t m_cv;
74 bool m_cv_valid;
76 semaphore* m_semaphore;
77 RawChannelDataCallback& m_callback;
78 unsigned char *m_sendBuffer;
79 bool m_zeroRegistered;
81 protected:
82 void UnregisterZeroSocketInterest();
84 public:
85 RawChannel(Controller &con, RawChannelDataCallback& callback);
86 virtual ~RawChannel();
88 //////////////////////////////////
89 // Raw channel mode specific methods
91 // Send some data on the raw channel.
92 // Will throw a Barry::Error if data is longer than
93 // MaximumPacketContentsSize or a Barry::Usb::Error if there
94 // is an underlying USB error.
95 void Send(Data& data, int timeout = -1);
97 // Returns the maximum quantity of data which
98 // can be sent
99 size_t MaximumSendSize();
101 // Not intended for use by users of this class.
102 // Instead data received will come in via the
103 // RawChannelDataCallback::DataReceived callback.
104 void HandleReceivedData(Data& data);
106 // Not intended for use by users of this class.
107 // This method is called by the internals of
108 // Barry when setting up a connection.
109 void OnOpen();
112 }} // namespace Barry::Mode
114 #endif