Skip to content

Commit

Permalink
Merge branch 'documentation'
Browse files Browse the repository at this point in the history
  • Loading branch information
shovon committed Jul 2, 2019
2 parents 3a9ba69 + 10f0fa4 commit 44022d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Connection.prototype.send = function (data) {
this._sender.send(data);
};

/**
* This is the function called by `Server` and `Client` to process packets as
* they arrive.
* @param {Packet} packet Is a single packet received from an endpoint.
*/
Connection.prototype.receive = function (packet) {
if (packet.getIsAcknowledgement()) {
this._sender.verifyAcknowledgement(packet.getSequenceNumber());
Expand Down
24 changes: 23 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,49 @@ var util = require('util');
var PacketSender = require('./PacketSender');

module.exports = Server;

/**
* The constructor function for a "server" in our RUDP construct.
* @param {dgram.Socket} socket Node.js' dgram.Socket.
*/
function Server(socket) {
// A list of all connections.
this._connections = {};

var self = this;

socket.on('message', function (message, rinfo) {
var addressKey = rinfo.address + rinfo.port;
var connection;

// Get connection.
if (!self._connections[addressKey]) {
// Record a new connection to the list of connections.

connection = new Connection(new PacketSender(socket, rinfo.address, rinfo.port));
self._connections[addressKey] = connection;
self.emit('connection', connection);
} else {
// Just get the existing connection.

connection = self._connections[addressKey];
}

// Parse the message.
var packet = new Packet(message);
if (packet.getIsFinish()){

if (packet.getIsFinish()) {
// The client requested that the connection be closed.

delete self._connections[addressKey];
} else {
// Capture the packet, and place it into the window of packets.

setImmediate(function () {
connection.receive(packet);
});
}

});
};

Expand Down

0 comments on commit 44022d6

Please sign in to comment.