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 |
|
---|
23 | using std::string;
|
---|
24 |
|
---|
25 | namespace flair
|
---|
26 | {
|
---|
27 | namespace core
|
---|
28 | {
|
---|
29 |
|
---|
30 | Socket::Socket(const Object* parent, string name,uint16_t port): Object(parent,name) {
|
---|
31 | pimpl_=new Socket_impl(this,name,port);
|
---|
32 | }
|
---|
33 |
|
---|
34 | Socket::Socket(const Object* parent, string name,string address,bool broadcast): Object(parent,name) {
|
---|
35 | pimpl_=new Socket_impl(this,name,address,broadcast);
|
---|
36 | }
|
---|
37 |
|
---|
38 | Socket::~Socket() {
|
---|
39 | delete pimpl_;
|
---|
40 | }
|
---|
41 |
|
---|
42 | void Socket::SendMessage(const char* message,size_t message_len) {
|
---|
43 | pimpl_->SendMessage(message,message_len);
|
---|
44 | }
|
---|
45 |
|
---|
46 | void Socket::SendMessage(string message) {
|
---|
47 | pimpl_->SendMessage(message);
|
---|
48 | }
|
---|
49 |
|
---|
50 | ssize_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 |
|
---|
54 | void 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 |
|
---|
68 | void 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
|
---|