Skip to content

Commit

Permalink
Add new sessionTransportDisconnected event to TransportTCP
Browse files Browse the repository at this point in the history
Helps us detect errors on socket ends and notify them to SIP.js clients properly

Also remade callIndex assembly to ease parsing and improved logging
  • Loading branch information
prlanzarin committed Jun 10, 2019
1 parent a0f6c95 commit 99eba33
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/TransportTCP.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ Transport.prototype = {
callTag = parsedMsg.from_tag;
}
if (callId && callTag) {
let socket = this.sockets[callId + callTag];
const callIndex = `${callId}|${callTag}`;
let socket = this.sockets[callIndex]
if (socket) {
socket.write(message, (e) => {
if (e) {
this.logger.warn('unable to send TCP message with error', e);
this.logger.warn(`unable to send TCP message with error ${e}`);
return false;
}
if (this.ua.configuration.traceSip === true) {
Expand Down Expand Up @@ -169,14 +170,16 @@ Transport.prototype = {
this.server = net.createServer((socket) => {
socket.pendingSegments = [];
socket.setKeepAlive(true);
socket.on('end', function() {
transport.onClose(socket);
socket.on('end', (e) => {
this.logger.log(`TCP socket ended for connection ${socket.callIndex || 'Unknown'}`);
transport.onClientSocketClose(socket, e);
});

const onSocketError = (e) => {
this.logger.log("TCP socket returned error " + e + " and will close");
if (socket.callIndex) {
//this.logger.log("TCP socket ended for connection" + socket.callIndex);
this.logger.log(`TCP socket ended for connection ${socket.callIndex}`);
transport.onClientSocketClose(socket, e);
if (this.sockets[socket.callIndex]) {
delete this.sockets[socket.callIndex];
}
Expand Down Expand Up @@ -236,13 +239,29 @@ Transport.prototype = {
*/
onClose: function(socket) {
this.stopSendingKeepAlives();
this.ua.onTransportClosed(this);
this.ua.emit('disconnected', {
transport: this,
code: "",
reason: ""
});
},

/**
* @event
* @param {net.Socket} socket
* @param {event} e
*/
onClientSocketClose: function(socket, e = {}) {
const callId = socket.callIndex? socket.callIndex.split('|')[0] : ''
this.ua.emit('sessionTransportDisconnected', {
transport: this,
callId,
code: e.code || '',
reason: e.message || ''
});
},

/**
* @event
* @param {event} e
Expand Down Expand Up @@ -333,8 +352,9 @@ Transport.prototype = {
switch (message.method) {
case SIP.C.INVITE:
if (this.sockets[message.call_id + message.from_tag] == null) {
socket.callIndex = message.call_id + message.from_tag;
this.sockets[message.call_id + message.from_tag] = socket;
const callIndex = `${message.call_id}|${message.from_tag}`;
socket.callIndex = callIndex;
this.sockets[callIndex] = socket;
}
break;
default:
Expand Down

0 comments on commit 99eba33

Please sign in to comment.