forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathexported_connection.cpp
More file actions
81 lines (68 loc) · 2.99 KB
/
exported_connection.cpp
File metadata and controls
81 lines (68 loc) · 2.99 KB
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
#if !defined(_WIN32)
#include <unistd.h>
#endif
#include "exported_connection.hpp"
#include "connection.hpp"
namespace datastax { namespace internal { namespace core {
ExportedConnection::ExportedConnection(SharedRefPtr<Connection> connection) {
// Connection
this->host = connection->host();
this->listener_ = connection->listener_;
// We don't want to notify higher layers of code
// that connection has been closed.
connection->set_listener();
this->protocol_version_ = connection->protocol_version();
this->keyspace_ = connection->keyspace();
this->shard_id_ = connection->shard_id();
this->idle_timeout_secs_ = connection->idle_timeout_secs_;
this->heartbeat_interval_secs_ = connection->heartbeat_interval_secs_;
// Socket
#if defined(_WIN32)
uv_fileno(reinterpret_cast<uv_handle_t *>(&connection->socket_->tcp_), reinterpret_cast<uv_os_fd_t *>(&this->fd));
WSAPROTOCOL_INFOW info;
WSADuplicateSocketW(this->fd, GetCurrentProcessId(), &info);
this->fd = WSASocketW(info.iAddressFamily, info.iSocketType, info.iProtocol, &info, 0, WSA_FLAG_OVERLAPPED);
#else
uv_fileno(reinterpret_cast<uv_handle_t *>(&connection->socket_->tcp_), &this->fd);
this->fd = dup(this->fd);
#endif
this->handler_ = connection->socket_->handler_.release();
// Set basic handler, to notify Connection about closing, and destroy it.
connection->socket_->set_handler(new ConnectionHandler(connection.get()));
this->is_defunct_ = connection->socket_->is_defunct();
this->max_reusable_write_objects_ = connection->socket_->max_reusable_write_objects_;
this->address_ = connection->socket_->address();
connection->close();
}
ExportedConnection::~ExportedConnection() {
if(handler_) {
delete handler_;
}
}
SharedRefPtr<Connection> ExportedConnection::import_connection(uv_loop_t *loop) {
Socket::Ptr socket_(new Socket(this->address_, this->max_reusable_write_objects_));
if (uv_tcp_init(loop, socket_->handle()) != 0) {
return Connection::Ptr();
}
socket_->is_defunct_ = this->is_defunct_;
uv_tcp_open(socket_->handle(), this->fd);
Connection::Ptr connection_(new Connection(socket_, this->host, this->protocol_version_, this->idle_timeout_secs_,
this->heartbeat_interval_secs_));
connection_->set_listener(this->listener_);
connection_->keyspace_ = keyspace_;
connection_->set_shard_id(this->shard_id_);
ConnectionHandler *c_handler = dynamic_cast<ConnectionHandler *>(this->handler_);
SslConnectionHandler *s_handler = dynamic_cast<SslConnectionHandler *>(this->handler_);
if (c_handler != nullptr) {
c_handler->set_connection(connection_.get());
} else if (s_handler != nullptr) {
s_handler->set_connection(connection_.get());
} else {
return Connection::Ptr();
}
socket_->set_handler(this->handler_);
this->handler_ = nullptr;
socket_->inc_ref();
return connection_;
}
}}} // namespace datastax::internal::core