~mathiaz/+junk/ceph-new-pkg-review

« back to all changes in this revision

Viewing changes to src/msg/tcp.cc

  • Committer: Mathias Gug
  • Date: 2010-07-29 03:10:42 UTC
  • Revision ID: mathias.gug@canonical.com-20100729031042-n9n8kky962qb4onb
Import ceph_0.21-0ubuntu1 from https://launchpad.net/~clint-fewbar/+archive/ceph/+packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
 
2
// vim: ts=8 sw=2 smarttab
 
3
 
 
4
#include <poll.h>
 
5
#include "tcp.h"
 
6
 
 
7
/******************
 
8
 * tcp crap
 
9
 */
 
10
 
 
11
int tcp_read(int sd, char *buf, int len) {
 
12
  if (sd < 0)
 
13
    return -1;
 
14
  struct pollfd pfd;
 
15
  pfd.fd = sd;
 
16
  pfd.events = POLLIN | POLLHUP | POLLRDHUP | POLLNVAL | POLLERR;
 
17
  while (len > 0) {
 
18
    if (poll(&pfd, 1, -1) < 0)
 
19
      return -1;
 
20
 
 
21
    if (!(pfd.revents & POLLIN))
 
22
      return -1;
 
23
 
 
24
    /*
 
25
     * although we turn on the MSG_DONTWAIT flag, we don't expect
 
26
     * receivng an EAGAIN, as we polled on the socket, so there
 
27
     * should be data waiting for us.
 
28
     */
 
29
    int got = ::recv( sd, buf, len, MSG_DONTWAIT );
 
30
    if (got <= 0) {
 
31
      //char buf[100];
 
32
      //generic_dout(0) << "tcp_read socket " << sd << " returned " << got
 
33
      //<< " errno " << errno << " " << strerror_r(errno, buf, sizeof(buf)) << dendl;
 
34
      return -1;
 
35
    }
 
36
    len -= got;
 
37
    buf += got;
 
38
    //generic_dout(DBL) << "tcp_read got " << got << ", " << len << " left" << dendl;
 
39
  }
 
40
  return len;
 
41
}
 
42
 
 
43
int tcp_write(int sd, const char *buf, int len) {
 
44
  if (sd < 0)
 
45
    return -1;
 
46
  struct pollfd pfd;
 
47
  pfd.fd = sd;
 
48
  pfd.events = POLLOUT | POLLHUP | POLLRDHUP | POLLNVAL | POLLERR;
 
49
 
 
50
  if (poll(&pfd, 1, -1) < 0)
 
51
    return -1;
 
52
 
 
53
  if (!(pfd.revents & POLLOUT))
 
54
    return -1;
 
55
 
 
56
  //generic_dout(DBL) << "tcp_write writing " << len << dendl;
 
57
  assert(len > 0);
 
58
  while (len > 0) {
 
59
    int did = ::send( sd, buf, len, MSG_NOSIGNAL );
 
60
    if (did < 0) {
 
61
      //generic_dout(1) << "tcp_write error did = " << did << "  errno " << errno << " " << strerror(errno) << dendl;
 
62
      //generic_derr(1) << "tcp_write error did = " << did << "  errno " << errno << " " << strerror(errno) << dendl;
 
63
      return did;
 
64
    }
 
65
    len -= did;
 
66
    buf += did;
 
67
    //generic_dout(DBL) << "tcp_write did " << did << ", " << len << " left" << dendl;
 
68
  }
 
69
  return 0;
 
70
}