1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2013/11/17
|
---|
6 | // filename: UdpSocket.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: Class defining a UDP socket
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 | #include "UdpSocket.h"
|
---|
18 | #include "UdpSocket_impl.h"
|
---|
19 | #include <FrameworkManager.h>
|
---|
20 | #include <string.h>
|
---|
21 | #include <stdexcept>
|
---|
22 |
|
---|
23 | using std::string;
|
---|
24 |
|
---|
25 | namespace flair {
|
---|
26 | namespace core {
|
---|
27 |
|
---|
28 | UdpSocket::UdpSocket(const Object *parent, string name, uint16_t port)
|
---|
29 | : Object(parent, name) {
|
---|
30 | pimpl_ = new UdpSocket_impl(this, name, port);
|
---|
31 | }
|
---|
32 |
|
---|
33 | UdpSocket::UdpSocket(const Object *parent, string name, string address,
|
---|
34 | bool broadcast)
|
---|
35 | : Object(parent, name) {
|
---|
36 | pimpl_ = new UdpSocket_impl(this, name, address, broadcast);
|
---|
37 | }
|
---|
38 |
|
---|
39 | UdpSocket::~UdpSocket() { delete pimpl_; }
|
---|
40 |
|
---|
41 | void UdpSocket::SendMessage(const char *message, size_t message_len,int dst_id) {
|
---|
42 | pimpl_->SendMessage(message, message_len,dst_id);
|
---|
43 | }
|
---|
44 |
|
---|
45 | void UdpSocket::SendMessage(string message) { pimpl_->SendMessage(message); }
|
---|
46 |
|
---|
47 | ssize_t UdpSocket::RecvMessage(char *buf, size_t buf_len, Time timeout, char *src,
|
---|
48 | size_t *src_len,int *src_id) {
|
---|
49 | return pimpl_->RecvMessage(buf, buf_len, timeout, src, src_len,src_id);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void UdpSocket::NetworkToHost(char *data, size_t dataSize) {
|
---|
53 | if (core::IsBigEndian())
|
---|
54 | return;
|
---|
55 | if (dataSize == 1)
|
---|
56 | return;
|
---|
57 | if ((dataSize == 2) || (dataSize == 4) || (dataSize == 8) ||
|
---|
58 | (dataSize == 16)) {
|
---|
59 | char dataInHostEndianness[dataSize];
|
---|
60 | for (unsigned int i = 0; i < dataSize; i++) {
|
---|
61 | dataInHostEndianness[i] = data[dataSize - i - 1];
|
---|
62 | }
|
---|
63 | memcpy(data, dataInHostEndianness, dataSize);
|
---|
64 | return;
|
---|
65 | }
|
---|
66 | throw std::runtime_error(
|
---|
67 | string("Unsupported data size (") + std::to_string(dataSize) +
|
---|
68 | string(") in host to network endianness conversion"));
|
---|
69 | }
|
---|
70 |
|
---|
71 | void UdpSocket::HostToNetwork(char *data, size_t dataSize) {
|
---|
72 | if (IsBigEndian())
|
---|
73 | return;
|
---|
74 | if (dataSize == 1)
|
---|
75 | return;
|
---|
76 | if ((dataSize == 2) || (dataSize == 4) || (dataSize == 8) ||
|
---|
77 | (dataSize == 16)) {
|
---|
78 | char dataInNetworkEndianness[dataSize];
|
---|
79 | for (unsigned int i = 0; i < dataSize; i++) {
|
---|
80 | dataInNetworkEndianness[i] = data[dataSize - i - 1];
|
---|
81 | }
|
---|
82 | memcpy(data, dataInNetworkEndianness, dataSize);
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | throw std::runtime_error(
|
---|
86 | string("Unsupported data size (") + std::to_string(dataSize) +
|
---|
87 | string(") in host to network endianness conversion"));
|
---|
88 | }
|
---|
89 |
|
---|
90 | } // end namespace core
|
---|
91 | } // end namespace flair
|
---|