YARP
Yet Another Robot Platform
TcpStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2010 Anne van Rossum <anne@almende.com>
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_IMPL_POSIX_TCPSTREAM_H
11 #define YARP_OS_IMPL_POSIX_TCPSTREAM_H
12 
13 // General files
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <unistd.h>
19 
20 #include <cstring>
21 
22 namespace yarp {
23 namespace os {
24 namespace impl {
25 namespace posix {
26 
27 
28 /* **************************************************************************************
29  * Interface of TcpStream
30  * **************************************************************************************/
31 
32 
33 class TcpStream
34 {
35 public:
40 
44  virtual ~TcpStream();
45 
46  inline ssize_t recv_n(void *buf, size_t n)
47  {
48  return ::recv(sd, buf, n, 0);
49  }
50 
51  inline ssize_t recv_n(void *buf, size_t n, struct timeval *tv)
52  {
53  setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char *>(tv), sizeof (*tv));
54  return ::recv(sd, buf, n, 0);
55  }
56 
57  inline ssize_t recv(void *buf, size_t n)
58  {
59  return ::recv(sd, buf, n, 0);
60  }
61 
62  inline ssize_t recv(void *buf, size_t n, struct timeval *tv)
63  {
64  setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char *>(tv), sizeof (*tv));
65  return ::recv(sd, buf, n, 0);
66  }
67 
68  inline ssize_t send_n(const void *buf, size_t n)
69  {
70  return ::send(sd, buf, n, 0);
71  }
72 
73  inline ssize_t send_n(const void *buf, size_t n, struct timeval *tv)
74  {
75  setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char *>(tv), sizeof (*tv));
76  return ::send(sd, buf, n, 0);
77  }
78 
79  // No idea what this should do...
80  void flush() { }
81 
82  void close_reader()
83  {
84  if (sd!=-1) {
85  ::shutdown(sd, SHUT_RD);
86  }
87  }
88 
89  void close_writer()
90  {
91  if (sd!=-1) {
92  ::shutdown(sd, SHUT_WR);
93  }
94  }
95 
96  void close()
97  {
98  if (sd!=-1) {
99  ::close(sd);
100  sd = -1;
101  }
102  }
103 
104  int open();
105 
106  int get_local_addr(sockaddr&);
107 
108  int get_remote_addr(sockaddr&);
109 
110  // get stream descriptor
111  int get_handle() { return sd; }
112 
113  // set stream descriptor
114  void set_handle(int h) { sd = h; }
115 
116  // Wrapper around the setsockopt system call.
117  inline int set_option(int level, int option, void *optval, int optlen) const
118  {
119  return setsockopt(sd, level, option, static_cast<char*>(optval), optlen);
120  }
121 
122  // Wrapper around the getsockopt system call.
123  inline int get_option(int level, int option, void *optval, int *optlen) const
124  {
125  return getsockopt(sd, level, option, static_cast<char*>(optval), reinterpret_cast<socklen_t*>(optlen));
126  }
127 private:
128  // stream descriptor
129  int sd {-1};
130 };
131 
132 } // namespace posix
133 } // namespace impl
134 } // namespace os
135 } // namespace yarp
136 
137 #endif // YARP_OS_IMPL_POSIX_TCPSTREAM_H
yarp::os::impl::posix::TcpStream::recv_n
ssize_t recv_n(void *buf, size_t n)
Definition: TcpStream.h:46
yarp::os::impl::posix::TcpStream::get_option
int get_option(int level, int option, void *optval, int *optlen) const
Definition: TcpStream.h:123
yarp::os::impl::posix::TcpStream::flush
void flush()
Definition: TcpStream.h:80
yarp::os::impl::posix::TcpStream::open
int open()
Definition: TcpStream.cpp:35
yarp::os::impl::posix::TcpStream::set_option
int set_option(int level, int option, void *optval, int optlen) const
Definition: TcpStream.h:117
yarp::os::impl::posix::TcpStream::get_handle
int get_handle()
Definition: TcpStream.h:111
yarp::os::impl::posix::TcpStream::close
void close()
Definition: TcpStream.h:96
yarp::os::impl::posix::TcpStream::~TcpStream
virtual ~TcpStream()
Destructor ~TcpStream.
yarp::os::impl::posix::TcpStream::recv_n
ssize_t recv_n(void *buf, size_t n, struct timeval *tv)
Definition: TcpStream.h:51
yarp::os::impl::posix::TcpStream::recv
ssize_t recv(void *buf, size_t n, struct timeval *tv)
Definition: TcpStream.h:62
yarp::os::impl::posix::TcpStream::get_local_addr
int get_local_addr(sockaddr &)
Definition: TcpStream.cpp:45
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
yarp::os::impl::posix::TcpStream::send_n
ssize_t send_n(const void *buf, size_t n, struct timeval *tv)
Definition: TcpStream.h:73
yarp::os::impl::posix::TcpStream::get_remote_addr
int get_remote_addr(sockaddr &)
Definition: TcpStream.cpp:54
yarp::os::impl::posix::TcpStream::TcpStream
TcpStream()
Constructor TcpStream.
yarp::os::impl::posix::TcpStream::set_handle
void set_handle(int h)
Definition: TcpStream.h:114
yarp::os::impl::posix::TcpStream::close_writer
void close_writer()
Definition: TcpStream.h:89
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::impl::posix::TcpStream
Definition: TcpStream.h:34
yarp::os::impl::posix::TcpStream::close_reader
void close_reader()
Definition: TcpStream.h:82
yarp::os::impl::posix::TcpStream::recv
ssize_t recv(void *buf, size_t n)
Definition: TcpStream.h:57
yarp::os::impl::posix::TcpStream::send_n
ssize_t send_n(const void *buf, size_t n)
Definition: TcpStream.h:68