Apollo 10.0
自动驾驶开放平台
apollo::drivers::gnss::TcpStream类 参考

#include <tcp_stream.h>

类 apollo::drivers::gnss::TcpStream 继承关系图:
apollo::drivers::gnss::TcpStream 的协作图:

Public 成员函数

 TcpStream (const char *address, uint16_t port, uint32_t timeout_usec, bool auto_reconnect=true)
 
 ~TcpStream ()
 
virtual bool Connect ()
 
virtual bool Disconnect ()
 
virtual size_t read (uint8_t *buffer, size_t max_length)
 
virtual size_t write (const uint8_t *data, size_t length)
 
- Public 成员函数 继承自 apollo::drivers::gnss::Stream
virtual ~Stream ()
 
Status get_status () const
 
void RegisterLoginData (const std::vector< std::string > login_data)
 
void Login ()
 
size_t write (const std::string &buffer)
 

额外继承的成员函数

- Public 类型 继承自 apollo::drivers::gnss::Stream
enum class  Status { DISCONNECTED , CONNECTED , ERROR }
 
- 静态 Public 成员函数 继承自 apollo::drivers::gnss::Stream
static Streamcreate_tcp (const char *address, uint16_t port, uint32_t timeout_usec=1000000)
 
static Streamcreate_udp (const char *address, uint16_t port, uint32_t timeout_usec=1000000)
 
static Streamcreate_serial (const char *device_name, uint32_t baud_rate, uint32_t timeout_usec=0)
 
static Streamcreate_ntrip (const std::string &address, uint16_t port, const std::string &mountpoint, const std::string &user, const std::string &passwd, uint32_t timeout_s=30)
 
static Streamcreate_can (const apollo::drivers::canbus::CANCardParameter &parameter)
 
- 静态 Public 属性 继承自 apollo::drivers::gnss::Stream
static constexpr size_t NUM_STATUS
 
- Protected 成员函数 继承自 apollo::drivers::gnss::Stream
 Stream ()
 
- Protected 属性 继承自 apollo::drivers::gnss::Stream
Status status_ = Status::DISCONNECTED
 

详细描述

在文件 tcp_stream.h23 行定义.

构造及析构函数说明

◆ TcpStream()

apollo::drivers::gnss::TcpStream::TcpStream ( const char *  address,
uint16_t  port,
uint32_t  timeout_usec,
bool  auto_reconnect = true 
)

在文件 tcp_stream.cc37 行定义.

39 : sockfd_(-1), errno_(0), auto_reconnect_(auto_reconnect) {
40 peer_addr_ = inet_addr(address);
41 peer_port_ = htons(port);
42 timeout_usec_ = timeout_usec;
43}

◆ ~TcpStream()

apollo::drivers::gnss::TcpStream::~TcpStream ( )

在文件 tcp_stream.cc45 行定义.

45{ this->close(); }

成员函数说明

◆ Connect()

bool apollo::drivers::gnss::TcpStream::Connect ( )
virtual

实现了 apollo::drivers::gnss::Stream.

在文件 tcp_stream.cc141 行定义.

141 {
142 if (sockfd_ < 0) {
143 this->open();
144 if (sockfd_ < 0) {
145 // error
146 return false;
147 }
148 }
149
151 return true;
152 }
153
154 fd_set fds;
155 timeval timeo = {10, 0};
156 int ret = 0;
157 sockaddr_in peer_addr;
158
159 bzero(&peer_addr, sizeof(peer_addr));
160 peer_addr.sin_family = AF_INET;
161 peer_addr.sin_port = peer_port_;
162 peer_addr.sin_addr.s_addr = peer_addr_;
163
164 int fd_flags = fcntl(sockfd_, F_GETFL);
165 if (fd_flags < 0 || fcntl(sockfd_, F_SETFL, fd_flags | O_NONBLOCK) < 0) {
166 AERROR << "Failed to set noblock, error: " << strerror(errno);
167 return false;
168 }
169
170 while ((ret = ::connect(sockfd_, reinterpret_cast<sockaddr*>(&peer_addr),
171 sizeof(peer_addr))) < 0) {
172 if (errno == EINTR) {
173 AINFO << "Tcp connect return EINTR, continue.";
174 continue;
175 } else {
176 if ((errno != EISCONN) && (errno != EINPROGRESS) && (errno != EALREADY)) {
178 errno_ = errno;
179 AERROR << "Connect failed, error: " << strerror(errno);
180 return false;
181 }
182
183 FD_ZERO(&fds);
184 FD_SET(sockfd_, &fds);
185 ret = select(sockfd_ + 1, NULL, &fds, NULL, &timeo);
186 if (ret < 0) {
188 errno_ = errno;
189 AERROR << "Wait connect failed, error: " << strerror(errno);
190 return false;
191 } else if (ret == 0) {
192 AINFO << "Tcp connect timeout.";
193 return false;
194 } else if (FD_ISSET(sockfd_, &fds)) {
195 int error = 0;
196 socklen_t len = sizeof(int);
197
198 if (getsockopt(sockfd_, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
200 errno_ = errno;
201 AERROR << "Getsockopt failed, error: " << strerror(errno);
202 return false;
203 }
204 if (error != 0) {
206 errno_ = errno;
207 AERROR << "Socket error: " << strerror(errno);
208 return false;
209 }
210
211 // connect successfully
212 break;
213 } else {
215 errno_ = errno;
216 AERROR << "Should not be here.";
217 return false;
218 }
219 }
220 }
221
222 if (!InitSocket()) {
223 close();
225 errno_ = errno;
226 AERROR << "Failed to init socket.";
227 return false;
228 }
229 AINFO << "Tcp connect success.";
231 Login();
232 return true;
233}
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42

◆ Disconnect()

bool apollo::drivers::gnss::TcpStream::Disconnect ( )
virtual

实现了 apollo::drivers::gnss::Stream.

在文件 tcp_stream.cc235 行定义.

235 {
236 if (sockfd_ < 0) {
237 // not open
238 return false;
239 }
240
241 this->close();
242 return true;
243}

◆ read()

size_t apollo::drivers::gnss::TcpStream::read ( uint8_t *  buffer,
size_t  max_length 
)
virtual

实现了 apollo::drivers::gnss::Stream.

在文件 tcp_stream.cc245 行定义.

245 {
246 ssize_t ret = 0;
247
249 Reconnect();
251 return 0;
252 }
253 }
254
255 if (!Readable(10000)) {
256 return 0;
257 }
258
259 while ((ret = ::recv(sockfd_, buffer, max_length, 0)) < 0) {
260 if (errno == EINTR) {
261 continue;
262 } else {
263 // error
264 if (errno != EAGAIN) {
266 errno_ = errno;
267 AERROR << "Read errno " << errno << ", error " << strerror(errno);
268 }
269 }
270
271 return 0;
272 }
273
274 if (ret == 0) {
276 errno_ = errno;
277 AERROR << "Remote closed.";
278 if (Reconnect()) {
279 AINFO << "Reconnect tcp success.";
280 }
281 }
282
283 return ret;
284}

◆ write()

size_t apollo::drivers::gnss::TcpStream::write ( const uint8_t *  data,
size_t  length 
)
virtual

实现了 apollo::drivers::gnss::Stream.

在文件 tcp_stream.cc286 行定义.

286 {
287 size_t total_nsent = 0;
288
290 Reconnect();
292 return 0;
293 }
294 }
295
296 while (length > 0) {
297 ssize_t nsent = ::send(sockfd_, buffer, length, 0);
298 if (nsent < 0) {
299 if (errno == EINTR) {
300 continue;
301 } else {
302 // error
303 if (errno == EPIPE || errno == ECONNRESET) {
305 errno_ = errno;
306 } else if (errno != EAGAIN) {
308 errno_ = errno;
309 }
310 return total_nsent;
311 }
312 }
313
314 total_nsent += nsent;
315 length -= nsent;
316 buffer += nsent;
317 }
318
319 return total_nsent;
320}

该类的文档由以下文件生成: