Revision 41f12636 som/net_helper.c

View differences:

som/net_helper.c
91 91

  
92 92
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
93 93
{
94
  return sendto(from->fd, buffer_ptr, buffer_size, 0,
95
                (const struct sockaddr *)&to->addr, sizeof(struct sockaddr_in));
94
  static struct msghdr msg;
95
  static uint8_t my_hdr;
96
  struct iovec iov[2];
97
  int res;
98

  
99
  iov[0].iov_base = &my_hdr;
100
  iov[0].iov_len = 1;
101
  msg.msg_name = &to->addr;
102
  msg.msg_namelen = sizeof(struct sockaddr_in);
103
  msg.msg_iovlen = 2;
104
  msg.msg_iov = iov;
105
  
106
  do {
107
    iov[1].iov_base = buffer_ptr;
108
    if (buffer_size > 1024 * 60) {
109
      iov[1].iov_len = 1024 * 60;
110
      my_hdr = 0;
111
    } else {
112
      iov[1].iov_len = buffer_size;
113
      my_hdr = 1;
114
    }
115
    buffer_size -= iov[1].iov_len;
116
    buffer_ptr += iov[1].iov_len;
117
    res = sendmsg(from->fd, &msg, 0);
118
  } while (buffer_size > 0);
119

  
120
  return res;
96 121
}
97 122

  
98 123
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
99 124
{
100
  int res;
125
  int res, recv;
101 126
  struct sockaddr_in raddr;
102
  socklen_t raddr_size = sizeof(struct sockaddr_in);
127
  static struct msghdr msg;
128
  static uint8_t my_hdr;
129
  struct iovec iov[2];
130

  
131
  iov[0].iov_base = &my_hdr;
132
  iov[0].iov_len = 1;
133
  msg.msg_name = &raddr;
134
  msg.msg_namelen = sizeof(struct sockaddr_in);
135
  msg.msg_iovlen = 2;
136
  msg.msg_iov = iov;
103 137

  
104 138
  *remote = malloc(sizeof(struct nodeID));
105 139
  if (*remote == NULL) {
106 140
    return -1;
107 141
  }
108
  res = recvfrom(local->fd, buffer_ptr, buffer_size, 0, (struct sockaddr *)&raddr, &raddr_size);
109
  memcpy(&(*remote)->addr, &raddr, raddr_size);
142

  
143
  recv = 0;
144
  do {
145
    iov[1].iov_base = buffer_ptr;
146
    if (buffer_size > 1024 * 60) {
147
      iov[1].iov_len = 1024 * 60;
148
    } else {
149
      iov[1].iov_len = buffer_size;
150
    }
151
    buffer_size -= iov[1].iov_len;
152
    buffer_ptr += iov[1].iov_len;
153
    res = recvmsg(local->fd, &msg, 0);
154
    recv += (res - 1);
155
  } while ((my_hdr == 0) && (buffer_size > 0));
156
  memcpy(&(*remote)->addr, &raddr, msg.msg_namelen);
110 157
  (*remote)->fd = -1;
111
  //fprintf(stderr, "Read %d from %s\n", res, inet_ntoa(raddr.sin_addr));
112 158

  
113
  return res;
159
  return recv;
114 160
}
115 161

  
116 162
const char *node_addr(const struct nodeID *s)

Also available in: Unified diff