Browse Source

[net2] delete unix sockfile on server start and shutdown

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
8d0f756158
  1. 55
      lib/net.js
  2. 1
      src/node_net2.cc
  3. 36
      test-net-server.js

55
lib/net.js

@ -25,6 +25,7 @@ var getsockname = process.getsockname;
var getaddrinfo = process.getaddrinfo;
var needsLookup = process.needsLookup;
var EINPROGRESS = process.EINPROGRESS;
var ENOENT = process.ENOENT;
var END_OF_FILE = 42;
@ -318,7 +319,7 @@ Socket.prototype.connect = function () {
};
}
if (typeof(arguments[0]) == 'string' && arguments.length == 1) {
if (typeof(arguments[0]) == 'string') {
self.fd = socket('unix');
self.type = 'unix';
// TODO check if sockfile exists?
@ -457,12 +458,32 @@ Server.prototype.listen = function () {
// the first argument specifies a path
self.fd = socket('unix');
self.type = 'unix';
// TODO unlink sockfile if exists?
// if (lstat(SOCKFILE, &tstat) == 0) {
// assert(S_ISSOCK(tstat.st_mode));
// unlink(SOCKFILE);
// }
bind(self.fd, arguments[0]);
var path = arguments[0];
self.path = path;
// unlink sockfile if it exists
process.fs.stat(path, function (r) {
if (r instanceof Error) {
if (r.errno == ENOENT) {
bind(self.fd, path);
doListen();
} else {
throw r;
}
} else {
if (!r.isFile()) {
throw new Error("Non-file exists at " + path);
} else {
process.fs.unlink(path, function (err) {
if (err) {
throw err;
} else {
bind(self.fd, path);
doListen();
}
});
}
}
});
} else if (arguments.length == 0) {
self.fd = socket('tcp');
self.type = 'tcp';
@ -488,9 +509,19 @@ Server.prototype.address = function () {
Server.prototype.close = function () {
if (!this.fd) throw new Error('Not running');
this.watcher.stop();
close(this.fd);
this.fd = null;
this.emit("close");
var self = this;
if (!self.fd) throw new Error('Not running');
self.watcher.stop();
close(self.fd);
self.fd = null;
if (self.type === "unix") {
process.fs.unlink(self.path, function () {
self.emit("close");
});
} else {
self.emit("close");
}
};

1
src/node_net2.cc

@ -761,6 +761,7 @@ void InitNet2(Handle<Object> target) {
NODE_SET_METHOD(target, "getaddrinfo", GetAddrInfo);
NODE_SET_METHOD(target, "needsLookup", NeedsLookup);
target->Set(String::NewSymbol("ENOENT"), Integer::New(ENOENT));
target->Set(String::NewSymbol("EINPROGRESS"), Integer::New(EINPROGRESS));
target->Set(String::NewSymbol("EINTR"), Integer::New(EINTR));
target->Set(String::NewSymbol("EACCES"), Integer::New(EACCES));

36
test-net-server.js

@ -17,6 +17,10 @@ var server = new net.Server(function (socket) {
socket.send("pong ascii\r\n", "ascii");
socket.send(b);
socket.send("pong utf8\r\n", "utf8");
if (/^quit/.test(b)) {
socket.close();
server.close();
}
});
socket.addListener("eof", function () {
@ -28,25 +32,27 @@ var server = new net.Server(function (socket) {
sys.puts("server-side socket drain");
});
});
server.listen(8000);
server.listen("/tmp/node.sock");
sys.puts("server fd: " + server.fd);
server.addListener("listening", function () {
var c = net.createConnection("/tmp/node.sock");
c.addListener('connect', function () {
sys.puts("!!!client connected");
c.send("hello\n");
});
var c = net.createConnection(8000, "localhost");
c.addListener('connect', function () {
sys.puts("!!!client connected");
c.send("hello\n");
});
c.addListener('drain', function () {
sys.puts("!!!client drain");
});
c.addListener('drain', function () {
sys.puts("!!!client drain");
});
c.addListener('data', function (d) {
sys.puts("!!!client got: " + JSON.stringify(d.toString()));
c.close();
});
c.addListener('data', function (d) {
sys.puts("!!!client got: " + JSON.stringify(d.toString()));
c.close();
c.addListener('eof', function (d) {
sys.puts("!!!client eof");
});
});
c.addListener('dataEnd', function (d) {
sys.puts("!!!client eof");
});

Loading…
Cancel
Save