libassa  3.5.1
UnConUDPSocket.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // UnConUDPSocket.C
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1999 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 // Created: 03/23/99
13 //------------------------------------------------------------------------------
14 
15 #include "assa/UnConUDPSocket.h"
16 
17 #if defined (WIN32)
18 typedef unsigned int socklen_t;
19 #endif
20 
21 using namespace ASSA;
22 
23 int
25 recvfrom (char* buf_, int size_, Address* peer_addr_)
26 {
27  // ::recvfrom() can return 0 bytes which is not
28  // considered an eof. Peer can advertise its address to
29  // the server by sending 0 bytes length message.
30  //
31 
32  // char self[] = "Socket::recvfro"; trace(self);
33 
34  // Setting saddr_len is crucial to proper ::recvfrom() operation.
35  // If left improprely initialized, ::recvfrom() won't fill in peer's
36  // address and won't report an error either. If SA ptr is passed to
37  // recvfrom() along with uninitialized address len (or set to 0),
38  // recvfrom() returns zeroed out address structure!!!
39 
40  int len;
41  socklen_t pa_len = peer_addr_->getLength();
42 
43  SA* pa = peer_addr_->getAddress();
44 
45 #if defined (__CYGWIN32__) || defined (WIN32)
46  len = ::recvfrom(getHandler(), buf_, size_, 0, pa, (int*)&pa_len);
47 #else // posix/unix
48  len = ::recvfrom(getHandler(), buf_, size_, 0, pa, &pa_len);
49 #endif
50 
51  // Q: for UNIX domain socket, returned length will be essential to
52  // remember and probably should be set in peer_addr_ by calling
53  // setLength().....
54 
55  return len;
56 }
57 
58 int
60 sendto (const char* buf_, const unsigned int size_, const Address* peer_addr_)
61 {
62  return ::sendto (getHandler(), buf_, size_, 0,
63  peer_addr_->getAddress(),
64  peer_addr_->getLength());
65 }
66 
67 
Class UnConUPDSocket class is unconnected UDP socket.
virtual SA * getAddress() const =0
Retrieve pointer to the address structure.
virtual const int getLength() const =0
Return length of the underlying address structure.
handler_t getHandler() const
Get socket file descriptor.
Definition: UDPSocket.h:74
int recvfrom(char *buf_, int size_, Address *peer_addr_)
recvfrom() function receives a message from connectionless-mode socket.
int sendto(const char *buf_, const unsigned int size_, const Address *dest_addr_)
sendto() function sends a message through connectionless-mode socket.
Definition: Acceptor.h:40
struct sockaddr SA
Definition: Address.h:33