Renaming raw sockets to raw channels to follow device side API naming.
[barry.git] / src / m_raw_channel.cc
blob73884c2f5a16088956c5de96a6ddcec69c2e0616
1 ///
2 /// \file m_raw_channel.cc
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 #include "m_raw_channel.h"
24 #include "data.h"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "packet.h"
28 #include "endian.h"
29 #include "error.h"
30 #include "usbwrap.h"
31 #include "controller.h"
32 #include <stdexcept>
33 #include <sstream>
34 #include <cstring>
36 #include "debug.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),
49 Callback(callback)
53 RawChannel::~RawChannel()
57 void RawChannel::OnOpen()
59 m_socket->RegisterInterest(HandleReceivedDataCallback, this);
62 ///////////////////////////////////////////////////////////////////////////////
63 // public API
65 void RawChannel::Send(Data& data)
67 Data toReceive;
68 try
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