libassa  3.5.1
ConUDPSocket.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------
3 // ConUDPSocket.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1999 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 
13 #ifndef WIN32
14 
15 #include "assa/ConUDPSocket.h"
16 #include "assa/UNIXAddress.h"
17 #include "assa/INETAddress.h"
18 
19 using namespace ASSA;
20 
21 bool
23 connect (const Address& peer_address_)
24 {
25  char self[] = "ConUDPSocket::connect"; trace(self);
26 
27  if ( ::connect (getHandler(),peer_address_.getAddress(),
28  peer_address_.getLength()) < 0 ) {
30  return false;
31  }
32  return true;
33 }
34 
35 void
37 unconnect()
38 {
39  // Ignore errors here. On some systems connect() might return
40  // EAFNOSUPPORT error, on some might not, but it is OK.
41  //
42  char self[] = "ConUDPSocket::unconnect"; trace(self);
43 
44  if ( getDomain() == AF_INET ) {
45  INETAddress addr;
46  SA_IN* addrp = (SA_IN*) addr.getAddress();
47 
48  addrp->sin_family = AF_UNSPEC;
49  (void) connect(addr);
50  }
51  else { // AF_LOCAL
52  // I haven't tested whether it works at all.
53 
54  UNIXAddress addr("");
55  SA_UN* addrp = (SA_UN*) addr.getAddress();
56 
57  addrp->sun_family = AF_UNSPEC;
58  (void) connect(addr);
59  }
60 }
61 
62 int
64 read(char* packet_, const unsigned int size_)
65 {
66  int len;
67  len = ::read(getHandler(), packet_, size_);
68 
69  if (len == -1) {
71  }
72  else if ( len == 0 ) {
74  }
75  return len;
76 }
77 
78 int
80 write (const char* packet_, const unsigned int size_)
81 {
82  return ::write(getHandler(), (const void*) packet_, size_);
83 }
84 
85 #endif // WIN32
Encapsulation of a connected UDP socket.
An incapsulation of TCP/UDP Internet Protocol socket address structure.
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition: Logger.h:429
UNIXAddress encapsulates UNIX domain socket address structure.
virtual SA * getAddress() const =0
Retrieve pointer to the address structure.
virtual const int getLength() const =0
Return length of the underlying address structure.
bool connect(const Address &peer_addr_)
Connect socket to the peer.
void unconnect()
Unconnect connected socket.
int write(const char *buf_=NULL, const unsigned int size_=0)
Perform blocking write by writing packet of specified size.
int read(char *buf_, const unsigned int size_)
Read specified number of bytes off the socket.
SA * getAddress() const
Get hold of address structure.
Definition: INETAddress.h:110
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
@ eofbit
indicates that an input operation reached the end of an input sequence
Definition: Socket.h:82
@ failbit
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
const int getDomain() const
Get socket domain type.
Definition: UDPSocket.h:77
handler_t getHandler() const
Get socket file descriptor.
Definition: UDPSocket.h:74
SA * getAddress() const
Retrieve underlying address structure.
Definition: UNIXAddress.h:64
Definition: Acceptor.h:40
struct sockaddr_in SA_IN
Definition: Address.h:34
struct sockaddr_un SA_UN
Definition: Address.h:44