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