monotone

monotone Mtn Source Tree

Root/netxx/recvfrom.cxx

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains the implementation of the Netxx::call_recvfrom function.
35**/
36
37// common header
38#include "common.h"
39
40// Netxx includes
41#include "recvfrom.h"
42#include "sockaddr.h"
43
44// standard includes
45#include <utility>
46#include <string>
47
48//####################################################################
49std::pair<Netxx::signed_size_type, Netxx::Peer> Netxx::call_recvfrom (Socket &socket, void *buffer, size_type length)
50{
51# if defined(WIN32)
52char *buffer_ptr = static_cast<char*>(buffer);
53# else
54void *buffer_ptr(buffer);
55# endif
56
57 SockAddr socket_address(socket.get_type());
58 sockaddr *sa = socket_address.get_sa();
59 os_socklen_type sa_size = socket_address.get_sa_size();
60 os_socklen_ptr_type sa_size_ptr = get_socklen_ptr(sa_size);
61 signed_size_type rc;
62
63 for (;;) {
64if ( (rc = recvfrom(socket.get_socketfd(), buffer_ptr, length, 0, sa, sa_size_ptr)) < 0) {
65 error_type error_code = get_last_error();
66 if (error_code == EWOULDBLOCK) error_code = EAGAIN;
67
68 switch (error_code) {
69case EAGAIN:
70 return std::make_pair(static_cast<signed_size_type>(-1), Peer());
71
72case EINTR:
73 continue;
74
75default:
76{
77 std::string error("recvfrom(2) failed: ");
78 error += str_error(error_code);
79 throw Exception(error);
80}
81 }
82}
83
84return std::make_pair(rc, Peer(socket.get_socketfd(), sa, sa_size));
85 }
86}
87//####################################################################

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status