-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamer.h
More file actions
96 lines (75 loc) · 2.38 KB
/
streamer.h
File metadata and controls
96 lines (75 loc) · 2.38 KB
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
//
// Created by ip on 21.02.2017.
//
#ifndef UDP_STREAMER_H
#define UDP_STREAMER_H
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
using namespace boost::filesystem;
using namespace boost::iostreams;
class streamer {
public:
streamer( size_t mem_pages_size,
path dir = current_path() )
: m_size (mem_pages_size), m_dir(dir), m_written(0)
{
m_mempagesize = boost::interprocess::mapped_region::get_page_size();
m_size = m_size * m_mempagesize; // get size in bytes
next_file_name();
init_mmap_file();
m_f_compress = false;
}
~streamer()
{
m_file.close();
}
private:
mapped_file_sink m_file;
size_t m_size;
size_t m_written;
size_t m_mempagesize;
path m_dir;
path m_filename;
bool m_f_compress;
void init_mmap_file()
{
if(m_file.is_open())
m_file.close();
/*
mapped_file_params p(first.name());
p.new_file_size = boost::iostreams::test::data_reps * boost::iostreams::test::data_length();
boost::iostreams::stream<mapped_file_sink> out;
out.open(mapped_file_sink(p));
boost::iostreams::test::write_data_in_chars(out);
out.close();
BOOST_CHECK_MESSAGE(
boost::iostreams::test::compare_files(first.name(), test.name()),
"failed writing to pre-existing mapped file in chars"
);
*/
mapped_file_params p(m_filename.generic_string());
p.new_file_size = m_size;
m_file.open(p);
// m_file.open(m_filename, m_size);
m_written = 0;
// Check if file was successfully opened
if(!m_file.is_open())
throw this; // TODO: add error handling
}
void next_file_name()
{
m_filename = m_dir;
m_filename /= "streamer_";
m_filename += unique_path();
m_filename.replace_extension("bin");
}
public:
/// returns size of saved data to memory mapped file
size_t mempagesize() { return m_mempagesize; }
size_t dump_data(boost::asio::mutable_buffer buff);
size_t dump_data(boost::array<unsigned char*, 1000> buff);
};
#endif //UDP_STREAMER_H