1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 /** @suppress {duplicate} */
8 var remoting
= remoting
|| {};
12 /** @type {Object<number, remoting.TcpSocket>} */
14 var receiveListenersAdded
= false;
16 function addReceiveListeners() {
17 if (receiveListenersAdded
) {
21 receiveListenersAdded
= true;
23 chrome
.sockets
.tcp
.onReceive
.addListener(function(
24 /** chrome.sockets.tcp.ReceiveEventData */ info
) {
25 var socket
= sockets
[info
.socketId
];
26 if (socket
=== undefined) {
27 console
.warn("Received data for unknown socket " + info
.socketId
);
30 if (socket
.receiveCallback_
=== null) {
31 console
.warn("Received data when socket was paused.");
34 socket
.receiveCallback_(info
.data
);
37 chrome
.sockets
.tcp
.onReceiveError
.addListener(function(
38 /** chrome.sockets.tcp.ReceiveErrorEventData */ info
) {
39 var socket
= sockets
[info
.socketId
];
40 if (socket
=== undefined) {
41 console
.warn("Received error for unknown socket " + info
.socketId
);
44 if (socket
.receiveErrorCallback_
=== null) {
45 console
.warn("Recv() failed when socket was paused: " + info
.resultCode
);
48 socket
.receiveErrorCallback_(info
.resultCode
);
53 * Wrapper for chrome.sockets.tcp API.
56 * @implements {base.Disposable}
58 remoting
.TcpSocket = function() {
60 this.destroyed_
= false;
63 /** @private {?function(ArrayBuffer):void} */
64 this.receiveCallback_
= null;
65 /** @private {?function(number):void} */
66 this.receiveErrorCallback_
= null;
68 addReceiveListeners();
72 * Connects the socket to the specified host and port.
74 * @returns {Promise} Promise that's resolved when the socket is connected.
76 remoting
.TcpSocket
.prototype.connect = function(/** string */ host
,
80 return new Promise(function(resolve
, reject
) {
81 chrome
.sockets
.tcp
.create({}, /** @type {function(Object)} */ (onCreated
));
84 function onCreated(/** chrome.socket.CreateInfo */ createInfo
) {
85 // Check if the socket was destroyed.
86 if (that
.destroyed_
) {
87 chrome
.sockets
.tcp
.close(createInfo
.socketId
);
91 that
.socketId_
= createInfo
.socketId
;
92 sockets
[that
.socketId_
] = that
;
94 // Pause the socket so that we start receiving only after startReceiving()
96 chrome
.sockets
.tcp
.setPaused(that
.socketId_
, true);
98 chrome
.sockets
.tcp
.connect(that
.socketId_
, host
, port
, onConnected
);
101 function onConnected(/** number */ result
) {
102 if (that
.destroyed_
) {
115 remoting
.TcpSocket
.prototype.dispose = function() {
116 if (this.socketId_
!= -1) {
117 chrome
.sockets
.tcp
.close(this.socketId_
);
118 delete sockets
[this.socketId_
];
121 this.destroyed_
= true;
122 this.receiveCallback_
= null;
126 * Starts receiving data on the socket. Calls receiveCallback when new data is
127 * received or receiveErrorCallback when recv() returns an error.
129 remoting
.TcpSocket
.prototype.startReceiving = function(
130 /** ?function(ArrayBuffer):void */ receiveCallback
,
131 /** ?function(number):void */ receiveErrorCallback
) {
132 console
.assert(this.receiveCallback_
== null,
133 'Duplicate startReceiving() invocation.');
134 this.receiveCallback_
= receiveCallback
;
135 this.receiveErrorCallback_
= receiveErrorCallback
;
136 chrome
.sockets
.tcp
.setPaused(this.socketId_
, false);
144 remoting
.TcpSocket
.prototype.send = function(/** !ArrayBuffer */ data
) {
147 return new Promise(function(resolve
, reject
) {
148 chrome
.sockets
.tcp
.send(that
.socketId_
, data
,
149 function(/** chrome.socket.SendInfo */ sendInfo
) {
150 if (sendInfo
.resultCode
< 0) {
151 reject(sendInfo
.resultCode
);
153 resolve(sendInfo
.bytesSent
);
160 * Starts TLS on the socket. Once TLS is negotiated the caller will need to call
161 * startReceiving() to start receiving data, even if startReceiving() was called
166 remoting
.TcpSocket
.prototype.startTls = function() {
169 return new Promise(function(resolve
, reject
) {
170 function doStartTls() {
171 chrome
.sockets
.tcp
.secure(that
.socketId_
, {}, function(result
) {
180 if (!that
.receiveCallback_
) {
181 // Socket is already paused.
184 // Socket must be paused before staring TLS. This won't work correctly
185 // until crbug.com/403076 is fixed. Log a warning and try anyway.
187 "remoting.TcpSocket.secure() was called after some data was " +
188 "received on the socket. This won't work properly until " +
189 "crbug.com/403076 is fixed.");
190 chrome
.sockets
.tcp
.setPaused(that
.socketId_
, true, function() {
191 if (that
.destroyed_
) {
194 that
.receiveCallback_
= null;
195 that
.receiveErrorCallback_
= null;