-
Notifications
You must be signed in to change notification settings - Fork 195
/
frame.cpp
104 lines (87 loc) · 1.84 KB
/
frame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file is part of zmqpp.
* Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
*/
/**
* \file
*
* \date 8 Jan 2014
* \author Ben Gray (\@benjamg)
*/
#include <cassert>
#include <cstring>
#include "exception.hpp"
#include "frame.hpp"
namespace zmqpp {
frame::frame()
: _sent( false )
{
if( 0 != zmq_msg_init( &_msg ) )
{
throw zmq_internal_exception();
}
}
frame::frame(size_t const size)
: _sent( false )
{
if( 0 != zmq_msg_init_size( &_msg, size ) )
{
throw zmq_internal_exception();
}
}
frame::frame(void const* part, size_t const size)
: _sent( false )
{
if( 0 != zmq_msg_init_size( &_msg, size ) )
{
throw zmq_internal_exception();
}
void* msg_data = zmq_msg_data( &_msg );
memcpy( msg_data, part, size );
}
frame::frame(void* part, size_t const size, zmq_free_fn *ffn, void *hint)
: _sent( false )
{
if( 0 != zmq_msg_init_data( &_msg, part, size, ffn, hint ) )
{
throw zmq_internal_exception();
}
}
frame::~frame()
{
#ifndef NDEBUG // unused assert variable in release
int result = zmq_msg_close( &_msg );
assert(0 == result);
#else
zmq_msg_close( &_msg );
#endif // NDEBUG
}
frame::frame(frame&& other)
: _sent( other._sent )
{
zmq_msg_init( &_msg );
zmq_msg_move( &_msg, &other._msg );
other._sent = false;
}
frame& frame::operator=(frame&& other)
{
zmq_msg_init( &_msg );
zmq_msg_move( &_msg, &other._msg );
std::swap( _sent, other._sent );
return *this;
}
frame frame::copy() const
{
frame other( size() );
other._sent = _sent;
if( 0 != zmq_msg_copy( &other._msg, const_cast<zmq_msg_t*>(&_msg) ) )
{
throw zmq_internal_exception();
}
return other;
}
} // namespace zmqpp