staubli_driver_ros2 main
ROS2 control driver for Staubli robots
Loading...
Searching...
No Matches
socket.hpp
Go to the documentation of this file.
1// Copyright 2025 ICUBE Laboratory, University of Strasbourg
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Author: Thibault Poignonec (thibault.poignonec@gmail.fr)
16
17#ifndef STAUBLI_ROBOT_DRIVER__COMMUNICATION__SOCKET_HPP_
18#define STAUBLI_ROBOT_DRIVER__COMMUNICATION__SOCKET_HPP_
19
20#include <string>
21#include <vector>
22#include <functional>
23#include <memory>
24
25namespace staubli_robot_driver {
26
30constexpr size_t MAX_SOCKET_PACKET_SIZE = 1024;
31
38class Socket {
39public:
43 Socket() = default;
44
48 virtual ~Socket() = default;
49
57 virtual bool connect(
58 const std::string& remote_address, uint16_t remote_port, uint16_t local_port) = 0;
59
68 virtual bool connect(
69 const std::string& remote_address, uint16_t remote_port,
70 const std::string& local_address, uint16_t local_port) = 0;
71
76 virtual bool disconnect() = 0;
77
82 virtual bool is_connected() const = 0;
83
89 virtual bool send(std::vector<uint8_t>& data) = 0;
90
100 virtual bool receive_once(int timeout_ms, std::vector<uint8_t>& data) = 0;
101
108 std::function<void(
109 std::vector<uint8_t>& /*reception buffer*/,
110 size_t /*bytes_transferred*/)> reception_callback) = 0;
111
116 virtual bool stop_receive_thread() = 0;
117
122 virtual bool is_receiving() const = 0;
123};
124
129public:
133 enum class ProtocolType {
134 UDP,
135 TCP,
136 };
137
143 static std::shared_ptr<Socket> create(ProtocolType type);
144};
145
146} // namespace staubli_robot_driver
147
148#endif // STAUBLI_ROBOT_DRIVER__COMMUNICATION__SOCKET_HPP_
Factory for creating communication interfaces.
Definition socket.hpp:128
static std::shared_ptr< Socket > create(ProtocolType type)
Create a communication interface.
ProtocolType
Communication protocol types.
Definition socket.hpp:133
Abstract interface for communication with the robot.
Definition socket.hpp:38
virtual bool connect(const std::string &remote_address, uint16_t remote_port, const std::string &local_address, uint16_t local_port)=0
Connect to the remote endpoint with specific local address.
virtual bool receive_once(int timeout_ms, std::vector< uint8_t > &data)=0
Receive data from socket (blocking with timeout)
virtual bool is_receiving() const =0
Check if the receive thread is running.
Socket()=default
Default constructor.
virtual bool start_receive_thread(std::function< void(std::vector< uint8_t > &, size_t)> reception_callback)=0
Start a receive thread that calls the provided reception_callback when data is received.
virtual bool send(std::vector< uint8_t > &data)=0
Send data to the remote endpoint.
virtual ~Socket()=default
Virtual destructor.
virtual bool connect(const std::string &remote_address, uint16_t remote_port, uint16_t local_port)=0
Connect to the remote endpoint.
virtual bool disconnect()=0
Disconnect from the remote endpoint.
virtual bool is_connected() const =0
Check if the connection is established.
virtual bool stop_receive_thread()=0
Stop the receive thread.
Definition messages.hpp:23
constexpr size_t MAX_SOCKET_PACKET_SIZE
Maximum packet size (bytes) for socket communication.
Definition socket.hpp:30