2 /// \file m_raw_channel.cc
3 /// Mode class for a raw channel
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 #include "m_raw_channel.h"
24 #include "semaphore.h"
27 #include "protostructs.h"
32 #include "controller.h"
37 #include "protostructs.h"
41 #define RAW_HEADER_SIZE 4
43 namespace Barry
{ namespace Mode
{
45 ///////////////////////////////////////////////////////////////////////////////
46 // RawChannel SocketDataHandler callback class
47 class RawChannelSocketHandler
: public SocketRoutingQueue::SocketDataHandler
49 RawChannel
&m_raw_channel
;
51 RawChannelSocketHandler(RawChannel
&raw_channel
)
52 : m_raw_channel(raw_channel
)
54 virtual void DataReceived(Data
&data
)
56 m_raw_channel
.HandleReceivedData(data
);
58 virtual void Error(Barry::Error
&error
)
60 SocketDataHandler::Error(error
);
61 m_raw_channel
.HandleError(error
);
63 virtual ~RawChannelSocketHandler()
67 ///////////////////////////////////////////////////////////////////////////////
68 // RawChannel Mode class
70 RawChannel::RawChannel(Controller
&con
, RawChannelDataCallback
&callback
)
71 : Mode(con
, Controller::RawChannel
)
72 , m_mutex_valid(false)
75 , m_callback(&callback
)
77 , m_zero_registered(false)
78 , m_pending_error(NULL
)
80 CheckQueueAvailable();
85 RawChannel::RawChannel(Controller
&con
)
86 : Mode(con
, Controller::RawChannel
)
87 , m_mutex_valid(false)
92 , m_zero_registered(false)
93 , m_pending_error(NULL
)
95 CheckQueueAvailable();
100 void RawChannel::CheckQueueAvailable()
102 if( !m_con
.m_queue
) {
103 throw Barry::Error("RawChannel: No routing queue set in controller");
107 void RawChannel::InitBuffer()
109 m_send_buffer
= new unsigned char[MAX_PACKET_SIZE
];
112 void RawChannel::InitSemaphore()
114 // Create the thread synchronization objects
115 if( pthread_mutex_init(&m_mutex
, NULL
) ) {
116 throw Barry::Error("Failed to create mutex");
118 m_mutex_valid
= true;
119 if( pthread_cond_init(&m_cv
, NULL
) ) {
120 throw Barry::Error("Failed to create condvar");
123 m_semaphore
= new semaphore(m_mutex
, m_cv
);
126 RawChannel::~RawChannel()
128 UnregisterZeroSocketInterest();
130 delete[] m_send_buffer
;
132 if( m_mutex_valid
) {
133 pthread_mutex_destroy(&m_mutex
);
136 pthread_cond_destroy(&m_cv
);
139 delete m_pending_error
;
142 void RawChannel::OnOpen()
144 // Enable sequence packets so that DataSendAck callback and close can be
146 m_zero_registered
= true;
147 m_socket
->HideSequencePacket(false);
148 SocketRoutingQueue::SocketDataHandlerPtr callback
;
149 callback
.reset(new RawChannelSocketHandler(*this));
150 m_con
.m_queue
->RegisterInterest(0, callback
);
151 // Get socket data packets routed to this class as well if using callback
152 // otherside just request interest
154 // Don't want to be called back immediately on data
157 m_socket
->RegisterInterest(callback
);
161 void RawChannel::HandleReceivedData(Data
&data
)
163 // Only ever called in callback mode
164 Protocol::CheckSize(data
, MIN_PACKET_DATA_SIZE
);
165 MAKE_PACKETPTR_BUF(packet
, data
.GetData());
167 if( packet
->socket
== 0 ) {
168 switch( btohs(packet
->command
) )
170 case SB_COMMAND_SEQUENCE_HANDSHAKE
:
171 m_semaphore
->Signal();
173 case SB_COMMAND_CLOSE_SOCKET
:
174 case SB_COMMAND_REMOTE_CLOSE_SOCKET
:
175 // Stop listening to socket 0 messages
176 // so that socket close work.
177 UnregisterZeroSocketInterest();
179 m_callback
->ChannelClose();
182 m_semaphore
->Signal();
185 UnregisterZeroSocketInterest();
187 m_callback
->ChannelError("RawChannel: Got unexpected socket zero packet");
190 SetPendingError("RawChannel: Got unexpected socket zero packet");
192 m_semaphore
->Signal();
197 // Should be a socket packet for us, so remove packet headers
198 Data
partial(data
.GetData() + RAW_HEADER_SIZE
, data
.GetSize() - RAW_HEADER_SIZE
);
200 m_callback
->DataReceived(partial
);
203 SetPendingError("RawChannel: Received data to handle when in non-callback mode");
208 void RawChannel::HandleError(Barry::Error
&error
)
211 m_callback
->ChannelError("RawChannel: Socket error received");
214 SetPendingError("RawChannel: Socket error received");
216 m_semaphore
->Signal();
219 void RawChannel::UnregisterZeroSocketInterest()
221 if( m_zero_registered
) {
222 m_con
.m_queue
->UnregisterInterest(0);
223 m_socket
->HideSequencePacket(true);
224 m_zero_registered
= false;
228 void RawChannel::SetPendingError(const char *msg
)
230 if( !m_pending_error
) {
231 m_pending_error
= new std::string(msg
);
235 ///////////////////////////////////////////////////////////////////////////////
238 void RawChannel::Send(Data
&data
, int timeout
)
240 size_t packetSize
= RAW_HEADER_SIZE
+ data
.GetSize();
242 if( packetSize
> MAX_PACKET_SIZE
) {
243 throw Barry::Error("RawChannel: send data size larger than MaximumPacketSize");
246 // setup header and copy data in
247 MAKE_PACKETPTR_BUF(packet
, m_send_buffer
);
248 packet
->socket
= htobs(m_socket
->GetSocket());
249 packet
->size
= htobs(packetSize
);
250 std::memcpy(&(m_send_buffer
[RAW_HEADER_SIZE
]), data
.GetData(), data
.GetSize());
252 Data
toSend(m_send_buffer
, packetSize
);
253 m_socket
->Send(toSend
, timeout
);
254 m_semaphore
->WaitForSignal();
255 if( m_pending_error
) {
256 throw Barry::Error(*m_pending_error
);
260 void RawChannel::Receive(Data
&data
,int timeout
)
263 throw std::logic_error("RawChannel: Receive called when channel was created with a callback");
265 // Receive into a buffer
266 m_socket
->Receive(m_receive_data
, timeout
);
267 // Then transfer across, skipping the header
268 Protocol::CheckSize(m_receive_data
, RAW_HEADER_SIZE
);
269 size_t len
= m_receive_data
.GetSize() - RAW_HEADER_SIZE
;
270 memcpy(data
.GetBuffer(), m_receive_data
.GetData() + RAW_HEADER_SIZE
, len
);
271 data
.ReleaseBuffer(len
);
275 size_t RawChannel::MaximumSendSize()
277 return MAX_PACKET_SIZE
- RAW_HEADER_SIZE
;
280 }} // namespace Barry::Mode