source: flair-src/trunk/lib/FlairCore/src/UdpSocket.cpp@ 358

Last change on this file since 358 was 178, checked in by Bayard Gildas, 7 years ago

Change Udp socket object name (from "Socket" to "UdpSocket")

File size: 2.6 KB
Line 
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
23using std::string;
24
25namespace flair {
26namespace core {
27
28UdpSocket::UdpSocket(const Object *parent, string name, uint16_t port)
29 : Object(parent, name) {
30 pimpl_ = new UdpSocket_impl(this, name, port);
31}
32
33UdpSocket::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
39UdpSocket::~UdpSocket() { delete pimpl_; }
40
41void UdpSocket::SendMessage(const char *message, size_t message_len) {
42 pimpl_->SendMessage(message, message_len);
43}
44
45void UdpSocket::SendMessage(string message) { pimpl_->SendMessage(message); }
46
47ssize_t UdpSocket::RecvMessage(char *buf, size_t buf_len, Time timeout, char *src,
48 size_t *src_len) {
49 return pimpl_->RecvMessage(buf, buf_len, timeout, src, src_len);
50}
51
52void 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
71void 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
Note: See TracBrowser for help on using the repository browser.