~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to src/udp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <netdb.h>
26
26
#include <sys/poll.h>
27
27
#include <errno.h>
 
28
#include <string.h>
28
29
 
29
30
log_define("cxxtools.net.udp")
30
31
 
36
37
  //////////////////////////////////////////////////////////////////////
37
38
  // UdpSender
38
39
  //
39
 
  UdpSender::UdpSender(const char* ipaddr, unsigned short int port)
 
40
  UdpSender::UdpSender(const char* ipaddr, unsigned short int port, bool bcast)
40
41
    : connected(false)
41
42
  {
42
 
    connect(ipaddr, port);
 
43
    connect(ipaddr, port, bcast);
43
44
  }
44
45
 
45
 
  void UdpSender::connect(const char* ipaddr, unsigned short int port)
 
46
  void UdpSender::connect(const char* ipaddr, unsigned short int port, bool bcast)
46
47
  {
47
 
    Addrinfo ai(ipaddr, port);
 
48
    struct addrinfo hints;
 
49
    memset(&hints, 0, sizeof(hints));
 
50
    hints.ai_socktype = SOCK_DGRAM;
 
51
 
 
52
    Addrinfo ai(ipaddr, port, hints);
48
53
 
49
54
    for (Addrinfo::const_iterator it = ai.begin(); it != ai.end(); ++it)
50
55
    {
51
 
      Socket::create(it->ai_family, SOCK_DGRAM, 0);
 
56
      try
 
57
      {
 
58
        Socket::create(it->ai_family, SOCK_DGRAM, 0);
 
59
      }
 
60
      catch (const Exception&)
 
61
      {
 
62
        continue;
 
63
      }
 
64
 
 
65
      if (bcast)
 
66
      {
 
67
        const int on = 1;
 
68
        if (::setsockopt(getFd(), SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
 
69
          throw Exception("setsockopt");
 
70
      }
52
71
 
53
72
      if (::connect(getFd(), it->ai_addr, it->ai_addrlen) == 0)
54
73
      {
57
76
      }
58
77
    }
59
78
 
60
 
    throw Exception(errno, "connect");
 
79
    throw Exception("connect");
61
80
  }
62
81
 
63
82
  UdpSender::size_type UdpSender::send(const void* message, size_type length, int flags) const
84
103
      if (getTimeout() == 0)
85
104
        throw Timeout();
86
105
 
87
 
      doPoll(POLLIN);
 
106
      poll(POLLIN);
88
107
 
89
108
      ret = ::recv(getFd(), buffer, length, flags);
90
109
    }
123
142
    int reuseAddr = 1;
124
143
    for (Addrinfo::const_iterator it = ai.begin(); it != ai.end(); ++it)
125
144
    {
126
 
      Socket::create(it->ai_family, SOCK_DGRAM, 0);
 
145
      try
 
146
      {
 
147
        Socket::create(it->ai_family, SOCK_DGRAM, 0);
 
148
      }
 
149
      catch (const Exception&)
 
150
      {
 
151
        continue;
 
152
      }
127
153
 
128
154
      log_debug("setsockopt");
129
155
      if (::setsockopt(getFd(), SOL_SOCKET, SO_REUSEADDR,
130
156
          &reuseAddr, sizeof(reuseAddr)) < 0)
131
157
        throw Exception(errno, "setsockopt");
132
158
 
133
 
      log_debug("bind");
 
159
      log_debug("bind ip " << ipaddr << " port " << port);
134
160
      if (::bind(getFd(), it->ai_addr, it->ai_addrlen) == 0)
135
161
      {
136
162
        memmove(&peeraddr, it->ai_addr, it->ai_addrlen);
 
163
        peeraddrLen = it->ai_addrlen;
137
164
        return;
138
165
      }
139
166
    }
152
179
      if (getTimeout() == 0)
153
180
        throw Timeout();
154
181
 
155
 
      doPoll(POLLIN);
 
182
      poll(POLLIN);
156
183
 
157
184
      ret = ::recvfrom(getFd(), buffer, length, flags, reinterpret_cast <struct sockaddr *> (&peeraddr), &peeraddrLen);
158
185
    }