menu: added new Keywords tag to .desktop files
[barry.git] / src / m_raw_channel.cc
blobb8a802f04d28ce3912742b72f9ebcf30e827a727
1 ///
2 /// \file m_raw_channel.cc
3 /// Mode class for a raw channel
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 #include "i18n.h"
24 #include "m_raw_channel.h"
25 #include "semaphore.h"
26 #include "data.h"
27 #include "protocol.h"
28 #include "protostructs.h"
29 #include "packet.h"
30 #include "endian.h"
31 #include "error.h"
32 #include "usbwrap.h"
33 #include "controller.h"
34 #include <stdexcept>
35 #include <sstream>
36 #include <cstring>
37 #include <string>
38 #include "protostructs.h"
40 #include "debug.h"
42 namespace Barry { namespace Mode {
44 ///////////////////////////////////////////////////////////////////////////////
45 // RawChannel SocketDataHandler callback class for data socket
46 class RawChannelSocketHandler: public SocketRoutingQueue::SocketDataHandler
48 RawChannel &m_raw_channel;
49 public:
50 RawChannelSocketHandler(RawChannel &raw_channel)
51 : m_raw_channel(raw_channel)
53 virtual void DataReceived(Data &data)
55 m_raw_channel.HandleReceivedData(data);
57 virtual void Error(Barry::Error &error)
59 SocketDataHandler::Error(error);
60 m_raw_channel.HandleError(error);
62 virtual ~RawChannelSocketHandler()
66 ///////////////////////////////////////////////////////////////////////////////
67 // RawChannel SocketDataHandler callback class for zero socket
68 class RawChannelZeroSocketHandler: public SocketRoutingQueue::SocketDataHandler
70 RawChannel &m_raw_channel;
71 public:
72 RawChannelZeroSocketHandler(RawChannel &raw_channel)
73 : m_raw_channel(raw_channel)
75 virtual void DataReceived(Data &data)
77 m_raw_channel.HandleReceivedZeroPacket(data);
79 virtual void Error(Barry::Error &error)
81 SocketDataHandler::Error(error);
82 m_raw_channel.HandleError(error);
84 virtual ~RawChannelZeroSocketHandler()
88 ///////////////////////////////////////////////////////////////////////////////
89 // RawChannel Mode class
91 RawChannel::RawChannel(Controller &con, RawChannelDataCallback &callback)
92 : Mode(con, Controller::RawChannel)
93 , m_callback(&callback)
94 , m_send_buffer(NULL)
95 , m_zero_registered(false)
96 , m_pending_error(NULL)
98 CheckQueueAvailable();
99 InitBuffer();
102 RawChannel::RawChannel(Controller &con)
103 : Mode(con, Controller::RawChannel)
104 , m_callback(NULL)
105 , m_send_buffer(NULL)
106 , m_zero_registered(false)
107 , m_pending_error(NULL)
109 CheckQueueAvailable();
110 InitBuffer();
113 void RawChannel::CheckQueueAvailable()
115 if( !m_con.HasQueue() ) {
116 throw Barry::Error(_("RawChannel: No routing queue set in controller"));
120 void RawChannel::InitBuffer()
122 m_send_buffer = new unsigned char[SB_CHANNELPACKET_HEADER_SIZE + SB_CHANNELPACKET_MAX_DATA_SIZE];
125 RawChannel::~RawChannel()
127 UnregisterZeroSocketInterest();
129 delete[] m_send_buffer;
131 delete m_pending_error;
134 void RawChannel::OnOpen()
136 // Enable sequence packets so that DataSendAck callback and close can be
137 // implemented
138 m_zero_registered = true;
139 SocketRoutingQueue::SocketDataHandlerPtr zeroCallback;
140 zeroCallback.reset(new RawChannelZeroSocketHandler(*this));
141 m_con.GetQueue()->RegisterInterest(0, zeroCallback);
144 SocketRoutingQueue::SocketDataHandlerPtr RawChannel::GetHandler()
146 // Get socket data packets routed to this class as well if a
147 // callback was provided, otherside just get the data packets
148 // placed into a queue for the socket.
149 if( m_callback ) {
150 SocketRoutingQueue::SocketDataHandlerPtr ret;
151 ret.reset(new RawChannelSocketHandler(*this));
152 return ret;
154 else {
155 return Mode::GetHandler();
160 void RawChannel::HandleReceivedZeroPacket(Data &data)
162 Protocol::CheckSize(data, SB_PACKET_HEADER_SIZE);
163 MAKE_PACKETPTR_BUF(packet, data.GetData());
165 if( packet->socket != 0 ) {
166 UnregisterZeroSocketInterest();
167 SetPendingError(_("RawChannel: Got packet not for socket-zero"));
170 switch( btohs(packet->command) )
172 case SB_COMMAND_CLOSE_SOCKET:
173 case SB_COMMAND_REMOTE_CLOSE_SOCKET:
174 // Stop listening to socket 0 messages
175 // so that socket close work.
176 UnregisterZeroSocketInterest();
177 if( m_callback ) {
178 m_callback->ChannelClose();
181 break;
182 default:
183 UnregisterZeroSocketInterest();
184 if( m_callback ) {
185 m_callback->ChannelError(_("RawChannel: Got unexpected socket zero packet"));
187 else {
188 SetPendingError(_("RawChannel: Got unexpected socket zero packet"));
190 break;
195 void RawChannel::HandleReceivedData(Data &data)
197 // Only ever called in callback mode
198 ValidateDataPacket(data);
199 MAKE_CHANNELPACKETPTR_BUF(packet, data.GetData());
201 // Should be a socket packet for us, so remove packet headers
202 Data partial(packet->u.data, data.GetSize() - SB_CHANNELPACKET_HEADER_SIZE);
203 if( m_callback ) {
204 m_callback->DataReceived(partial);
206 else {
207 SetPendingError(_("RawChannel: Received data to handle when in non-callback mode"));
211 void RawChannel::HandleError(Barry::Error &error)
213 std::ostringstream errorOss;
214 errorOss << _("RawChannel: Socket error received, what: ") << error.what();
216 if( m_callback ) {
217 m_callback->ChannelError(errorOss.str().c_str());
219 else {
220 SetPendingError(errorOss.str().c_str());
224 void RawChannel::UnregisterZeroSocketInterest()
226 if( m_zero_registered ) {
227 m_con.GetQueue()->UnregisterInterest(0);
228 m_zero_registered = false;
232 void RawChannel::SetPendingError(const char *msg)
234 if( !m_pending_error ) {
235 m_pending_error = new std::string(msg);
239 ///////////////////////////////////////////////////////////////////////////////
240 // public API
242 void RawChannel::Send(Data &data, int timeout)
244 size_t packetSize = SB_CHANNELPACKET_HEADER_SIZE + data.GetSize();
246 if( packetSize > SB_CHANNELPACKET_HEADER_SIZE + SB_CHANNELPACKET_MAX_DATA_SIZE ) {
247 throw Barry::Error(_("RawChannel: send data size larger than MaximumPacketSize"));
250 if( m_pending_error ) {
251 throw Barry::Error(*m_pending_error);
254 // setup header and copy data in
255 MAKE_CHANNELPACKETPTR_BUF(packet, m_send_buffer);
256 packet->size = htobs(packetSize);
257 std::memcpy(packet->u.data, data.GetData(), data.GetSize());
259 Data toSend(m_send_buffer, packetSize);
260 m_socket->SyncSend(toSend, timeout);
262 if( m_pending_error ) {
263 throw Barry::Error(*m_pending_error);
267 void RawChannel::Receive(Data &data,int timeout)
269 if( m_callback ) {
270 throw std::logic_error(_("RawChannel: Receive called when channel was created with a callback"));
273 if( m_pending_error ) {
274 throw Barry::Error(*m_pending_error);
277 // Receive into a buffer
278 m_socket->Receive(m_receive_data, timeout);
279 // Then transfer across, skipping the header
280 ValidateDataPacket(m_receive_data);
281 MAKE_CHANNELPACKETPTR_BUF(packet, m_receive_data.GetData());
283 size_t len = packet->size - SB_CHANNELPACKET_HEADER_SIZE;
284 memcpy(data.GetBuffer(), packet->u.data, len);
285 data.ReleaseBuffer(len);
289 void RawChannel::ValidateDataPacket(Data &data)
291 Protocol::CheckSize(data, SB_CHANNELPACKET_HEADER_SIZE);
292 MAKE_CHANNELPACKETPTR_BUF(packet, data.GetData());
293 if( packet->size != data.GetSize() ) {
295 throw std::logic_error(_("RawChannel: Data size doesn't match packet size"));
299 size_t RawChannel::MaximumSendSize()
301 return SB_CHANNELPACKET_MAX_DATA_SIZE;
304 }} // namespace Barry::Mode