UNPKG

3.44 kBJavaScriptView Raw
1// Copyright 2011 Timothy J Fontaine <tjfontaine@gmail.com>
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE
20
21'use strict';
22
23var dgram = require('dgram'),
24 EventEmitter = require('events').EventEmitter,
25 net = require('net'),
26 util = require('util'),
27 UDPSocket = require('./utils').UDPSocket,
28 TCPSocket = require('./utils').TCPSocket,
29 Packet = require('./packet');
30
31var Server = function(opts) {
32 var self = this;
33
34 this._socket.on('listening', function() {
35 self.emit('listening');
36 });
37
38 this._socket.on('close', function() {
39 self.emit('close');
40 });
41
42 this._socket.on('error', function(err) {
43 self.emit('socketError', err, self._socket);
44 });
45};
46util.inherits(Server, EventEmitter);
47
48Server.prototype.close = function() {
49 this._socket.close();
50};
51
52Server.prototype.address = function() {
53 return this._socket.address();
54};
55
56Server.prototype.handleMessage = function(msg, remote, address) {
57 var request, response = new Packet(remote);
58
59 try {
60 request = Packet.parse(msg, remote);
61
62 request.address = address;
63
64 response.header.id = request.header.id;
65 response.header.qr = 1;
66 response.question = request.question;
67
68 this.emit('request', request, response);
69 } catch (e) {
70 this.emit('error', e, msg, response);
71 }
72};
73
74var UDPServer = function(opts) {
75 var self = this;
76
77 this._socket = dgram.createSocket(opts.dgram_type || 'udp4');
78
79 this._socket.on('message', function(msg, remote) {
80 self.handleMessage(msg, new UDPSocket(self._socket, remote), remote);
81 });
82
83 Server.call(this, opts);
84};
85util.inherits(UDPServer, Server);
86
87UDPServer.prototype.serve = function(port, address) {
88 this._socket.bind(port, address);
89};
90
91var TCPServer = function(opts) {
92 var self = this;
93
94 this._socket = net.createServer(function(client) {
95 var tcp = new TCPSocket(client);
96 var address = client.address();
97 tcp.on('message', function(msg, remote) {
98 self.handleMessage(msg, tcp, address);
99 });
100 tcp.catchMessages();
101 });
102
103 Server.call(this, opts);
104};
105util.inherits(TCPServer, Server);
106
107TCPServer.prototype.serve = function(port, address) {
108 this._socket.listen(port, address);
109};
110
111exports.createServer = function(opts) {
112 return new UDPServer(opts || {});
113};
114
115exports.createUDPServer = function(opts) {
116 return exports.createServer(opts);
117};
118
119exports.createTCPServer = function(opts) {
120 return new TCPServer(opts || {});
121};