iCub-main
Loading...
Searching...
No Matches
DSocket.cpp
Go to the documentation of this file.
1/*
2* Copyright (C) 2012 RobotCub Consortium
3* Author: Alessandro Scalzo alessandro.scalzo@iit.it
4* CopyPolicy: Released under the terms of the GNU GPL v2.0.
5*
6*/
7
8#include "DSocket.h"
9#include <ace/Time_Value.h>
10#include <ace/OS_NS_sys_socket.h>
11
12//#include <arpa/inet.h>
13
15{
16 ACE_OS::socket_init(2,2);
17 mSocket=NULL;
18}
20{
21 Close();
22 ACE_OS::socket_fini();
23}
24
25bool DSocket::Create(ACE_UINT16 port,std::string& address)
26{
27 mSocket=new ACE_SOCK_Dgram_Bcast(ACE_INET_Addr(port,address.c_str()));
28
29 return mSocket!=NULL;
30}
31
32bool DSocket::Create(ACE_UINT16 port,ACE_UINT32 address)
33{
34 mSocket=new ACE_SOCK_Dgram_Bcast(ACE_INET_Addr(port,address));
35
36 return mSocket!=NULL;
37}
38
39bool DSocket::Create(eOipv4addr_t ipv4, eOipv4port_t port)
40{
41 // eOipv4port_t is in host format (little endian) (as ACE requires).
42 // eOipv4addr_t is in network order (big endian) (but ACE requires host format)
43 // hence ... use ntohl() for ipv4
44 ACE_UINT32 address = ntohl(ipv4);
45 return Create(port, address);
46}
47
48void DSocket::SendTo(void* data,size_t len,ACE_UINT16 port,std::string& address)
49{
50 mSocket->send(data,len,ACE_INET_Addr(port,address.c_str()));
51}
52
53void DSocket::SendTo(void* data,size_t len,ACE_UINT16 port,ACE_UINT32 address)
54{
55 mSocket->send(data,len,ACE_INET_Addr(port,address));
56}
57
58void DSocket::SendTo(eOipv4addr_t ipv4, eOipv4port_t port, void* data, size_t len)
59{
60 ACE_UINT32 address = ntohl(ipv4);
61 mSocket->send(data, len, ACE_INET_Addr(port, address));
62}
63
64void DSocket::SendBroad(void* data,size_t len,ACE_UINT16 port)
65{
66 mSocket->send(data,len,port);
67}
68
69
70void DSocket::SendBroad(eOipv4port_t port, void* data, size_t len)
71{
72 mSocket->send(data, len, port);
73}
74
75ssize_t DSocket::ReceiveFrom(void* data,size_t len,std::string &address,ACE_UINT16 &port,int wait_msec)
76{
77 ACE_Time_Value tv(wait_msec/1000,(wait_msec%1000)*1000);
78 ACE_INET_Addr ace_addr;
79 ssize_t nrec=mSocket->recv(data,len,ace_addr,0,&tv);
80
81 if (nrec>0)
82 {
83 address=ace_addr.get_host_addr();
84 port=ace_addr.get_port_number();
85 }
86 else
87 {
88 address="";
89 port=0;
90 }
91
92 return nrec;
93}
94
95ssize_t DSocket::ReceiveFrom(void* data,size_t len,ACE_UINT32 &address,ACE_UINT16 &port,int wait_msec)
96{
97 ACE_Time_Value tv(wait_msec/1000,(wait_msec%1000)*1000);
98 ACE_INET_Addr ace_addr;
99 ssize_t nrec=mSocket->recv(data,len,ace_addr,0,&tv);
100
101 if (nrec>0)
102 {
103 address=ace_addr.get_ip_address();
104 port=ace_addr.get_port_number();
105 }
106 else
107 {
108 address=0;
109 port=0;
110 }
111
112 return nrec;
113}
114
115
116ssize_t DSocket::ReceiveFrom(eOipv4addr_t &ipv4, eOipv4port_t &port, void* data, size_t len, int wait_msec)
117{
118 ACE_Time_Value tv(wait_msec/1000, (wait_msec%1000)*1000);
119 ACE_INET_Addr ace_addr;
120 ssize_t nrec = mSocket->recv(data, len, ace_addr, 0, &tv);
121
122
123 if(nrec>0)
124 {
125 ipv4 = htonl(ace_addr.get_ip_address());
126 port = ace_addr.get_port_number();
127 }
128 else
129 {
130 ipv4 = 0;
131 port = 0;
132 }
133
134 return nrec;
135}
136
138{
139 if (mSocket)
140 {
141 mSocket->close();
142 delete mSocket;
143 mSocket=NULL;
144 }
145}
146
@ data
void SendTo(eOipv4addr_t ipv4, eOipv4port_t port, void *data, size_t len)
Definition DSocket.cpp:58
ACE_SOCK_Dgram_Bcast * mSocket
Definition DSocket.h:42
bool Create(ACE_UINT16 port, std::string &address)
Definition DSocket.cpp:25
DSocket()
Definition DSocket.cpp:14
void SendBroad(eOipv4port_t port, void *data, size_t len)
Definition DSocket.cpp:70
ssize_t ReceiveFrom(eOipv4addr_t &ipv4, eOipv4port_t &port, void *data, size_t len, int wait_msec)
Definition DSocket.cpp:116
~DSocket()
Definition DSocket.cpp:19
void Close()
Definition DSocket.cpp:137