Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VsnetUDPSocket Class Reference

#include <vsnet_socketudp.h>

Inheritance diagram for VsnetUDPSocket:
VsnetSocket VsnetSocketBase

Public Member Functions

 VsnetUDPSocket (int sock, const AddressIP &remote_ip, SocketSet &set)
 
 ~VsnetUDPSocket ()
 
virtual bool isTcp () const
 
virtual int optPayloadSize () const
 
virtual int queueLen (int pri)
 
virtual bool isActive ()
 
virtual int sendbuf (Packet *packet, const AddressIP *to, int pcktflags)
 
virtual int recvbuf (Packet *p, AddressIP *ipadr)
 
virtual bool lower_selected (int datalen=-1)
 
virtual void dump (std::ostream &ostr) const
 
virtual bool setRemoteAddress (const AddressIP &inp)
 
- Public Member Functions inherited from VsnetSocket
 VsnetSocket (int sock, const AddressIP &remote_ip, const char *socktype, SocketSet &set)
 
virtual ~VsnetSocket ()
 
bool eq (const VsnetSocket &r) const
 
bool lt (const VsnetSocket &r) const
 
bool sameAddress (const VsnetSocket &r) const
 
const AddressIPgetRemoteAddress () const
 
const AddressIPgetLocalAddress () const
 
bool setLocalAddress (const AddressIP &lip)
 
- Public Member Functions inherited from VsnetSocketBase
 VsnetSocketBase (int fd, const char *socktype, SocketSet &set)
 
virtual ~VsnetSocketBase ()
 
bool valid () const
 
int get_fd () const
 
int close_fd ()
 
const char * get_socktype () const
 
bool set_block ()
 
bool set_nonblock ()
 
bool get_nonblock () const
 
void disconnect (const char *s)
 
virtual bool need_test_writable ()
 
virtual bool write_on_negative ()
 
virtual int get_write_fd () const
 
virtual int lower_sendbuf ()
 
virtual void lower_clean_sendbuf ()
 
virtual bool isReadyToSend (fd_set *)
 
void setSet (SocketSet *set)
 

Additional Inherited Members

- Protected Member Functions inherited from VsnetSocketBase
virtual void child_disconnect (const char *s)
 
- Protected Attributes inherited from VsnetSocket
AddressIP _remote_ip
 
AddressIP _local_ip
 

Detailed Description

Definition at line 15 of file vsnet_socketudp.h.

Constructor & Destructor Documentation

VsnetUDPSocket::VsnetUDPSocket ( int  sock,
const AddressIP remote_ip,
SocketSet set 
)

Definition at line 22 of file vsnet_socketudp.cpp.

References MAXBUFFER, and VsnetUDPSocket().

Referenced by VsnetUDPSocket().

22  :
23  VsnetSocket( sock, remote_ip, "VsnetUDPSocket", socketset )
24  , _mtu_size_estimation( 1024 )
25 {
26  _negotiated_max_size = MAXBUFFER;
27  _recv_buf = new char[MAXBUFFER];
28 }
VsnetUDPSocket::~VsnetUDPSocket ( )

Definition at line 30 of file vsnet_socketudp.cpp.

31 {
32  delete[] _recv_buf;
33 }

Member Function Documentation

void VsnetUDPSocket::dump ( std::ostream &  ostr) const
virtual

Implements VsnetSocket.

Definition at line 119 of file vsnet_socketudp.cpp.

References VsnetSocket::_remote_ip, and VsnetSocketBase::get_fd().

120 {
121  ostr<<"( s="<<get_fd()<<" UDP r="<<_remote_ip<<" )";
122 }
bool VsnetUDPSocket::isActive ( )
virtual

Implements VsnetSocketBase.

Definition at line 124 of file vsnet_socketudp.cpp.

References VSMutex::lock(), and VSMutex::unlock().

125 {
126  _cpq_mx.lock();
127  bool ret = (_cpq.empty() == false);
128  _cpq_mx.unlock();
129  return ret;
130 }
virtual bool VsnetUDPSocket::isTcp ( ) const
inlinevirtual

Implements VsnetSocket.

Definition at line 20 of file vsnet_socketudp.h.

21  {
22  return false;
23  }
bool VsnetUDPSocket::lower_selected ( int  datalen = -1)
virtual

Implements VsnetSocketBase.

Definition at line 132 of file vsnet_socketudp.cpp.

References VsnetSocketBase::_set, SocketSet::add_pending(), COUT, VsnetSocketBase::get_fd(), VSMutex::lock(), VSMutex::unlock(), and vsnetLastError().

133 {
134  int ret = 0;
135  socklen_t len1;
136  AddressIP from;
137  size_t lentoread = _negotiated_max_size;
138  if (datalen != -1)
139  lentoread = datalen;
140  //In UDP mode, always receive data on sock
141  len1 = sizeof (sockaddr_in);
142  ret = recvfrom( get_fd(), _recv_buf, lentoread,
143  0, (sockaddr*) (sockaddr_in*) &from, &len1 );
144  if (ret < 0) {
145  COUT<<" fd="<<get_fd()<<" error receiving: "
146  <<vsnetLastError()<<endl;
147  } else if (ret == 0) {
148  COUT<<" Received "<<ret<<" bytes : "<<_recv_buf
149  <<" (UDP socket closed, strange)"<<endl;
150  } else {
151 //COUT << "NETUI : Recvd " << ret << " bytes" << " <- " << from << endl;
152  Pending mem( _recv_buf, ret, from );
153  _cpq_mx.lock();
154  _cpq.push( mem );
155  _cpq_mx.unlock();
156  if (_set) _set->add_pending( get_fd() );
157  return true;
158  }
159  return false;
160 }
int VsnetUDPSocket::optPayloadSize ( ) const
virtual

Implements VsnetSocket.

Definition at line 35 of file vsnet_socketudp.cpp.

36 {
37  return _mtu_size_estimation;
38 }
int VsnetUDPSocket::queueLen ( int  pri)
virtual

Implements VsnetSocket.

Definition at line 40 of file vsnet_socketudp.cpp.

41 {
42  return 0;
43 }
int VsnetUDPSocket::recvbuf ( Packet p,
AddressIP from 
)
virtual

This function copies or moves data into the given PacketMem variable. It is preferred over the other recvbuf function because it may reduce the number of copy operations by at least one.

Implements VsnetSocket.

Definition at line 101 of file vsnet_socketudp.cpp.

References VsnetSocketBase::_set, buffer, VsnetSocketBase::get_fd(), PacketMem::len(), VSMutex::lock(), SocketSet::rem_pending(), and VSMutex::unlock().

102 {
103  _cpq_mx.lock();
104  if ( _cpq.empty() ) {
105  _cpq_mx.unlock();
106  if (_set) _set->rem_pending( get_fd() );
107  return -1;
108  }
109  PacketMem buffer = _cpq.front().mem;
110  if (ipadr) *ipadr = _cpq.front().ip;
111  _cpq.pop();
112  _cpq_mx.unlock();
113  int len = buffer.len();
114  Packet packet( buffer );
115  *p = packet;
116  return len;
117 }
int VsnetUDPSocket::sendbuf ( Packet packet,
const AddressIP to,
int  pcktflags 
)
virtual

Implements VsnetSocket.

Definition at line 69 of file vsnet_socketudp.cpp.

References VsnetSocket::_remote_ip, COUT, Packet::display(), VsnetSocketBase::get_fd(), Packet::getCommand(), Packet::getSendBuffer(), Packet::getSendBufferLength(), vsnetLastError(), and x.

70 {
71 //COUT << "enter " << __PRETTY_FUNCTION__ << endl;
72  int numsent;
73 
74  //In UDP mode, always send on this->sock
75  const sockaddr_in *dest = to;
76  if (dest == NULL) dest = &_remote_ip;
77  assert( dest != NULL );
78  if (packet->getSendBufferLength() >= 512) {
79  int *x = NULL;
80  COUT<<"Trying to send UDP "<<packet->getCommand()<<" of invalid size "
81  <<packet->getSendBufferLength()<<"(>=512)"<<endl;
82  packet->display( __FILE__, __LINE__ );
83  cout<<flush;
84  cerr<<flush;
85 #ifndef NO_CRASH_INVALID_UDP
86  (*x) = 1; //Cause crash.
87 #endif
88  return -1;
89  }
90  numsent = sendto( get_fd(),
91  packet->getSendBuffer(), packet->getSendBufferLength(),
92  0, (sockaddr*) dest, sizeof (struct sockaddr_in) );
93  if (numsent < 0) {
94  COUT<<"Error sending: "<<vsnetLastError()<<endl;
95  return -1;
96  }
97 //cout<<"Sent "<<numsent<<" bytes"<<" -> "<<inet_ntoa( dest->sin_addr)<<":"<<ntohs(dest->sin_port)<<endl;
98  return numsent;
99 }
bool VsnetUDPSocket::setRemoteAddress ( const AddressIP inp)
virtual

Reimplemented from VsnetSocket.

Definition at line 64 of file vsnet_socketudp.cpp.

References VsnetSocket::_remote_ip.

65 {
66  _remote_ip = to;
67  return true;
68 }

The documentation for this class was generated from the following files: