source: flair-src/trunk/lib/FlairCore/src/ConnectedSocket.cpp@ 13

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

flaircore

File size: 4.0 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: 2015/04/21
6// filename: ConnectedSocket.cpp
7//
8// author: Gildas Bayard
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a connected socket
14//
15//
16/*********************************************************************/
17#include "ConnectedSocket.h"
18#include <string.h>
19#include <stdexcept>
20
21using std::string;
22
23namespace flair
24{
25namespace core
26{
27
28ConnectedSocket::ConnectedSocket(const Object* parent,const std::string name):Object(parent,name) {};
29
30ConnectedSocket::~ConnectedSocket() {};
31
32uint16_t ConnectedSocket::ReadUInt16(Time const& timeout) {
33 char buffer[sizeof(uint16_t)];
34 size_t alreadyReceived=0;
35 Time remainingTimeout=timeout; //ms
36 do {
37 Time beforeTime=GetTime(); //ns
38 ssize_t received=RecvMessage(buffer+alreadyReceived,sizeof(uint16_t)-alreadyReceived,remainingTimeout);
39 remainingTimeout-=(GetTime()-beforeTime)/100000;
40 if ((received<0) || (remainingTimeout<0)) throw std::runtime_error("Timeout");
41 alreadyReceived+=received;
42 } while (alreadyReceived!=sizeof(uint16_t));
43 uint16_t *dataInNetworkEndianness=(uint16_t*)buffer;
44 uint16_t myinteger=NetworkToHost16(*dataInNetworkEndianness);
45 return myinteger;
46}
47
48void ConnectedSocket::WriteUInt16(uint16_t const& data,Time const& timeout) {
49 uint16_t dataInNetworkEndianness=HostToNetwork16(data);
50 char *buffer=(char *)&dataInNetworkEndianness;
51 size_t alreadySent=0;
52 Time remainingTimeout=timeout; //ms
53 do {
54 Time beforeTime=GetTime(); //ns
55 ssize_t sent=SendMessage(buffer+alreadySent,sizeof(uint16_t)-alreadySent,remainingTimeout);
56 remainingTimeout-=(GetTime()-beforeTime)/100000;
57 if ((sent<0) || (remainingTimeout<0)) throw std::runtime_error("Timeout");
58 alreadySent+=sent;
59 } while (alreadySent!=sizeof(uint16_t));
60}
61
62uint32_t ConnectedSocket::ReadUInt32(Time const& timeout) {
63 char buffer[sizeof(uint32_t)];
64 size_t alreadyReceived=0;
65 Time remainingTimeout=timeout; //ms
66 do {
67 Time beforeTime=GetTime(); //ns
68 ssize_t received=RecvMessage(buffer+alreadyReceived,sizeof(uint32_t)-alreadyReceived,remainingTimeout);
69 remainingTimeout-=(GetTime()-beforeTime)/100000;
70 if ((received<0) || (remainingTimeout<0)) throw std::runtime_error("Timeout");
71 alreadyReceived+=received;
72 } while (alreadyReceived!=sizeof(uint32_t));
73 uint32_t *dataInNetworkEndianness=(uint32_t*)buffer;
74 uint32_t myinteger=NetworkToHost32(*dataInNetworkEndianness);
75 return myinteger;
76}
77
78void ConnectedSocket::WriteUInt32(uint32_t const& data,Time const& timeout) {
79 uint32_t dataInNetworkEndianness=HostToNetwork32(data);
80 char *buffer=(char *)&dataInNetworkEndianness;
81 size_t alreadySent=0;
82 Time remainingTimeout=timeout; //ms
83 do {
84 Time beforeTime=GetTime(); //ns
85 ssize_t sent=SendMessage(buffer+alreadySent,sizeof(uint32_t)-alreadySent,remainingTimeout);
86 remainingTimeout-=(GetTime()-beforeTime)/100000;
87 if ((sent<0) || (remainingTimeout<0)) throw std::runtime_error("Timeout");
88 alreadySent+=sent;
89 } while (alreadySent!=sizeof(uint32_t));
90}
91
92string ConnectedSocket::ReadString(const size_t &stringSize, Time timeout) {
93 char buffer[stringSize+1];
94 size_t alreadyReceived=0;
95 Time remainingTimeout=timeout; //ms
96 do {
97 Time beforeTime=GetTime(); //ns
98 ssize_t received=RecvMessage(buffer+alreadyReceived,stringSize-alreadyReceived,remainingTimeout);
99 remainingTimeout-=(GetTime()-beforeTime)/100000;
100 if ((received<0) || (remainingTimeout<0)) throw std::runtime_error("Timeout");
101 alreadyReceived+=received;
102 } while (alreadyReceived!=stringSize);
103 buffer[stringSize]='\0';
104
105 string mystring=buffer;
106 return mystring;
107}
108
109} // end namespace core
110} // end namespace flair
Note: See TracBrowser for help on using the repository browser.