[net2] delete unix sockfile on server start and shutdown

This commit is contained in:
Ryan Dahl 2009-12-30 11:32:07 -08:00
parent a8ede8dd9e
commit 8d0f756158
3 changed files with 67 additions and 29 deletions

View File

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

View File

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

View File

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