Subversion Repositories HomeAutomation

Rev

Rev 1310 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1310 Rev 1314
Line 45... Line 45...
45
    return udpServer;
45
    return udpServer;
46
}
46
}
47
 
47
 
48
udp::endpoint UdpServer::getEndpoint(string address)
48
udp::endpoint UdpServer::getEndpoint(string address)
49
{
49
{
50
    LOG.info("Resolving " + address + ":" + itos(this->getPort()) + "...");
50
    LOG.info("Resolving " + address + ":" + convert::int2string(this->getPort()) + "...");
51
    udp::resolver resolver(this->myIoService);
51
    udp::resolver resolver(this->myIoService);
52
    udp::resolver::query query(udp::v4(), address, itos(this->getPort()));
52
    udp::resolver::query query(udp::v4(), address, convert::int2string(this->getPort()));
53
    return *resolver.resolve(query);
53
    return *resolver.resolve(query);
54
}
54
}
55
 
55
 
56
unsigned int UdpServer::getPort()
56
unsigned int UdpServer::getPort()
57
{
57
{
Line 60... Line 60...
60
 
60
 
61
void UdpServer::sendTo(udp::endpoint receiver, byte_list bytes)
61
void UdpServer::sendTo(udp::endpoint receiver, byte_list bytes)
62
{
62
{
63
    try
63
    try
64
    {
64
    {
65
        LOG.info("Sending packet, length " + itos(bytes.size()) + ", to " + receiver.address().to_string() + ":" + itos(this->getPort()) + "...");
65
        LOG.info("Sending packet, length " + convert::int2string(bytes.size()) + ", to " + receiver.address().to_string() + ":" + convert::int2string(this->getPort()) + "...");
66
        this->mySocket.send_to(boost::asio::buffer(bytes), receiver);
66
        this->mySocket.send_to(boost::asio::buffer(bytes), receiver);
67
    }
67
    }
68
    catch (std::exception& e)
68
    catch (std::exception& e)
69
    {
69
    {
70
        LOG.error(e.what());
70
        LOG.error(e.what());
71
    }
71
    }
72
}
72
}
73
 
73
 
74
void UdpServer::receiveFrom()
74
void UdpServer::receiveFrom()
75
{
75
{
76
    LOG.info("Listening for incoming data on port " + itos(this->getPort()) + "...");
76
    LOG.info("Listening for incoming data on port " + convert::int2string(this->getPort()) + "...");
77
    this->mySocket.async_receive_from(boost::asio::buffer(this->myDataBuffer, MAX_LENGTH), this->mySender, boost::bind(&UdpServer::handleReceiveFrom, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
77
    this->mySocket.async_receive_from(boost::asio::buffer(this->myDataBuffer, MAX_LENGTH), this->mySender, boost::bind(&UdpServer::handleReceiveFrom, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
78
}
78
}
79
 
79
 
80
void UdpServer::handleReceiveFrom(const boost::system::error_code& error, size_t bytesReceived)
80
void UdpServer::handleReceiveFrom(const boost::system::error_code& error, size_t bytesReceived)
81
{
81
{
82
    if (error)
82
    if (error)
83
    {
83
    {
84
        LOG.error("handleReceiveFrom got the error code " + itos(error.value()) + " and the message " + error.message());
84
        LOG.error("handleReceiveFrom got the error code " + convert::int2string(error.value()) + " and the message " + error.message());
85
    }
85
    }
86
    else if (bytesReceived == 0)
86
    else if (bytesReceived == 0)
87
    {
87
    {
88
        LOG.info("handleReceiveFrom got 0 bytes.");
88
        LOG.info("handleReceiveFrom got 0 bytes.");
89
    }
89
    }
90
    else
90
    else
91
    {
91
    {
92
        LOG.info("Got " + itos(bytesReceived) + " bytes from " + this->mySender.address().to_string());
92
        LOG.info("Got " + convert::int2string(bytesReceived) + " bytes from " + this->mySender.address().to_string());
93
 
93
 
94
        byte_list bytes(this->myDataBuffer, this->myDataBuffer + bytesReceived);
94
        byte_list bytes(this->myDataBuffer, this->myDataBuffer + bytesReceived);
95
 
95
 
96
        this->onNewData(this->mySender, bytes);
96
        this->onNewData(this->mySender, bytes);
97
    }
97
    }