00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------ 00003 // ConUDPSocket.cpp 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 1999 Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------ 00012 00013 #ifndef WIN32 00014 00015 #include "assa/ConUDPSocket.h" 00016 #include "assa/UNIXAddress.h" 00017 #include "assa/INETAddress.h" 00018 00019 using namespace ASSA; 00020 00021 bool 00022 ConUDPSocket:: 00023 connect (const Address& peer_address_) 00024 { 00025 char self[] = "ConUDPSocket::connect"; trace(self); 00026 00027 if ( ::connect (getHandler(),peer_address_.getAddress(), 00028 peer_address_.getLength()) < 0 ) { 00029 setstate (Socket::failbit); 00030 return false; 00031 } 00032 return true; 00033 } 00034 00035 void 00036 ConUDPSocket:: 00037 unconnect() 00038 { 00039 // Ignore errors here. On some systems connect() might return 00040 // EAFNOSUPPORT error, on some might not, but it is OK. 00041 // 00042 char self[] = "ConUDPSocket::unconnect"; trace(self); 00043 00044 if ( getDomain() == AF_INET ) { 00045 INETAddress addr; 00046 SA_IN* addrp = (SA_IN*) addr.getAddress(); 00047 00048 addrp->sin_family = AF_UNSPEC; 00049 (void) connect(addr); 00050 } 00051 else { // AF_LOCAL 00052 // I haven't tested whether it works at all. 00053 00054 UNIXAddress addr(""); 00055 SA_UN* addrp = (SA_UN*) addr.getAddress(); 00056 00057 addrp->sun_family = AF_UNSPEC; 00058 (void) connect(addr); 00059 } 00060 } 00061 00062 int 00063 ConUDPSocket:: 00064 read(char* packet_, const unsigned int size_) 00065 { 00066 int len; 00067 len = ::read(getHandler(), packet_, size_); 00068 00069 if (len == -1) { 00070 setstate (Socket::failbit); 00071 } 00072 else if ( len == 0 ) { 00073 setstate (Socket::failbit | Socket::eofbit); 00074 } 00075 return len; 00076 } 00077 00078 int 00079 ConUDPSocket:: 00080 write (const char* packet_, const unsigned int size_) 00081 { 00082 return ::write(getHandler(), (const void*) packet_, size_); 00083 } 00084 00085 #endif // WIN32