source: flair-src/trunk/lib/FlairCore/src/Socket.cpp @ 2

Last change on this file since 2 was 2, checked in by Sanahuja Guillaume, 6 years ago

flaircore

File size: 2.4 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:   Socket.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 "Socket.h"
18#include "Socket_impl.h"
19#include <FrameworkManager.h>
20#include <string.h>
21#include <stdexcept>
22
23using std::string;
24
25namespace flair
26{
27namespace core
28{
29
30Socket::Socket(const Object* parent, string name,uint16_t port): Object(parent,name) {
31    pimpl_=new Socket_impl(this,name,port);
32}
33
34Socket::Socket(const Object* parent, string name,string address,bool broadcast): Object(parent,name) {
35    pimpl_=new Socket_impl(this,name,address,broadcast);
36}
37
38Socket::~Socket() {
39    delete pimpl_;
40}
41
42void Socket::SendMessage(const char* message,size_t message_len) {
43    pimpl_->SendMessage(message,message_len);
44}
45
46void Socket::SendMessage(string message) {
47    pimpl_->SendMessage(message);
48}
49
50ssize_t Socket::RecvMessage(char* buf,size_t buf_len,Time timeout,char* src,size_t* src_len) {
51    return pimpl_->RecvMessage(buf,buf_len,timeout,src,src_len);
52}
53
54void Socket::NetworkToHost(char *data,size_t dataSize) {
55    if (core::IsBigEndian()) return;
56    if (dataSize==1) return;
57    if ((dataSize==2)||(dataSize==4)||(dataSize==8)||(dataSize==16)) {
58        char dataInHostEndianness[dataSize];
59        for (unsigned int i=0;i<dataSize;i++) {
60            dataInHostEndianness[i]=data[dataSize-i-1];
61        }
62        memcpy(data,dataInHostEndianness,dataSize);
63        return;
64    }
65    throw std::runtime_error(string("Unsupported data size (")+std::to_string(dataSize)+string(") in host to network endianness conversion"));
66}
67
68void Socket::HostToNetwork(char *data,size_t dataSize) {
69    if (IsBigEndian()) return;
70    if (dataSize==1) return;
71    if ((dataSize==2)||(dataSize==4)||(dataSize==8)||(dataSize==16)) {
72        char dataInNetworkEndianness[dataSize];
73        for (unsigned int i=0;i<dataSize;i++) {
74            dataInNetworkEndianness[i]=data[dataSize-i-1];
75        }
76        memcpy(data,dataInNetworkEndianness,dataSize);
77        return;
78    }
79    throw std::runtime_error(string("Unsupported data size (")+std::to_string(dataSize)+string(") in host to network endianness conversion"));
80}
81
82} // end namespace core
83} // end namespace flair
Note: See TracBrowser for help on using the repository browser.