sl_tcp_channel.cpp
Go to the documentation of this file.
1 /*
2  * Slamtec LIDAR SDK
3  *
4  * Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd.
5  * http://www.slamtec.com
6  *
7  */
8  /*
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include "sl_lidar_driver.h"
34 #include "hal/abs_rxtx.h"
35 #include "hal/socket.h"
36 
37 
38 namespace sl {
39 
40  class TcpChannel : public IChannel
41  {
42  public:
43  TcpChannel(const std::string& ip, int port) : _binded_socket(rp::net::StreamSocket::CreateSocket()) {
44  _ip = ip;
45  _port = port;
46  }
47 
48  bool bind(const std::string & ip, sl_s32 port)
49  {
50  _socket = rp::net::SocketAddress(ip.c_str(), port);
51  return SL_RESULT_OK;
52  }
53 
54  bool open()
55  {
56  if(SL_IS_FAIL(bind(_ip, _port)))
57  return false;
59 
60  }
61 
62  void close()
63  {
65  _binded_socket = NULL;
66  }
67  void flush()
68  {
69 
70  }
71 
72  bool waitForData(size_t size, sl_u32 timeoutInMs, size_t* actualReady)
73  {
74  if (actualReady)
75  *actualReady = size;
76  return (_binded_socket->waitforData(timeoutInMs) == RESULT_OK);
77 
78  }
79 
80  int write(const void* data, size_t size)
81  {
82  return _binded_socket->send(data, size);
83  }
84 
85  int read(void* buffer, size_t size)
86  {
87  size_t lenRec = 0;
88  _binded_socket->recv(buffer, size, lenRec);
89  return lenRec;
90  }
91 
92  void clearReadCache() {}
93 
95  private:
98  std::string _ip;
99  int _port;
100  };
101  Result<IChannel*> createTcpChannel(const std::string& ip, int port)
102  {
103  return new TcpChannel(ip, port);
104  }
105 }
#define IS_OK(x)
Definition: rptypes.h:113
bool bind(const std::string &ip, sl_s32 port)
Definition: sl_crc.h:38
rp::net::SocketAddress _socket
rp::net::StreamSocket * _binded_socket
#define RESULT_OK
Definition: rptypes.h:102
virtual void dispose()=0
sl_u8 size
std::string _ip
sl_u8 flag
Definition: sl_lidar_cmd.h:43
bool waitForData(size_t size, sl_u32 timeoutInMs, size_t *actualReady)
virtual u_result send(const void *buffer, size_t len)=0
virtual u_result waitforData(_u32 timeout=DEFAULT_SOCKET_TIMEOUT)=0
int write(const void *data, size_t size)
virtual u_result recv(void *buf, size_t len, size_t &recv_len)=0
uint32_t _u32
Definition: rptypes.h:69
TcpChannel(const std::string &ip, int port)
virtual u_result connect(const SocketAddress &pairAddress)=0
void setStatus(_u32 flag)
#define SL_IS_FAIL(x)
Definition: sl_types.h:83
#define SL_RESULT_OK
Definition: sl_types.h:71
Result< IChannel * > createTcpChannel(const std::string &ip, int port)
int read(void *buffer, size_t size)
sl_u8 data[0]


rplidar_ros
Author(s):
autogenerated on Tue Jun 13 2023 02:07:38