]>
Dogcows Code - chaz/yoink/blob - src/Moof/Socket.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_SOCKET_HH_
13 #define _MOOF_SOCKET_HH_
20 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
25 #include <Moof/Log.hh>
26 #include <Moof/Packet.hh>
27 #include <Moof/Thread.hh>
37 SocketAddress(int type
= SOCK_STREAM
)
40 memset(&mAddress
.in4
, 0, sizeof(mAddress
.in4
));
41 mAddress
.in4
.sin_family
= AF_INET
;
42 mAddress
.in4
.sin_port
= 0;
43 mAddress
.in4
.sin_addr
.s_addr
= INADDR_ANY
;
45 mSize
= sizeof(mAddress
.in4
);
48 SocketAddress(const std::string
& name
, const std::string
& service
,
49 int type
= SOCK_STREAM
, int family
= AF_INET
)
51 ASSERT(type
== SOCK_STREAM
|| type
== SOCK_DGRAM
);
52 ASSERT(family
== AF_INET
|| family
== AF_INET6
|| family
== AF_UNSPEC
);
54 struct addrinfo hints
;
55 memset(&hints
, 0, sizeof(hints
));
56 hints
.ai_family
= family
;
57 hints
.ai_socktype
= type
;
58 hints
.ai_flags
= AI_PASSIVE
| AI_CANONNAME
;
60 struct addrinfo
* addr
;
61 int status
= getaddrinfo(name
.length() > 0 ? name
.c_str() : 0,
62 service
.length() > 0 ? service
.c_str() : 0, &hints
, &addr
);
65 Mf::logError("uh oh, that didn't work!");
69 mType
= addr
->ai_socktype
;
70 memcpy(&mAddress
.sa
, addr
->ai_addr
, addr
->ai_addrlen
);
71 mName
= addr
->ai_canonname
;
72 mSize
= addr
->ai_addrlen
;
77 SocketAddress(const std::string
& name
, const struct addrinfo
* addr
)
79 mType
= addr
->ai_socktype
;
80 memcpy(&mAddress
.sa
, addr
->ai_addr
, addr
->ai_addrlen
);
82 mSize
= addr
->ai_addrlen
;
85 SocketAddress(const struct sockaddr
* addr
, size_t size
,
86 int type
= SOCK_STREAM
)
89 memcpy(&mAddress
.sa
, addr
, size
);
92 char name
[128] = {'\0'};
93 inet_ntop(addr
->sa_family
, (struct sockaddr_in
*)addr
,
99 const std::string
& name() const
104 unsigned short port() const
106 return ntohs(mAddress
.in4
.sin_port
);
116 return mAddress
.sa
.sa_family
;
120 const struct sockaddr
* address() const
131 static int resolve(const std::string
& name
, const std::string
& service
,
132 int type
, int family
, std::vector
<SocketAddress
>& resolved
)
134 ASSERT(type
== SOCK_STREAM
|| type
== SOCK_DGRAM
);
135 ASSERT(family
== AF_INET
|| family
== AF_INET6
|| family
== AF_UNSPEC
);
137 struct addrinfo hints
;
138 memset(&hints
, 0, sizeof(hints
));
139 hints
.ai_family
= family
;
140 hints
.ai_socktype
= type
;
141 hints
.ai_flags
= AI_PASSIVE
| AI_CANONNAME
;
143 struct addrinfo
* list
;
144 int status
= getaddrinfo(name
.length() > 0 ? name
.c_str() : 0,
145 service
.length() > 0 ? service
.c_str() : 0, &hints
, &list
);
146 if (status
!= 0) return -1;
150 const char* canonicalName
= list
->ai_canonname
;
151 struct addrinfo
* addr
= list
;
154 resolved
.push_back(SocketAddress(canonicalName
, addr
));
155 addr
= addr
->ai_next
;
181 Socket(const SocketAddress
& address
) :
184 mFd
= socket(address
.family(), address
.type(), 0);
193 bool isConnected() const
198 const SocketAddress
& address() const
204 void write(const Packet
& packet
)
206 write(mAddress
, packet
);
209 void write(const SocketAddress
& address
, const Packet
& packet
)
211 sendto(mFd
, packet
.bytes(), packet
.size(), 0,
212 address
.address(), address
.size());
218 int size
= recv(mFd
, buffer
, sizeof(buffer
), 0);
220 return Packet(buffer
, size
);
223 Packet
read(SocketAddress
& address
)
228 sockaddr_storage storage
;
230 socklen_t length
= sizeof(addr
);
233 int size
= recvfrom(mFd
, buffer
, sizeof(buffer
), 0,
236 address
= SocketAddress(&addr
.sa
, length
, SOCK_DGRAM
);
237 return Packet(buffer
, size
);
244 SocketAddress mAddress
;
248 class ResolverTask
: public ThreadedTask
252 ResolverTask(const std::string
& name
, const std::string
& service
,
253 int type
= SOCK_STREAM
, int family
= AF_UNSPEC
) :
256 mFunction
= boost::bind(&ResolverTask::resolve
,
257 this, name
, service
, type
, family
);
268 if (!mThread
) mThread
= Mf::detachFunction(mFunction
);
272 const std::vector
<SocketAddress
>& addresses() const
280 int resolve(const std::string
& name
, const std::string
& service
,
281 int type
, int family
)
283 int status
= SocketAddress::resolve(name
, service
,
284 type
, family
, mAddressList
);
290 std::vector
<SocketAddress
> mAddressList
;
298 #endif // _MOOF_SOCKET_HH_
This page took 0.050341 seconds and 5 git commands to generate.