File: binarystream.cpp

package info (click to toggle)
r-cran-rinside 0.2.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: cpp: 3,310; ansic: 117; xml: 57; ruby: 34; makefile: 2
file content (175 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (5)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (c) 2014 Christian Authmann
 */

#include "binarystream.h"

#include <sstream>
#include <cstring> // memset(), strerror()
#include <memory>
#include <stdexcept>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>


BinaryStream::BinaryStream(int read_fd, int write_fd) : is_eof(false), read_fd(read_fd), write_fd(write_fd) {
}

BinaryStream::~BinaryStream() {
	close();
}

void BinaryStream::close() {
	if (read_fd == write_fd)
		write_fd = -1;
	if (read_fd >= 0) {
		::close(read_fd);
		read_fd = -1;
	}
	if (write_fd >= 0) {
		::close(write_fd);
		write_fd = -1;
	}
	is_eof = true;
}

BinaryStream::BinaryStream(BinaryStream &&other) : is_eof(other.is_eof), read_fd(other.read_fd), write_fd(other.write_fd) {
	other.is_eof = true;
	other.read_fd = -1;
	other.write_fd = -1;
}

BinaryStream &BinaryStream::operator=(BinaryStream &&other) {
	std::swap(is_eof, other.is_eof);
	std::swap(read_fd, other.read_fd);
	std::swap(write_fd, other.write_fd);
	return *this;
}


BinaryStream BinaryStream::connectToUnixSocket(const char *server_path) {
	int new_fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (new_fd < 0)
		throw stream_exception();

	struct sockaddr_un server_addr;
	memset((void *) &server_addr, 0, sizeof(server_addr));

	server_addr.sun_family = AF_UNIX;
	strcpy(server_addr.sun_path, server_path);
	if (connect(new_fd, (sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
		::close(new_fd);
		throw stream_exception();
	}

	return BinaryStream(new_fd, new_fd);
}


void BinaryStream::write(const char *buffer, size_t len) {
	if (write_fd < 0)
		throw stream_exception();

	//printf("Stream: writing %lu bytes\n", len);
	auto res = ::write(write_fd, buffer, len);
	if (res < 0 || (size_t) res != len) {
		// strerror(errno);
		throw stream_exception();
	}
}

size_t BinaryStream::read(char *buffer, size_t len) {
	if (read_fd < 0 || is_eof)
		throw stream_exception();

	//printf("Stream: reading %lu bytes\n", len);

	size_t remaining = len;
	size_t bytes_read = 0;
	while (remaining > 0) {
		auto r = ::read(read_fd, buffer, remaining);
		if (r == 0) {
			is_eof = true;
			throw stream_exception();
		}
		if (r < 0) {
			// strerror(errno);
			throw stream_exception();
		}
		bytes_read += r;
		buffer += r;
		remaining -= r;
	}
	if (bytes_read != len)
		throw stream_exception();

	return bytes_read;
}



namespace serialization {
	// Strings
	void serializer<std::string>::serialize(BinaryStream &stream, const std::string &string) {
		size_t len = string.size();
		if (len > (size_t) (1<<31))
			throw BinaryStream::stream_exception();
		stream.write(len);
		stream.write(string.data(), len);
	}

	std::string serializer<std::string>::deserialize(BinaryStream &stream) {
		auto len = stream.read<size_t>();
		if (len == 0)
			return "";

		if (len > (size_t) (1<<31))
			throw BinaryStream::stream_exception();

		std::unique_ptr<char[]> buffer( new char[len] );
		stream.read(buffer.get(), len);

		std::string string(buffer.get(), len);
		return string;
	}

	// Vectors
	template <typename T>
	void serializer<std::vector<T>>::serialize(BinaryStream &stream, const std::vector<T> &vec) {
		size_t size = vec.size();
		if (size > (size_t) (1<<31))
			throw BinaryStream::stream_exception();
		stream.write(size);
		for (size_t i=0;i<size;i++)
			stream.write(vec[i]);
	}
	template <typename T>
	std::vector<T> serializer<std::vector<T>>::deserialize(BinaryStream &stream) {
		std::vector<T> vec;
		auto size = stream.read<size_t>();
		if (size > (size_t) (1<<31))
			throw BinaryStream::stream_exception();
		vec.reserve(size);
		for (size_t i=0;i<size;i++)
			vec.push_back(stream.read<T>());
		return vec;
	};

	// Note: when adding more serializers, don't forget to add their declaration in binarystream.h!

	// Make sure to instantiate the vectors we need
	template struct serializer<std::vector<int8_t>>;
	template struct serializer<std::vector<uint8_t>>;
	template struct serializer<std::vector<int16_t>>;
	template struct serializer<std::vector<uint16_t>>;
	template struct serializer<std::vector<int32_t>>;
	template struct serializer<std::vector<uint32_t>>;
	template struct serializer<std::vector<int64_t>>;
	template struct serializer<std::vector<uint64_t>>;
	template struct serializer<std::vector<float>>;
	template struct serializer<std::vector<double>>;
	template struct serializer<std::vector<std::string>>;
}