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

Last change on this file since 238 was 238, checked in by Bayard Gildas, 4 years ago

correction sémaphore. bloquant tout ça...

File size: 4.0 KB
RevLine 
[2]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[2]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
[15]23namespace flair {
24namespace core {
[2]25
[15]26ConnectedSocket::ConnectedSocket(const Object *parent, const std::string name)
27    : Object(parent, name){};
[2]28
[15]29ConnectedSocket::~ConnectedSocket(){};
[2]30
[15]31uint16_t ConnectedSocket::ReadUInt16(Time const &timeout) {
32  char buffer[sizeof(uint16_t)];
33  size_t alreadyReceived = 0;
[238]34  Time remainingTimeout = timeout; // ns
[15]35  do {
36    Time beforeTime = GetTime(); // ns
37    ssize_t received =
38        RecvMessage(buffer + alreadyReceived,
39                    sizeof(uint16_t) - alreadyReceived, remainingTimeout);
[238]40    remainingTimeout -= GetTime() - beforeTime;
41    if ((received < 0) || (remainingTimeout < 0)) throw std::runtime_error("Timeout");
[15]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;
[2]47}
48
[15]49void 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;
[238]53  Time remainingTimeout = timeout; // ns
[15]54  do {
55    Time beforeTime = GetTime(); // ns
56    ssize_t sent = SendMessage(
57        buffer + alreadySent, sizeof(uint16_t) - alreadySent, remainingTimeout);
[238]58    remainingTimeout -= GetTime() - beforeTime;
[15]59    if ((sent < 0) || (remainingTimeout < 0))
60      throw std::runtime_error("Timeout");
61    alreadySent += sent;
62  } while (alreadySent != sizeof(uint16_t));
[2]63}
64
[15]65uint32_t ConnectedSocket::ReadUInt32(Time const &timeout) {
66  char buffer[sizeof(uint32_t)];
67  size_t alreadyReceived = 0;
[238]68  Time remainingTimeout = timeout; // ns
[15]69  do {
70    Time beforeTime = GetTime(); // ns
71    ssize_t received =
72        RecvMessage(buffer + alreadyReceived,
73                    sizeof(uint32_t) - alreadyReceived, remainingTimeout);
[238]74    remainingTimeout -= GetTime() - beforeTime;
[15]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;
[2]82}
83
[15]84void 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;
[238]88  Time remainingTimeout = timeout; // ns
[15]89  do {
90    Time beforeTime = GetTime(); // ns
91    ssize_t sent = SendMessage(
92        buffer + alreadySent, sizeof(uint32_t) - alreadySent, remainingTimeout);
[238]93    remainingTimeout -= GetTime() - beforeTime;
[15]94    if ((sent < 0) || (remainingTimeout < 0))
95      throw std::runtime_error("Timeout");
96    alreadySent += sent;
97  } while (alreadySent != sizeof(uint32_t));
[2]98}
99
100string ConnectedSocket::ReadString(const size_t &stringSize, Time timeout) {
[15]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);
[238]109    remainingTimeout -= GetTime() - beforeTime;
[15]110    if ((received < 0) || (remainingTimeout < 0))
111      throw std::runtime_error("Timeout");
112    alreadyReceived += received;
113  } while (alreadyReceived != stringSize);
114  buffer[stringSize] = '\0';
[2]115
[15]116  string mystring = buffer;
117  return mystring;
[2]118}
119
120} // end namespace core
121} // end namespace flair
Note: See TracBrowser for help on using the repository browser.