-
Notifications
You must be signed in to change notification settings - Fork 0
/
baglanti.cpp
72 lines (60 loc) · 3.47 KB
/
baglanti.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "baglanti.hpp"
#include <vector>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/lexical_cast.hpp>
baglanti::baglanti ( boost::shared_ptr<boost::asio::io_service>& sp_io_service, const std::string& a3nwan, const std::string& madkhal )
{
boost::asio::ip::udp::resolver resolver ( *sp_io_service );
boost::asio::ip::udp::resolver::query query ( boost::asio::ip::udp::v4(), a3nwan, madkhal );
sp_uzak_bitishnoktasi_.reset ( new boost::asio::ip::udp::endpoint ( *resolver.resolve ( query ) ) );
sp_priz.reset ( new boost::asio::ip::udp::socket ( *sp_io_service, boost::asio::ip::udp::endpoint ( boost::asio::ip::udp::v4(), boost::lexical_cast<short> ( madkhal ) ) ) );
sp_priz->set_option ( boost::asio::ip::udp::socket::reuse_address ( true ) );
sp_strand_.reset ( new boost::asio::io_service::strand ( *sp_io_service ) );
}
void baglanti::bashla()
{
boost::shared_ptr<boost::array<char, 8192> > sp_bffr_arry ( new boost::array<char, 8192> );
vctr_sp_gelen_buffer_.push_back ( sp_bffr_arry );
sp_priz->async_receive_from ( boost::asio::buffer ( *sp_bffr_arry ), *sp_uzak_bitishnoktasi_,
sp_strand_->wrap ( boost::bind ( &baglanti::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, sp_bffr_arry ) ) );
}
void baglanti::sendAMsg ( std::string& msgo )
{
sp_priz->async_send_to ( boost::asio::buffer ( msgo ), *sp_uzak_bitishnoktasi_,
sp_strand_->wrap ( boost::bind ( &baglanti::handle_send_to, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ) );
}
void baglanti::handle_receive_from ( const boost::system::error_code& error, std::size_t bytes_transferred , boost::shared_ptr<boost::array<char, 8192> > sp_ba )
{
if ( vctr_sp_gelen_buffer_.size() < 20 ) {
boost::shared_ptr<boost::array<char, 8192> > sp_bffr_arry ( new boost::array<char, 8192> );
vctr_sp_gelen_buffer_.push_back ( sp_bffr_arry );
sp_priz->async_receive_from ( boost::asio::buffer ( *sp_bffr_arry ), *sp_uzak_bitishnoktasi_,
sp_strand_->wrap ( boost::bind ( &baglanti::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred , sp_bffr_arry ) ) );
}
if ( !error && bytes_transferred > 0 ) {
std::cout << "received :" << sp_ba->c_array() << std::endl;
}
for ( std::vector<boost::shared_ptr<boost::array<char, 8192> > >::iterator it = vctr_sp_gelen_buffer_.begin(); it != vctr_sp_gelen_buffer_.end(); ++it ) {
//std::cout << "it contents :" << (*it)->c_array() << std::endl;
if ( *it == sp_ba ) {
//std::cout << "erasing :" << (*it)->c_array() << std::endl;
vctr_sp_gelen_buffer_.erase ( it );
//(*it).reset();
break;
}
}
sp_ba.reset();
}
void baglanti::handle_send_to ( const boost::system::error_code& error, std::size_t bytes_sent )
{
if ( !error && bytes_sent > 0 ) {
std::cout << "sent: " << bytes_sent << std::endl;
}
}