Go to the documentation of this file.00001
00012 #ifndef TCP_H_
00013 #define TCP_H_
00014
00015 #include <iostream>
00016 #include <string>
00017 #include <boost/asio.hpp>
00018 #include <boost/system/system_error.hpp>
00019 #include <boost/bind.hpp>
00020
00021 #define ERROR_CODE_LOST_SOCKET 2
00022
00023
00024 namespace apps
00025 {
00026
00027 using boost::asio::ip::tcp;
00028
00029 class TCP
00030 {
00031 private:
00032
00033 enum _mode {server,client};
00034 _mode _choosenMode;
00035
00036 unsigned int _delay;
00037
00038 bool _requestNewConnection;
00039
00040 boost::asio::io_service _ioService;
00041 unsigned int _port;
00042 std::string _ip;
00043
00044 boost::asio::ip::tcp::endpoint* _targetServer;
00045
00046 boost::asio::ip::tcp::socket* _socket;
00047
00048 tcp::acceptor* _server;
00049
00050
00051 boost::asio::deadline_timer* _timer;
00052
00053 public:
00054 TCP(unsigned int port);
00055 TCP(std::string ip, unsigned int port);
00056 virtual ~TCP();
00057
00058 int connectOnce();
00059
00069 int read(void* data, unsigned int size);
00070
00081 int readAll(void* data, unsigned int size);
00082
00092 int read(void* data, unsigned int size, unsigned int delay_us);
00093
00094 int write(void* data, unsigned int size);
00095
00096 private:
00111 void read_callback(bool& data_available, boost::asio::deadline_timer& timeout,
00112 const boost::system::error_code& error,
00113 std::size_t bytes_transferred);
00114
00127 void wait_callback(boost::asio::ip::tcp::socket& ser_port,
00128 const boost::system::error_code& error);
00129
00130 };
00131
00132 }
00133 #endif