Changeset 15 in flair-src for trunk/lib/FlairCore/src/ConnectedSocket.cpp


Ignore:
Timestamp:
Apr 8, 2016, 3:40:57 PM (5 years ago)
Author:
Bayard Gildas
Message:

sources reformatted with flair-format-dir script

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairCore/src/ConnectedSocket.cpp

    r2 r15  
    2121using std::string;
    2222
    23 namespace flair
    24 {
    25 namespace core
    26 {
     23namespace flair {
     24namespace core {
    2725
    28 ConnectedSocket::ConnectedSocket(const Object* parent,const std::string name):Object(parent,name) {};
     26ConnectedSocket::ConnectedSocket(const Object *parent, const std::string name)
     27    : Object(parent, name){};
    2928
    30 ConnectedSocket::~ConnectedSocket() {};
     29ConnectedSocket::~ConnectedSocket(){};
    3130
    32 uint16_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;
     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;
    4648}
    4749
    48 void 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));
     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));
    6064}
    6165
    62 uint32_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;
     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;
    7683}
    7784
    78 void 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));
     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));
    9099}
    91100
    92101string 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';
     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';
    104116
    105     string mystring=buffer;
    106     return mystring;
     117  string mystring = buffer;
     118  return mystring;
    107119}
    108120
Note: See TracChangeset for help on using the changeset viewer.