~mir-team/mir/trunk-0.1.7

« back to all changes in this revision

Viewing changes to src/client/mir_socket_rpc_channel.cpp

  • Committer: Daniel van Vugt
  • Date: 2013-04-24 05:22:20 UTC
  • mfrom: (628 trunk)
  • mto: This revision was merged to the branch mainline in revision 629.
  • Revision ID: daniel.van.vugt@canonical.com-20130424052220-qhpyhw2resxzr7bq
MergeĀ latestĀ lp:mir

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "ancillary.h"
28
28
 
29
29
#include <boost/bind.hpp>
 
30
#include <cstring>
30
31
 
31
32
namespace mcl = mir::client;
32
33
namespace mcld = mir::client::detail;
36
37
{
37
38
}
38
39
 
39
 
mcl::MirSocketRpcChannel::MirSocketRpcChannel(std::string const& endpoint, std::shared_ptr<Logger> const& log) :
40
 
    log(log), pending_calls(log), work(io_service), endpoint(endpoint), socket(io_service)
 
40
mcl::MirSocketRpcChannel::MirSocketRpcChannel(
 
41
    std::string const& endpoint,
 
42
    std::shared_ptr<Logger> const& log) :
 
43
    log(log),
 
44
    pending_calls(log),
 
45
    work(io_service),
 
46
    endpoint(endpoint),
 
47
    socket(io_service),
 
48
    event_handler(nullptr)
41
49
{
42
50
    socket.connect(endpoint);
43
51
 
245
253
 
246
254
        log->debug() << __PRETTY_FUNCTION__ << " result.id():" << result.id() << std::endl;
247
255
 
248
 
        pending_calls.complete_response(result);
 
256
        if (!result.has_id())  // It's an event sequence
 
257
        {
 
258
            process_event_sequence(result);
 
259
        }
 
260
        else
 
261
        {
 
262
            pending_calls.complete_response(result);
 
263
        }
249
264
    }
250
265
    catch (std::exception const& x)
251
266
    {
254
269
    }
255
270
}
256
271
 
 
272
void mcl::MirSocketRpcChannel::process_event_sequence(
 
273
    mir::protobuf::wire::Result const& result)
 
274
{
 
275
    if (!event_handler)
 
276
        return;
 
277
 
 
278
    mir::protobuf::EventSequence seq;
 
279
    if (seq.ParseFromString(result.response()))
 
280
    {
 
281
        int const nevents = seq.event_size();
 
282
        for (int i = 0; i < nevents; i++)
 
283
        {
 
284
            mir::protobuf::Event const& event = seq.event(i);
 
285
            if (event.has_raw())
 
286
            {
 
287
                std::string const& raw_event = event.raw();
 
288
 
 
289
                // In future, events might be compressed where possible.
 
290
                // But that's a job for later...
 
291
                if (raw_event.size() == sizeof(MirEvent))
 
292
                {
 
293
                    MirEvent e;
 
294
 
 
295
                    // Make a copy to ensure integer fields get correct memory
 
296
                    // alignment, which is critical on many non-x86
 
297
                    // architectures.
 
298
                    memcpy(&e, raw_event.data(), sizeof e);
 
299
                    event_handler->handle_event(e);
 
300
                }
 
301
                else
 
302
                {
 
303
                    log->error() << __PRETTY_FUNCTION__
 
304
                                 << " Received MirEvent of an unexpected size."
 
305
                                 << std::endl;
 
306
                }
 
307
            }
 
308
        }
 
309
    } // else protobuf will log an error
 
310
}
 
311
 
257
312
size_t mcl::MirSocketRpcChannel::read_message_header()
258
313
{
259
314
    const size_t body_size = (header_bytes[0] << 8) + header_bytes[1];
275
330
    result.ParseFromIstream(&in);
276
331
    return result;
277
332
}
 
333
 
 
334
void mcl::MirSocketRpcChannel::set_event_handler(mir::EventSink *sink)
 
335
{
 
336
    /*
 
337
     * Yes, these have to be regular pointers. Because ownership of the object
 
338
     * (which is actually a MirConnection) is the responsibility of the calling
 
339
     * client. So out of our control.
 
340
     */
 
341
    event_handler = sink;
 
342
}