2 /// \file m_raw_socket.cc
3 /// Mode class for the raw socket
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_socket.h"
26 #include "protostructs.h"
31 #include "controller.h"
38 namespace Barry
{ namespace Mode
{
40 static void HandleReceivedDataCallback(void* ctx
, Data
* data
)
42 ((RawSocket
*)ctx
)->HandleReceivedData(*data
);
45 ///////////////////////////////////////////////////////////////////////////////
46 // RawSocket Mode class
48 RawSocket::RawSocket(Controller
&con
, RawSocketDataCallback
& callback
)
49 : Mode(con
, Controller::RawSocket
),
54 RawSocket::~RawSocket()
58 void RawSocket::OnOpen()
60 m_socket
->RegisterInterest(HandleReceivedDataCallback
, this);
63 ///////////////////////////////////////////////////////////////////////////////
66 void RawSocket::Send(Data
& data
)
71 size_t packetSize
= 4 + data
.GetSize();
72 Barry::Protocol::Packet
* packet
= (Barry::Protocol::Packet
*)m_sendBuffer
;
73 packet
->socket
= htobs(m_socket
->GetSocket());
74 packet
->size
= htobs(packetSize
);
75 std::memcpy(&(m_sendBuffer
[4]), data
.GetData(), data
.GetSize());
77 Data
toSend(m_sendBuffer
, packetSize
);
78 m_socket
->PacketData(toSend
, toReceive
, 0); // timeout immediately
79 if (toReceive
.GetSize() != 0)
80 HandleReceivedData(toReceive
);
82 catch (Usb::Error
& err
)
87 void RawSocket::HandleReceivedData(Data
& data
)
89 // Remove packet headers
90 Data
partial(data
.GetData() + 4, data
.GetSize() - 4);
91 Callback
.DataReceived(partial
);
94 }} // namespace Barry::Mode