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"
26 #include "protostructs.h"
31 #include "controller.h"
38 namespace Barry
{ namespace Mode
{
40 static void HandleReceivedDataCallback(void* ctx
, Data
* data
) {
41 ((RawChannel
*)ctx
)->HandleReceivedData(*data
);
44 ///////////////////////////////////////////////////////////////////////////////
45 // RawChannel Mode class
47 RawChannel::RawChannel(Controller
&con
, RawChannelDataCallback
& callback
)
48 : Mode(con
, Controller::RawChannel
),
53 RawChannel::~RawChannel()
57 void RawChannel::OnOpen()
59 m_socket
->RegisterInterest(HandleReceivedDataCallback
, this);
62 ///////////////////////////////////////////////////////////////////////////////
65 void RawChannel::Send(Data
& data
)
70 size_t packetSize
= HeaderSize
+ data
.GetSize();
71 Barry::Protocol::Packet
* packet
= (Barry::Protocol::Packet
*)m_sendBuffer
;
72 packet
->socket
= htobs(m_socket
->GetSocket());
73 packet
->size
= htobs(packetSize
);
74 std::memcpy(&(m_sendBuffer
[HeaderSize
]), data
.GetData(), data
.GetSize());
76 Data
toSend(m_sendBuffer
, packetSize
);
77 m_socket
->PacketData(toSend
, toReceive
, 0); // timeout immediately
78 if (toReceive
.GetSize() != 0)
79 HandleReceivedData(toReceive
);
81 catch (Usb::Error
& err
)
86 void RawChannel::HandleReceivedData(Data
& data
)
88 // Remove packet headers
89 Data
partial(data
.GetData() + HeaderSize
, data
.GetSize() - HeaderSize
);
90 Callback
.DataReceived(partial
);
93 }} // namespace Barry::Mode