Skip to content

Commit 0d78763

Browse files
committed
Merge pull request brianc#149 from booo/c++
additional changes for the native binding
2 parents 4d482e4 + ccc3f81 commit 0d78763

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

lib/native/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ p.connect = function(cb) {
3030
if(err) {
3131
return cb ? cb(err) : self.emit('error', err);
3232
}
33-
nativeConnect.call(self, conString);
3433
if(cb) {
3534
var errCallback;
3635
var connectCallback = function() {
@@ -46,6 +45,7 @@ p.connect = function(cb) {
4645
self.once('connect', connectCallback);
4746
self.once('error', errCallback);
4847
}
48+
nativeConnect.call(self, conString);
4949
})
5050
}
5151

src/binding.cc

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,17 @@ class Connection : public ObjectWrap {
234234

235235
uv_poll_t read_watcher_;
236236
uv_poll_t write_watcher_;
237-
238237
PGconn *connection_;
239238
bool connecting_;
239+
bool ioInitialized_;
240240
Connection () : ObjectWrap ()
241241
{
242242
connection_ = NULL;
243243
connecting_ = false;
244+
ioInitialized_ = false;
244245

245246
TRACE("Initializing ev watchers");
246-
//ev_init(&read_watcher_, io_event);
247247
read_watcher_.data = this;
248-
//ev_init(&write_watcher_, io_event);
249248
write_watcher_.data = this;
250249
}
251250

@@ -308,7 +307,7 @@ class Connection : public ObjectWrap {
308307
{
309308
if(PQflush(connection_) == 1) {
310309
TRACE("Flushing");
311-
//ev_io_start(EV_DEFAULT_ &write_watcher_);
310+
uv_poll_start(&write_watcher_, UV_WRITABLE, io_event);
312311
}
313312
}
314313

@@ -331,15 +330,14 @@ class Connection : public ObjectWrap {
331330
LOG("Connection couldn't be created");
332331
}
333332

334-
if (PQsetnonblocking(connection_, 1) == -1) {
335-
LOG("Unable to set connection to non-blocking");
336-
return false;
337-
}
338-
339333
ConnStatusType status = PQstatus(connection_);
340334

341335
if(CONNECTION_BAD == status) {
342-
LOG("Bad connection status");
336+
return false;
337+
}
338+
339+
if (PQsetnonblocking(connection_, 1) == -1) {
340+
LOG("Unable to set connection to non-blocking");
343341
return false;
344342
}
345343

@@ -353,13 +351,11 @@ class Connection : public ObjectWrap {
353351

354352
PQsetNoticeProcessor(connection_, NoticeReceiver, this);
355353

354+
TRACE("Setting watchers to socket");
356355
uv_poll_init(uv_default_loop(), &read_watcher_, fd);
357356
uv_poll_init(uv_default_loop(), &write_watcher_, fd);
358357

359-
TRACE("Setting watchers to socket");
360-
//uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb)
361-
//ev_io_set(&read_watcher_, fd, EV_READ);
362-
//ev_io_set(&write_watcher_, fd, EV_WRITE);
358+
ioInitialized_ = true;
363359

364360
connecting_ = true;
365361
StartWrite();
@@ -399,7 +395,7 @@ class Connection : public ObjectWrap {
399395
TRACE("revents & EV_READ");
400396
if(PQconsumeInput(connection_) == 0) {
401397
End();
402-
EmitLastError();
398+
EmitLastError();
403399
LOG("Something happened, consume input is 0");
404400
return;
405401
}
@@ -623,28 +619,28 @@ class Connection : public ObjectWrap {
623619
void StopWrite()
624620
{
625621
TRACE("Stoping write watcher");
626-
//ev_io_stop(EV_DEFAULT_ &write_watcher_);
627-
uv_poll_stop(&write_watcher_);
622+
if(ioInitialized_) {
623+
uv_poll_stop(&write_watcher_);
624+
}
628625
}
629626

630627
void StartWrite()
631628
{
632629
TRACE("Starting write watcher");
633630
uv_poll_start(&write_watcher_, UV_WRITABLE, io_event);
634-
//ev_io_start(EV_DEFAULT_ &write_watcher_);
635631
}
636632

637633
void StopRead()
638634
{
639635
TRACE("Stoping read watcher");
640-
//ev_io_stop(EV_DEFAULT_ &read_watcher_);
641-
uv_poll_stop(&read_watcher_);
636+
if(ioInitialized_) {
637+
uv_poll_stop(&read_watcher_);
638+
}
642639
}
643640

644641
void StartRead()
645642
{
646643
TRACE("Starting read watcher");
647-
//ev_io_start(EV_DEFAULT_ &read_watcher_);
648644
uv_poll_start(&read_watcher_, UV_READABLE, io_event);
649645
}
650646
//Converts a v8 array to an array of cstrings

test/integration/client/error-handling-tests.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ test('error handling', function(){
8080
});
8181

8282
test('non-query error', function() {
83-
return false;
8483

8584
var client = new Client({
8685
user:'asldkfjsadlfkj'
@@ -90,7 +89,6 @@ test('error handling', function(){
9089
});
9190

9291
test('non-query error with callback', function() {
93-
return false;
9492
var client = new Client({
9593
user:'asldkfjsadlfkj'
9694
});
@@ -117,7 +115,6 @@ test('non-error calls supplied callback', function() {
117115
});
118116

119117
test('when connecting to invalid host', function() {
120-
return false;
121118
var client = new Client({
122119
user: 'brian',
123120
password: '1234',
@@ -128,7 +125,6 @@ test('when connecting to invalid host', function() {
128125
});
129126

130127
test('when connecting to invalid host with callback', function() {
131-
return false;
132128
var client = new Client({
133129
user: 'brian',
134130
password: '1234',

0 commit comments

Comments
 (0)