Browse Source

Add node.fs.sendfile()

v0.7.4-release
Connor Dunn 15 years ago
committed by Ryan Dahl
parent
commit
2dbd0d3806
  1. 17
      src/file.cc
  2. 6
      src/file.h
  3. 27
      test/mjsunit/test-fs-sendfile.js

17
src/file.cc

@ -134,6 +134,7 @@ int EIOPromise::After(eio_req *req) {
break;
case EIO_OPEN:
case EIO_SENDFILE:
argc = 1;
argv[0] = Integer::New(req->result);
break;
@ -269,6 +270,21 @@ static Handle<Value> MKDir(const Arguments& args) {
return scope.Close(EIOPromise::MKDir(*path, mode));
}
static Handle<Value> SendFile(const Arguments& args) {
HandleScope scope;
if (args.Length() < 4 || !args[0]->IsInt32() || !args[1]->IsInt32() || !args[3]->IsNumber()) {
return ThrowException(BAD_ARGUMENTS);
}
int out_fd = args[0]->Int32Value();
int in_fd = args[1]->Int32Value();
off_t in_offset = args[2]->IsNumber() ? args[2]->IntegerValue() : -1;
size_t length = args[3]->IntegerValue();
return scope.Close(EIOPromise::SendFile(out_fd, in_fd, in_offset, length));
}
static Handle<Value> ReadDir(const Arguments& args) {
HandleScope scope;
@ -365,6 +381,7 @@ void File::Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "rename", Rename);
NODE_SET_METHOD(target, "rmdir", RMDir);
NODE_SET_METHOD(target, "mkdir", MKDir);
NODE_SET_METHOD(target, "sendfile", SendFile);
NODE_SET_METHOD(target, "readdir", ReadDir);
NODE_SET_METHOD(target, "stat", Stat);
NODE_SET_METHOD(target, "unlink", Unlink);

6
src/file.h

@ -102,6 +102,12 @@ class EIOPromise : public Promise {
return p->handle_;
}
static v8::Handle<v8::Object> SendFile(int out_fd, int in_fd, off_t in_offset, size_t length) {
EIOPromise *p = Create();
p->req_ = eio_sendfile(out_fd, in_fd, in_offset, length, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
protected:
void Attach();

27
test/mjsunit/test-fs-sendfile.js

@ -0,0 +1,27 @@
node.mixin(require("common.js"));
tcp = require("/tcp.js");
sys = require("/sys.js");
PORT = 23123;
var x = node.path.join(fixturesDir, "x.txt");
var expected = "xyz";
var server = tcp.createServer(function (socket) {
socket.addListener("receive", function (data) {
found = data;
client.close();
socket.close();
server.close();
assertEquals(expected, found);
});
});
server.listen(PORT);
var client = tcp.createConnection(PORT);
client.addListener("connect", function () {
node.fs.open(x,node.O_RDONLY, 0666).addCallback(function (fd) {
node.fs.sendfile(client.fd, fd, 0, expected.length).addCallback(function (size) {
assertEquals(expected.length, size);
});
});
});
Loading…
Cancel
Save