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