Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created August 24, 2012 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/61b7a521d96f20e0e7d6 to your computer and use it in GitHub Desktop.
Save bnoordhuis/61b7a521d96f20e0e7d6 to your computer and use it in GitHub Desktop.
diff --git a/src/unix_dgram.cc b/src/unix_dgram.cc
index 3201b0a..1450cf1 100644
--- a/src/unix_dgram.cc
+++ b/src/unix_dgram.cc
@@ -97,8 +97,8 @@ void OnRecv(uv_poll_t* handle, int status, int events) {
Buffer* buf;
msghdr msg;
iovec iov;
- int size;
- int r;
+ ssize_t r;
+ char scratch[65536];
sc = container_of(handle, SocketContext, handle_);
@@ -109,16 +109,8 @@ void OnRecv(uv_poll_t* handle, int status, int events) {
assert(0 == status);
assert(0 == (events & ~UV_READABLE));
- if ((r = ioctl(sc->fd_, FIONREAD, &size)) == -1) {
- SetErrno(errno);
- goto err;
- }
-
- buf = Buffer::New(size);
- argv[1] = buf->handle_;
-
- iov.iov_base = Buffer::Data(buf);
- iov.iov_len = size;
+ iov.iov_base = scratch;
+ iov.iov_len = sizeof scratch;
memset(&msg, 0, sizeof msg);
msg.msg_iovlen = 1;
@@ -130,8 +122,13 @@ void OnRecv(uv_poll_t* handle, int status, int events) {
r = recvmsg(sc->fd_, &msg, 0);
while (r == -1 && errno == EINTR);
- if (r == -1)
+ if (r == -1) {
SetErrno(errno);
+ goto err;
+ }
+
+ buf = Buffer::New(scratch, r);
+ argv[1] = buf->handle_;
err:
argv[0] = Integer::New(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment