1
1
#include " ./socket.h"
2
2
3
+ #include < array>
3
4
#include < cmath>
4
5
#include < cstdint>
5
6
#include < limits>
6
7
#include < unordered_set>
7
- #include < array>
8
8
9
9
#include " ./context.h"
10
10
#include " ./incoming_msg.h"
@@ -105,13 +105,20 @@ Socket::Socket(const Napi::CallbackInfo& info)
105
105
uv_os_sock_t file_descriptor = 0 ;
106
106
std::function<void ()> const finalize = nullptr ;
107
107
108
+ const auto error = [this ]() {
109
+ [[maybe_unused]] auto err = zmq_close (socket);
110
+ assert (err == 0 );
111
+
112
+ socket = nullptr ;
113
+ };
114
+
108
115
#ifdef ZMQ_THREAD_SAFE
109
116
{
110
117
int value = 0 ;
111
118
size_t length = sizeof (value);
112
119
if (zmq_getsockopt (socket, ZMQ_THREAD_SAFE, &value, &length) < 0 ) {
113
120
ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
114
- goto error;
121
+ error () ;
115
122
}
116
123
117
124
thread_safe = (value != 0 );
@@ -126,7 +133,7 @@ Socket::Socket(const Napi::CallbackInfo& info)
126
133
auto poll = zmq_poller_new ();
127
134
if (poll == nullptr ) {
128
135
ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
129
- goto error;
136
+ error () ;
130
137
}
131
138
132
139
/* Callback to free the underlying poller. Move the poller to transfer
@@ -139,31 +146,31 @@ Socket::Socket(const Napi::CallbackInfo& info)
139
146
if (zmq_poller_add (poll, socket, nullptr , ZMQ_POLLIN | ZMQ_POLLOUT) < 0 ) {
140
147
ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
141
148
finalize ();
142
- goto error;
149
+ error () ;
143
150
}
144
151
145
152
if (zmq_poller_fd (poll, &fd) < 0 ) {
146
153
ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
147
154
finalize ();
148
- goto error;
155
+ error () ;
149
156
}
150
157
#else
151
158
/* A thread safe socket was requested, but there is no support for
152
159
retrieving a poller FD, so we cannot construct them. */
153
160
ErrnoException (Env (), EINVAL).ThrowAsJavaScriptException ();
154
- goto error;
161
+ error () ;
155
162
#endif
156
163
} else {
157
164
size_t length = sizeof (file_descriptor);
158
165
if (zmq_getsockopt (socket, ZMQ_FD, &file_descriptor, &length) < 0 ) {
159
166
ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
160
- goto error;
167
+ error () ;
161
168
}
162
169
}
163
170
164
171
if (poller.Initialize (Env (), file_descriptor, finalize) < 0 ) {
165
172
ErrnoException (Env (), errno).ThrowAsJavaScriptException ();
166
- goto error;
173
+ error () ;
167
174
}
168
175
169
176
/* Initialization was successful, register the socket for cleanup. */
@@ -177,14 +184,6 @@ Socket::Socket(const Napi::CallbackInfo& info)
177
184
if (info[1 ].IsObject ()) {
178
185
Assign (info.This ().As <Napi::Object>(), info[1 ].As <Napi::Object>());
179
186
}
180
-
181
- return ;
182
-
183
- error:
184
- [[maybe_unused]] auto err = zmq_close (socket);
185
- assert (err == 0 );
186
-
187
- socket = nullptr ;
188
187
}
189
188
190
189
Socket::~Socket () {
0 commit comments