Subversion Repositories HomeAutomation

Rev

Rev 1593 | Rev 1596 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1593 Rev 1595
Line 29... Line 29...
29
{
29
{
30
}
30
}
31
 
31
 
32
TcpClient::~TcpClient()
32
TcpClient::~TcpClient()
33
{
33
{
-
 
34
    if (this->socket_.is_open())
-
 
35
    {
-
 
36
        this->socket_.cancel();
34
    this->socket_.close();
37
        this->socket_.close();
-
 
38
    }
35
   
39
   
36
    if (this->acceptor_.use_count() != 0)
40
    if (this->acceptor_.use_count() != 0)
37
    {
41
    {
-
 
42
        this->acceptor_->cancel();
38
        this->acceptor_->close();
43
        this->acceptor_->close();
39
    }
44
    }
40
}
45
}
41
 
46
 
42
void TcpClient::Accept(AcceptorPointer acceptor)
47
void TcpClient::Accept(AcceptorPointer acceptor)
43
{
48
{
44
    try
49
    try
45
    {
50
    {
46
        this->acceptor_ = acceptor;
51
        this->acceptor_ = acceptor;
Line 52... Line 57...
52
    }
57
    }
53
    catch (std::exception e)
58
    catch (std::exception e)
54
    {
59
    {
55
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
60
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
56
    }
61
    }
57
}
62
}
58
 
63
 
59
void TcpClient::AcceptHandler(const boost::system::error_code& error)
64
void TcpClient::AcceptHandler(const boost::system::error_code& error)
60
{
65
{
61
    this->Read();
66
    this->Read();
62
   
67
   
Line 66... Line 71...
66
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
71
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
67
{
72
{
68
    AcceptorPointer acceptor = this->acceptor_;
73
    AcceptorPointer acceptor = this->acceptor_;
69
    this->acceptor_.reset();
74
    this->acceptor_.reset();
70
    return acceptor;
75
    return acceptor;
71
}
76
}
72
 
77
 
73
void TcpClient::Connect(std::string address, unsigned int port)
78
void TcpClient::Connect(std::string address, unsigned int port)
74
{
79
{
75
    try
80
    try
76
    {
81
    {
Line 81... Line 86...
81
        this->socket_.connect(*it);
86
        this->socket_.connect(*it);
82
    }
87
    }
83
    catch (std::exception e)
88
    catch (std::exception e)
84
    {
89
    {
85
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + std::string(e.what()));
90
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + std::string(e.what()));
86
    }
91
    }
87
   
92
   
88
    this->Read();
93
    this->Read();
89
}
94
}
90
 
95
 
91
void TcpClient::Disconnect()
96
void TcpClient::Disconnect()
92
{
97
{
-
 
98
    if (this->socket_.is_open())
-
 
99
    {
-
 
100
        this->socket_.cancel();
93
    this->socket_.close();
101
        this->socket_.close();
-
 
102
    }
94
   
103
   
95
    if (this->acceptor_.use_count() != 0)
104
    if (this->acceptor_.use_count() != 0)
96
    {
105
    {
-
 
106
        this->acceptor_->cancel();
97
        this->acceptor_->close();
107
        this->acceptor_->close();
98
    }
108
    }
99
   
109
   
100
    Client::Disconnect();
110
    Client::Disconnect();
101
}
111
}