-
Notifications
You must be signed in to change notification settings - Fork 9
/
Connection.cpp
134 lines (112 loc) · 3.35 KB
/
Connection.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <phpcpp.h>
#include "azure_c_shared_utility/platform.h"
#include "azure_c_shared_utility/tlsio.h"
#include "azure_c_shared_utility/socketio.h"
#include "azure_uamqp_c/uamqp.h"
#include "Connection.h"
#include "Session.h"
#include "Producer.h"
#include "Consumer.h"
#include "Message.h"
void Connection::__construct(Php::Parameters ¶ms)
{
host = params[0].stringValue();
port = params[1].numericValue();
useTls = params[2].boolValue();
keyName = params[3].stringValue();
key = params[4].stringValue();
debug = params.size() == 6 ? params[5].boolValue() : false;
}
void Connection::connect()
{
if (isConnected) {
return;
}
bool useAuth = !keyName.empty() && !key.empty();
if (platform_init() != 0) {
//throw Php::Exception("Could not run platform_init");
}
if (useTls) {
tls_io_config = { host.c_str(), port };
/* create the TLS IO */
tlsio_interface = platform_get_default_tlsio();
tls_io = xio_create(tlsio_interface, &tls_io_config);
} else {
socketio_config = { host.c_str(), port, NULL };
socket_io = xio_create(socketio_get_interface_description(), &socketio_config);
}
if (useAuth) {
sasl_plain_config = { keyName.c_str(), key.c_str(), NULL };
/* create SASL PLAIN handler */
sasl_mechanism_handle = saslmechanism_create(saslplain_get_interface(), &sasl_plain_config);
/* create the SASL client IO using the TLS IO or SOCKET OI */
if (useTls) {
sasl_io_config.underlying_io = tls_io;
} else {
sasl_io_config.underlying_io = socket_io;
}
sasl_io_config.sasl_mechanism = sasl_mechanism_handle;
sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config);
}
/* create the connection */
connection = connection_create(useAuth ? sasl_io : socket_io, host.c_str(), "some", NULL, NULL);
if (isDebugOn()) {
connection_set_trace(connection, true);
}
// Session
session = new Session(this);
isConnected = true;
}
void Connection::publish(Php::Parameters ¶ms)
{
connect();
std::string resourceName = params[0].stringValue();
Message *message = (Message*) params[1].implementation();
Producer *producer = new Producer(session, resourceName);
producer->publish(message);
}
void Connection::setCallback(Php::Parameters ¶ms)
{
connect();
std::string resourceName = params[0].stringValue();
Php::Value callback = params[1];
Php::Value loopFn = params[2];
consumer = new Consumer(session, resourceName);
consumer->setCallback(callback, loopFn);
}
void Connection::consume()
{
if (consumer != NULL) {
consumer->consume();
}
}
std::string Connection::getHost()
{
return host;
}
CONNECTION_HANDLE Connection::getConnectionHandler()
{
return connection;
}
void Connection::doWork()
{
connection_dowork(connection);
}
bool Connection::isDebugOn()
{
return debug;
}
void Connection::close()
{
if (consumer != NULL && !consumer->wasCloseRequested()) {
consumer->close();
}
if (!closeRequested) {
closeRequested = true;
connection_destroy(connection);
xio_destroy(sasl_io);
xio_destroy(tls_io);
saslmechanism_destroy(sasl_mechanism_handle);
platform_deinit();
}
}