Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1309 | runge | 1 | /* |
| 2 | * UdpConnection.cpp |
||
| 3 | * |
||
| 4 | * Created on: Jul 17, 2009 |
||
| 5 | * Author: Mattias Runge |
||
| 6 | */ |
||
| 7 | |||
| 8 | #include "UdpConnection.h" |
||
| 9 | |||
| 10 | namespace atom { |
||
| 11 | namespace utils { |
||
| 12 | |||
| 13 | UdpConnection::UdpConnection(string ip, unsigned int port) |
||
| 14 | { |
||
| 15 | LOG.setName("UdpConnection"); |
||
| 16 | |||
| 17 | this->_ip = ip; |
||
| 18 | this->_port = port; |
||
| 19 | |||
| 20 | // TODO Start connect/reconnect-loop, we can not block though |
||
| 21 | |||
| 22 | } |
||
| 23 | |||
| 24 | UdpConnection::~UdpConnection() |
||
| 25 | { |
||
| 26 | this->disconnect(); |
||
| 27 | } |
||
| 28 | |||
| 29 | void |
||
| 30 | UdpConnection::connect() |
||
| 31 | { |
||
| 32 | LOG.info("Connecting..."); |
||
| 33 | |||
| 34 | try |
||
| 35 | { |
||
| 36 | boost::asio::io_service ioService; |
||
| 37 | |||
| 38 | udp::resolver resolver(ioService); |
||
| 39 | |||
| 40 | LOG.info("Resolving " + this->_ip + ":" + itos(this->_port) + "..."); |
||
| 41 | |||
| 42 | udp::resolver::query query(udp::v4(), this->_ip, itos(this->_port)); |
||
| 43 | |||
| 44 | this->_receiverEndpoint = *resolver.resolve(query); |
||
| 45 | |||
| 46 | LOG.info("Creating socket..."); |
||
| 47 | this->_outSocket = new udp::socket(ioService); |
||
| 48 | |||
| 49 | LOG.info("Opening socket..."); |
||
| 50 | this->_outSocket->open(udp::v4()); |
||
| 51 | |||
| 52 | LOG.info("Connected."); |
||
| 53 | } |
||
| 54 | catch (std::exception& e) |
||
| 55 | { |
||
| 56 | LOG.error(e.what()); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | void |
||
| 61 | UdpConnection::disconnect() |
||
| 62 | { |
||
| 63 | LOG.info("Disconnecting..."); |
||
| 64 | |||
| 65 | if (this->_outSocket != NULL) |
||
| 66 | { |
||
| 67 | this->_outSocket->close(); |
||
| 68 | delete this->_outSocket; |
||
| 69 | this->_outSocket = NULL; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (this->_inSocket != NULL) |
||
| 73 | { |
||
| 74 | this->_inSocket->close(); |
||
| 75 | delete this->_inSocket; |
||
| 76 | this->_inSocket = NULL; |
||
| 77 | } |
||
| 78 | |||
| 79 | LOG.info("Disconnected."); |
||
| 80 | } |
||
| 81 | |||
| 82 | void |
||
| 83 | UdpConnection::incomming(BitBuffer buffer) |
||
| 84 | { |
||
| 85 | LOG.info("Received packet from socket."); |
||
| 86 | } |
||
| 87 | |||
| 88 | void |
||
| 89 | UdpConnection::write(BitBuffer buffer) |
||
| 90 | { |
||
| 91 | LOG.info("Writing buffer to socket..."); |
||
| 92 | |||
| 93 | try |
||
| 94 | { |
||
| 95 | this->_outSocket->send_to(boost::asio::buffer(buffer.getAsString()), |
||
| 96 | this->_receiverEndpoint); |
||
| 97 | } |
||
| 98 | catch (std::exception& e) |
||
| 99 | { |
||
| 100 | LOG.error(e.what()); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | void |
||
| 105 | UdpConnection::run() |
||
| 106 | { |
||
| 107 | boost::asio::io_service ioService; |
||
| 108 | |||
| 109 | try |
||
| 110 | { |
||
| 111 | this->_inSocket = new udp::socket(ioService, udp::endpoint(udp::v4(), |
||
| 112 | this->_port)); |
||
| 113 | |||
| 114 | char data[1024]; |
||
| 115 | udp::endpoint sender_endpoint; |
||
| 116 | BitBuffer buffer; |
||
| 117 | |||
| 118 | while (true) |
||
| 119 | { |
||
| 120 | size_t length = this->_inSocket->receive_from(boost::asio::buffer( |
||
| 121 | data, 1024), sender_endpoint); |
||
| 122 | |||
| 123 | buffer.setFromString(string(data, length)); |
||
| 124 | |||
| 125 | this->incomming(buffer); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | catch (std::exception& e) |
||
| 129 | { |
||
| 130 | LOG.error(e.what()); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | } |
||
| 135 | } |