This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
sockets.tcp.js
209 lines (185 loc) · 6.98 KB
/
sockets.tcp.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var Event = require('cordova-plugin-chrome-apps-common.events');
var platform = cordova.require('cordova/platform');
var exec = cordova.require('cordova/exec');
var callbackWithError = require('cordova-plugin-chrome-apps-common.errors').callbackWithError;
exports.create = function(properties, callback) {
if (typeof properties == 'function') {
callback = properties;
properties = {};
}
var win = callback && function(socketId) {
var createInfo = {
socketId: socketId
};
callback(createInfo);
};
exec(win, null, 'ChromeSocketsTcp', 'create', [properties]);
};
exports.update = function(socketId, properties, callback) {
exec(callback, null, 'ChromeSocketsTcp', 'update', [socketId, properties]);
};
exports.setPaused = function(socketId, paused, callback) {
exec(callback, null, 'ChromeSocketsTcp', 'setPaused', [socketId, paused]);
};
exports.setKeepAlive = function(socketId, enabled, delay, callback) {
if (typeof delay == 'function') {
callback = delay;
delay = 0;
}
if (platform.id == 'android') {
var win = callback && function() {
callback(0);
};
var fail = callback && function(error) {
callbackWithError(error.message, callback, error.resultCode);
};
exec(win, fail, 'ChromeSocketsTcp', 'setKeepAlive', [socketId, enabled, delay]);
} else {
console.warn('chrome.sockets.tcp.setKeepAlive not implemented yet, issue #391');
}
};
exports.setNoDelay = function(socketId, noDelay, callback) {
if (platform.id == 'android') {
var win = callback && function() {
callback(0);
};
var fail = callback && function(error) {
callbackWithError(error.message, callback, error.resultCode);
};
exec(win, fail, 'ChromeSocketsTcp', 'setNoDelay', [socketId, noDelay]);
} else {
console.warn('chrome.sockets.tcp.setNoDelay not implemented yet, issue #391');
}
};
exports.connect = function(socketId, peerAddress, peerPort, callback) {
var win = callback && function() {
callback(0);
};
var fail = callback && function(error) {
callbackWithError(error.message, callback, error.resultCode);
};
exec(win, fail, 'ChromeSocketsTcp', 'connect', [socketId, peerAddress, peerPort]);
};
exports.disconnect = function(socketId, callback) {
exec(callback, null, 'ChromeSocketsTcp', 'disconnect', [socketId]);
};
exports.secure = function(socketId, options, callback) {
if (typeof options == 'function') {
callback = options;
options = {};
}
var win = callback && function() {
callback(0);
};
var fail = callback && function(error) {
callbackWithError(error.message, callback, error.resultCode);
};
exec(win, fail, 'ChromeSocketsTcp', 'secure', [socketId, options]);
};
exports.send = function(socketId, data, callback) {
var type = Object.prototype.toString.call(data).slice(8, -1);
if (type != 'ArrayBuffer') {
throw new Error('chrome.sockets.tcp.send - data is not an Array Buffer! (Got: ' + type + ')');
}
var win = callback && function(bytesSent) {
var sendInfo = {
bytesSent: bytesSent,
resultCode: 0
};
callback(sendInfo);
};
var fail = callback && function(error) {
var sendInfo = {
bytesSent: 0,
resultCode: error.resultCode
};
callbackWithError(error.message, callback, sendInfo);
};
if (data.byteLength == 0) {
win(0);
} else {
exec(win, fail, 'ChromeSocketsTcp', 'send', [socketId, data]);
}
};
exports.close = function(socketId, callback) {
exec(callback, null, 'ChromeSocketsTcp', 'close', [socketId]);
};
exports.getInfo = function(socketId, callback) {
var win = callback && function(result) {
result.persistent = !!result.persistent;
result.paused = !!result.paused;
callback(result);
};
exec(win, null, 'ChromeSocketsTcp', 'getInfo', [socketId]);
};
exports.getSockets = function(callback) {
var win = callback && function(results) {
for (var result in results) {
result.persistent = !!result.persistent;
result.paused = !!result.paused;
}
callback(results);
};
exec(win, null, 'ChromeSocketsTcp', 'getSockets', []);
};
exports.pipeToFile = function(socketId, options, callback) {
exec(callback, null, 'ChromeSocketsTcp', 'pipeToFile', [socketId, options]);
};
exports.onReceive = new Event('onReceive');
exports.onReceiveError = new Event('onReceiveError');
function registerReceiveEvents() {
var win = function(info, data) {
if (data) { // Binary data has to be a top level argument.
info.data = data;
}
exports.onReceive.fire(info);
if (data) { // Only exec readyToRead when not redirect to file
// readyToRead signals the plugin to read the next tcp packet. exec
// it after fire() will allow all API calls in the onReceive
// listener exec before next read, such as, pause the socket.
exec(null, null, 'ChromeSocketsTcp', 'readyToRead', []);
}
};
// TODO: speical callback for android, DELETE when multipart result for
// android is avaliable
if (platform.id == 'android') {
win = (function() {
var recvInfo;
var call = 0;
return function(info) {
if (call === 0) {
recvInfo = info;
if (!recvInfo.uri) {
call++;
// uri implies only one callback becasue redirect to
// file is enabled, and binary data is not included in
// the receiveInfo.
return;
}
} else {
recvInfo.data = info;
call = 0;
}
exports.onReceive.fire(recvInfo);
if (recvInfo.data) { // Only exec readyToRead when not redirect to file
// readyToRead signals the plugin to read the next tcp
// packet. exec it after fire() will allow all API calls in
// the onReceive listener exec before next read, such as,
// pause the socket.
exec(null, null, 'ChromeSocketsTcp', 'readyToRead', [recvInfo.socketId]);
}
};
})();
}
var fail = function(info) {
var error = function() {
exports.onReceiveError.fire(info);
};
callbackWithError(info.message, error);
};
exec(win, fail, 'ChromeSocketsTcp', 'registerReceiveEvents', []);
}
require('cordova-plugin-chrome-apps-common.helpers').runAtStartUp(registerReceiveEvents);