diff options
author | neodarz <neodarz@neodarz.net> | 2018-08-11 20:21:34 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2018-08-11 20:21:34 +0200 |
commit | 0ea5fc66924303d1bf73ba283a383e2aadee02f2 (patch) | |
tree | 2568e71a7ccc44ec23b8bb3f0ff97fb6bf2ed709 /cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?rev=1.11&content-type=text/x-cvsweb-markup&sortby=date | |
download | nevrax-website-self-hostable-0ea5fc66924303d1bf73ba283a383e2aadee02f2.tar.xz nevrax-website-self-hostable-0ea5fc66924303d1bf73ba283a383e2aadee02f2.zip |
Initial commit
Diffstat (limited to '')
-rw-r--r-- | cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?rev=1.11&content-type=text/x-cvsweb-markup&sortby=date/index.html | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?rev=1.11&content-type=text/x-cvsweb-markup&sortby=date/index.html b/cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?rev=1.11&content-type=text/x-cvsweb-markup&sortby=date/index.html new file mode 100644 index 00000000..02dd6ba2 --- /dev/null +++ b/cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?rev=1.11&content-type=text/x-cvsweb-markup&sortby=date/index.html @@ -0,0 +1,220 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> +<HEAD><style> A { color:black }</style> +<!-- hennerik CVSweb $Revision: 1.93 $ --> +<TITLE>code/nel/src/net/udp_sock.cpp - view - 1.11</TITLE></HEAD> +<BODY BGCOLOR="#eeeeee"> +<table width="100%" border=0 cellspacing=0 cellpadding=1 bgcolor="#aaaaaa"><tr valign=bottom><td><a href="udp_sock.cpp?sortby=date"><IMG SRC="http://www.nevrax.org/inc/img/picto-up.gif" ALT="[BACK]" BORDER="0" WIDTH="14" HEIGHT="13"></a> <b>Return to <A HREF="udp_sock.cpp?sortby=date">udp_sock.cpp</A> + CVS log</b> <IMG SRC="http://www.nevrax.org/inc/img/picto-news.gif" ALT="[TXT]" BORDER="0" WIDTH="13" HEIGHT="15"></td><td align=right><IMG SRC="http://www.nevrax.org/inc/img/picto-dir.gif" ALT="[DIR]" BORDER="0" WIDTH="15" HEIGHT="13"> <b>Up to <a href="/cvs/cvsweb.cgi/?sortby=date">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=date">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=date">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/?sortby=date">src</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/?sortby=date">net</a></b></td></tr></table><HR noshade><table width="100%"><tr><td bgcolor="#ffffff">File: <a href="/cvs/cvsweb.cgi/?sortby=date">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=date">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=date">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/?sortby=date">src</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/?sortby=date">net</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?sortby=date">udp_sock.cpp</a> (<A HREF="/cvs/cvsweb.cgi/~checkout~/code/nel/src/net/udp_sock.cpp?rev=1.11&sortby=date" target="cvs_checkout" onClick="window.open('/cvs/cvsweb.cgi/~checkout~/code/nel/src/net/udp_sock.cpp?rev=1.11','cvs_checkout','resizeable,scrollbars');"><b>download</b></A>)<BR> +Revision <B>1.11</B>, <i>Wed Apr 17 09:54:00 2002 UTC</i> (3 months, 1 week ago) by <i>cado</i> +<BR>Branch: <b>MAIN</b> +<BR>CVS Tags: <b>georges_v2, HEAD</b><BR>Changes since <b>1.10: +3 -4 + lines</b><PRE> +BUGFIX: wrong address +</PRE> +</td></tr></table><HR noshade><PRE>/** \file udp_sock.cpp + * Network engine, layer 0, udp socket + * + * $Id: udp_sock.cpp,v 1.11 2002/04/17 09:54:00 cado Exp $ + */ + +/* Copyright, 2000 Nevrax Ltd. + * + * This file is part of NEVRAX NEL. + * NEVRAX NEL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + + * NEVRAX NEL is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with NEVRAX NEL; see the file COPYING. If not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#include "stdnet.h" + +#include "nel/net/udp_sock.h" + +#ifdef NL_OS_WINDOWS +#include <winsock2.h> +#define socklen_t int +#define ERROR_NUM WSAGetLastError() + +#elif defined NL_OS_UNIX +#include <unistd.h> +#include <sys/types.h> +#include <sys/time.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <errno.h> +//#include <fcntl.h> +#define SOCKET_ERROR -1 +#define INVALID_SOCKET -1 +#define ERROR_NUM errno +#define ERROR_MSG strerror(errno) +typedef int SOCKET; + +#endif + +using namespace NLMISC; + +namespace NLNET { + + +/* + * Constructor + */ +CUdpSock::CUdpSock( bool logging ) : + CSock( logging ), + _Bound( false ) +{ + // Socket creation + createSocket( SOCK_DGRAM, IPPROTO_UDP ); +} + + +/** Binds the socket to the specified port. Call bind() for an unreliable socket if the host acts as a server and waits for + * messages. If the host acts as a client, call sendTo(), there is no need to bind the socket. + */ +void CUdpSock::bind( uint16 port ) +{ + CInetAddress addr; // any IP address + addr.setPort( port ); + bind( addr ); + setLocalAddress(); // will not set the address if the host is multihomed, use bind(CInetAddress) instead +} + + +/* + * Same as bind(uint16) but binds on a specified address/port (useful when the host has several addresses) + */ +void CUdpSock::bind( const CInetAddress& addr ) +{ +#ifndef NL_OS_WINDOWS + // Set Reuse Address On (does not work on Win98 and is useless on Win2000) + int value = true; + if ( setsockopt( _Sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value) ) == SOCKET_ERROR ) + { + throw ESocket( "ReuseAddr failed" ); + } +#endif + + _LocalAddr = addr; + + // Bind the socket + if ( ::bind( _Sock, (sockaddr*)(_LocalAddr.sockAddr()), sizeof(sockaddr) ) == SOCKET_ERROR ) + { + throw ESocket( "Bind failed" ); + } + _Bound = true; + if ( _Logging ) + { + nldebug( "LNETL0: Socket %d bound at %s", _Sock, _LocalAddr.asString().c_str() ); + } +} + + +/* + * Sends a message + */ +void CUdpSock::sendTo( const uint8 *buffer, uint len, const CInetAddress& addr ) +{ + + // Send + if ( ::sendto( _Sock, (const char*)buffer, len, 0, (sockaddr*)(addr.sockAddr()), sizeof(sockaddr) ) != (sint32)len ) + { + throw ESocket( "Unable to send datagram" ); + } + _BytesSent += len; + + if ( _Logging ) + { + nldebug( "LNETL0: Socket %d sent %d bytes to %s", _Sock, len, addr.asString().c_str() ); + } + + // If socket is unbound, retrieve local address + if ( ! _Bound ) + { + setLocalAddress(); + _Bound = true; + } + +#ifdef NL_OS_WINDOWS + // temporary by ace to know size of SO_MAX_MSG_SIZE + static bool first = true; + if (first) + { + uint MMS, SB; + int size = sizeof (MMS); + getsockopt (_Sock, SOL_SOCKET, SO_SNDBUF, (char *)&SB, &size); + getsockopt (_Sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&MMS, &size); + nlinfo ("the udp SO_MAX_MSG_SIZE=%u, SO_SNDBUF=%u", MMS, SB); + first = false; + } +#endif +} + + +/* + * Receives data from the peer. (blocking function) + */ +void CUdpSock::receive( uint8 *buffer, uint32& len ) +{ + nlassert( _Connected && (buffer!=NULL) ); + + // Receive incoming message + len = ::recv( _Sock, (char*)buffer, len , 0 ); + + // Check for errors (after setting the address) + if ( len == SOCKET_ERROR ) + { + throw ESocket( "Cannot receive data" ); + } + + _BytesReceived += len; + if ( _Logging ) + { + nldebug( "LNETL0: Socket %d received %d bytes from peer %s", _Sock, len, _RemoteAddr.asString().c_str() ); + } +} + + +/* + * Receives data and say who the sender is. (blocking function) + */ +void CUdpSock::receivedFrom( uint8 *buffer, uint& len, CInetAddress& addr ) +{ + // Receive incoming message + sockaddr_in saddr; + socklen_t saddrlen = sizeof(saddr); + + len = ::recvfrom( _Sock, (char*)buffer, len , 0, (sockaddr*)&saddr, &saddrlen ); + + // If an error occurs, the saddr is not valid + // When the remote socket is closed, get sender's address to know who is quitting + addr.setSockAddr( &saddr ); + + // Check for errors (after setting the address) + if ( len == SOCKET_ERROR ) + { + throw ESocket( "Cannot receive data" ); + } + + _BytesReceived += len; + if ( _Logging ) + { + nldebug( "LNETL0: Socket %d received %d bytes from %s", _Sock, len, addr.asString().c_str() ); + } +} + + +} // NLNET +</PRE>
\ No newline at end of file |