udp.h
Go to the documentation of this file.
00001 
00009 /*
00010  * Copyright 2013,2014 Vladimir Ermakov.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 3 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00019  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00020  * for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License along
00023  * with this program; if not, write to the Free Software Foundation, Inc.,
00024  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00025  */
00026 
00027 #pragma once
00028 
00029 #include <list>
00030 #include <atomic>
00031 #include <boost/asio.hpp>
00032 #include <mavconn/interface.h>
00033 #include <mavconn/msgbuffer.h>
00034 
00035 namespace mavconn {
00036 
00042 class MAVConnUDP : public MAVConnInterface {
00043 public:
00050         MAVConnUDP(uint8_t system_id = 1, uint8_t component_id = MAV_COMP_ID_UDP_BRIDGE,
00051                         std::string bind_host = "localhost", unsigned short bind_port = 14555,
00052                         std::string remote_host = "", unsigned short remote_port = 14550);
00053         ~MAVConnUDP();
00054 
00055         void close();
00056 
00057         using MAVConnInterface::send_message;
00058         void send_message(const mavlink_message_t *message, uint8_t sysid, uint8_t compid);
00059         void send_bytes(const uint8_t *bytes, size_t length);
00060 
00061         inline mavlink_status_t get_status() { return *mavlink_get_channel_status(channel); };
00062         inline bool is_open() { return socket.is_open(); };
00063 
00064 private:
00065         boost::asio::io_service io_service;
00066         std::unique_ptr<boost::asio::io_service::work> io_work;
00067         std::thread io_thread;
00068 
00069         std::atomic<bool> remote_exists;
00070         boost::asio::ip::udp::socket socket;
00071         boost::asio::ip::udp::endpoint remote_ep;
00072         boost::asio::ip::udp::endpoint last_remote_ep;
00073         boost::asio::ip::udp::endpoint bind_ep;
00074 
00075         std::atomic<bool> tx_in_progress;
00076         std::list<MsgBuffer*> tx_q;
00077         uint8_t rx_buf[MsgBuffer::MAX_SIZE];
00078         std::recursive_mutex mutex;
00079 
00080         void do_recvfrom();
00081         void async_receive_end(boost::system::error_code, size_t bytes_transferred);
00082         void do_sendto(bool check_tx_state);
00083         void async_sendto_end(boost::system::error_code, size_t bytes_transferred);
00084 };
00085 
00086 }; // namespace mavconn
00087 


libmavconn
Author(s): Vladimir Ermakov
autogenerated on Wed Aug 26 2015 12:29:08