[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | // created: 2013/11/17
|
---|
[178] | 6 | // filename: UdpSocket.cpp
|
---|
[2] | 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 | /*********************************************************************/
|
---|
[178] | 17 | #include "UdpSocket.h"
|
---|
| 18 | #include "UdpSocket_impl.h"
|
---|
[2] | 19 | #include <FrameworkManager.h>
|
---|
| 20 | #include <string.h>
|
---|
| 21 | #include <stdexcept>
|
---|
| 22 |
|
---|
| 23 | using std::string;
|
---|
| 24 |
|
---|
[15] | 25 | namespace flair {
|
---|
| 26 | namespace core {
|
---|
[2] | 27 |
|
---|
[178] | 28 | UdpSocket::UdpSocket(const Object *parent, string name, uint16_t port)
|
---|
[15] | 29 | : Object(parent, name) {
|
---|
[178] | 30 | pimpl_ = new UdpSocket_impl(this, name, port);
|
---|
[2] | 31 | }
|
---|
| 32 |
|
---|
[178] | 33 | UdpSocket::UdpSocket(const Object *parent, string name, string address,
|
---|
[15] | 34 | bool broadcast)
|
---|
| 35 | : Object(parent, name) {
|
---|
[178] | 36 | pimpl_ = new UdpSocket_impl(this, name, address, broadcast);
|
---|
[2] | 37 | }
|
---|
| 38 |
|
---|
[178] | 39 | UdpSocket::~UdpSocket() { delete pimpl_; }
|
---|
[2] | 40 |
|
---|
[178] | 41 | void UdpSocket::SendMessage(const char *message, size_t message_len) {
|
---|
[15] | 42 | pimpl_->SendMessage(message, message_len);
|
---|
[2] | 43 | }
|
---|
| 44 |
|
---|
[178] | 45 | void UdpSocket::SendMessage(string message) { pimpl_->SendMessage(message); }
|
---|
[2] | 46 |
|
---|
[178] | 47 | ssize_t UdpSocket::RecvMessage(char *buf, size_t buf_len, Time timeout, char *src,
|
---|
[15] | 48 | size_t *src_len) {
|
---|
| 49 | return pimpl_->RecvMessage(buf, buf_len, timeout, src, src_len);
|
---|
[2] | 50 | }
|
---|
| 51 |
|
---|
[178] | 52 | void UdpSocket::NetworkToHost(char *data, size_t dataSize) {
|
---|
[15] | 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];
|
---|
[2] | 62 | }
|
---|
[15] | 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"));
|
---|
[2] | 69 | }
|
---|
| 70 |
|
---|
[178] | 71 | void UdpSocket::HostToNetwork(char *data, size_t dataSize) {
|
---|
[15] | 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];
|
---|
[2] | 81 | }
|
---|
[15] | 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"));
|
---|
[2] | 88 | }
|
---|
| 89 |
|
---|
| 90 | } // end namespace core
|
---|
| 91 | } // end namespace flair
|
---|