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

Last change on this file since 219 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

File size: 4.1 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 {
24namespace core {
25
26ConnectedSocket::ConnectedSocket(const Object *parent, const std::string name)
27 : Object(parent, name){};
28
29ConnectedSocket::~ConnectedSocket(){};
30
31uint16_t ConnectedSocket::ReadUInt16(Time const &timeout) {
32 char buffer[sizeof(uint16_t)];
33 size_t alreadyReceived = 0;
34 Time remainingTimeout = timeout; // ms
35 do {
36 Time beforeTime = GetTime(); // ns
37 ssize_t received =
38 RecvMessage(buffer + alreadyReceived,
39 sizeof(uint16_t) - alreadyReceived, remainingTimeout);
40 remainingTimeout -= (GetTime() - beforeTime) / 100000;
41 if ((received < 0) || (remainingTimeout < 0))
42 throw std::runtime_error("Timeout");
43 alreadyReceived += received;
44 } while (alreadyReceived != sizeof(uint16_t));
45 uint16_t *dataInNetworkEndianness = (uint16_t *)buffer;
46 uint16_t myinteger = NetworkToHost16(*dataInNetworkEndianness);
47 return myinteger;
48}
49
50void ConnectedSocket::WriteUInt16(uint16_t const &data, Time const &timeout) {
51 uint16_t dataInNetworkEndianness = HostToNetwork16(data);
52 char *buffer = (char *)&dataInNetworkEndianness;
53 size_t alreadySent = 0;
54 Time remainingTimeout = timeout; // ms
55 do {
56 Time beforeTime = GetTime(); // ns
57 ssize_t sent = SendMessage(
58 buffer + alreadySent, sizeof(uint16_t) - alreadySent, remainingTimeout);
59 remainingTimeout -= (GetTime() - beforeTime) / 100000;
60 if ((sent < 0) || (remainingTimeout < 0))
61 throw std::runtime_error("Timeout");
62 alreadySent += sent;
63 } while (alreadySent != sizeof(uint16_t));
64}
65
66uint32_t ConnectedSocket::ReadUInt32(Time const &timeout) {
67 char buffer[sizeof(uint32_t)];
68 size_t alreadyReceived = 0;
69 Time remainingTimeout = timeout; // ms
70 do {
71 Time beforeTime = GetTime(); // ns
72 ssize_t received =
73 RecvMessage(buffer + alreadyReceived,
74 sizeof(uint32_t) - alreadyReceived, remainingTimeout);
75 remainingTimeout -= (GetTime() - beforeTime) / 100000;
76 if ((received < 0) || (remainingTimeout < 0))
77 throw std::runtime_error("Timeout");
78 alreadyReceived += received;
79 } while (alreadyReceived != sizeof(uint32_t));
80 uint32_t *dataInNetworkEndianness = (uint32_t *)buffer;
81 uint32_t myinteger = NetworkToHost32(*dataInNetworkEndianness);
82 return myinteger;
83}
84
85void ConnectedSocket::WriteUInt32(uint32_t const &data, Time const &timeout) {
86 uint32_t dataInNetworkEndianness = HostToNetwork32(data);
87 char *buffer = (char *)&dataInNetworkEndianness;
88 size_t alreadySent = 0;
89 Time remainingTimeout = timeout; // ms
90 do {
91 Time beforeTime = GetTime(); // ns
92 ssize_t sent = SendMessage(
93 buffer + alreadySent, sizeof(uint32_t) - alreadySent, remainingTimeout);
94 remainingTimeout -= (GetTime() - beforeTime) / 100000;
95 if ((sent < 0) || (remainingTimeout < 0))
96 throw std::runtime_error("Timeout");
97 alreadySent += sent;
98 } while (alreadySent != sizeof(uint32_t));
99}
100
101string ConnectedSocket::ReadString(const size_t &stringSize, Time timeout) {
102 char buffer[stringSize + 1];
103 size_t alreadyReceived = 0;
104 Time remainingTimeout = timeout; // ms
105 do {
106 Time beforeTime = GetTime(); // ns
107 ssize_t received =
108 RecvMessage(buffer + alreadyReceived, stringSize - alreadyReceived,
109 remainingTimeout);
110 remainingTimeout -= (GetTime() - beforeTime) / 100000;
111 if ((received < 0) || (remainingTimeout < 0))
112 throw std::runtime_error("Timeout");
113 alreadyReceived += received;
114 } while (alreadyReceived != stringSize);
115 buffer[stringSize] = '\0';
116
117 string mystring = buffer;
118 return mystring;
119}
120
121} // end namespace core
122} // end namespace flair
Note: See TracBrowser for help on using the repository browser.