-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cc
97 lines (86 loc) · 3.27 KB
/
functions.cc
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
#include "functions.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
Nan::Persistent<v8::Function> SynchronousSocket::constructor;
NAN_MODULE_INIT(SynchronousSocket::Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("SynchronousSocket").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "connect", Connect);
Nan::SetPrototypeMethod(tpl, "disconnect", Disconnect);
Nan::SetPrototypeMethod(tpl, "read", Read);
Nan::SetPrototypeMethod(tpl, "write", Write);
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("SynchronousSocket").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
SynchronousSocket::SynchronousSocket(std::string socketPath) : socketPath_(socketPath) { }
SynchronousSocket::~SynchronousSocket() { }
NAN_METHOD(SynchronousSocket::New) {
std::string socketPath = *Nan::Utf8String(info[0]);
SynchronousSocket *obj = new SynchronousSocket(socketPath);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(SynchronousSocket::Connect) {
SynchronousSocket* obj = Nan::ObjectWrap::Unwrap<SynchronousSocket>(info.This());
obj->socketfd_ = socket(AF_UNIX, SOCK_STREAM, 0);
if (obj->socketfd_ == -1) {
Nan::ThrowError("Unable to open socket file descriptor.");
}
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, obj->socketPath_.c_str(), sizeof(addr.sun_path) - 1);
if (connect(obj->socketfd_, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
close(obj->socketfd_);
Nan::ThrowError("Unable to connect to socket.");
}
}
NAN_METHOD(SynchronousSocket::Disconnect) {
SynchronousSocket* obj = Nan::ObjectWrap::Unwrap<SynchronousSocket>(info.This());
close(obj->socketfd_);
}
NAN_METHOD(SynchronousSocket::Read) {
SynchronousSocket* obj = Nan::ObjectWrap::Unwrap<SynchronousSocket>(info.This());
char buffer;
ssize_t bytesRead;
size_t bufferSize = 0;
char* result = NULL;
while ((bytesRead = read(obj->socketfd_, &buffer, 1)) > 0) {
if (buffer == 0x04) { // Ctrl+D (end of transmission)
break;
}
char* temp = (char*)realloc(result, bufferSize + 1);
if (temp == NULL) {
free(result);
return;
}
result = temp;
result[bufferSize] = buffer;
bufferSize++;
}
if (result != NULL) {
result = (char*)realloc(result, bufferSize + 1);
if (result != NULL) {
result[bufferSize] = '\0';
}
}
info.GetReturnValue().Set(Nan::New<v8::String>(result).ToLocalChecked());
}
NAN_METHOD(SynchronousSocket::Write) {
SynchronousSocket* obj = Nan::ObjectWrap::Unwrap<SynchronousSocket>(info.This());
if (!info[0]->IsString()) {
return Nan::ThrowTypeError("Data must be a string.");
}
Nan::Utf8String socketDataArg(info[0]);
const char* data = *socketDataArg;
ssize_t size = strlen(data);
if (write(obj->socketfd_, data, size) != size) {
close(obj->socketfd_);
Nan::ThrowError("Unable to write to socket.");
}
}