<s> """<STR_LIT>""" <EOL> import socket <EOL> import operator <EOL> import struct <EOL> import warnings <EOL> from zope . interface import implements <EOL> from twisted . python . runtime import platformType <EOL> if platformType == '<STR_LIT:win32>' : <EOL> from errno import WSAEWOULDBLOCK <EOL> from errno import WSAEINTR , WSAEMSGSIZE , WSAETIMEDOUT <EOL> from errno import WSAECONNREFUSED , WSAECONNRESET , WSAENETRESET <EOL> from errno import WSAEINPROGRESS <EOL> _sockErrReadIgnore = [ WSAEINTR , WSAEWOULDBLOCK , WSAEMSGSIZE , WSAEINPROGRESS ] <EOL> _sockErrReadRefuse = [ WSAECONNREFUSED , WSAECONNRESET , WSAENETRESET , <EOL> WSAETIMEDOUT ] <EOL> EMSGSIZE = WSAEMSGSIZE <EOL> ECONNREFUSED = WSAECONNREFUSED <EOL> EAGAIN = WSAEWOULDBLOCK <EOL> EINTR = WSAEINTR <EOL> else : <EOL> from errno import EWOULDBLOCK , EINTR , EMSGSIZE , ECONNREFUSED , EAGAIN <EOL> _sockErrReadIgnore = [ EAGAIN , EINTR , EWOULDBLOCK ] <EOL> _sockErrReadRefuse = [ ECONNREFUSED ] <EOL> from twisted . internet import base , defer , address <EOL> from twisted . python import log , failure <EOL> from twisted . internet import abstract , error , interfaces <EOL> class Port ( base . BasePort ) : <EOL> """<STR_LIT>""" <EOL> implements ( <EOL> interfaces . IListeningPort , interfaces . IUDPTransport , <EOL> interfaces . ISystemHandle ) <EOL> addressFamily = socket . AF_INET <EOL> socketType = socket . SOCK_DGRAM <EOL> maxThroughput = <NUM_LIT> * <NUM_LIT> <EOL> _realPortNumber = None <EOL> def __init__ ( self , port , proto , interface = '<STR_LIT>' , maxPacketSize = <NUM_LIT> , reactor = None ) : <EOL> """<STR_LIT>""" <EOL> base . BasePort . __init__ ( self , reactor ) <EOL> self . port = port <EOL> self . protocol = proto <EOL> self . maxPacketSize = maxPacketSize <EOL> self . interface = interface <EOL> self . setLogStr ( ) <EOL> self . _connectedAddr = None <EOL> def __repr__ ( self ) : <EOL> if self . _realPortNumber is not None : <EOL> return "<STR_LIT>" % ( self . protocol . __class__ , self . _realPortNumber ) <EOL> else : <EOL> return "<STR_LIT>" % ( self . protocol . __class__ , ) <EOL> def getHandle ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . socket <EOL> def startListening ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _bindSocket ( ) <EOL> self . _connectToProtocol ( ) <EOL> def _bindSocket ( self ) : <EOL> try : <EOL> skt = self . createInternetSocket ( ) <EOL> skt . bind ( ( self . interface , self . port ) ) <EOL> except socket . error , le : <EOL> raise error . CannotListenError , ( self . interface , self . port , le ) <EOL> self . _realPortNumber = skt . getsockname ( ) [ <NUM_LIT:1> ] <EOL> log . msg ( "<STR_LIT>" % ( <EOL> self . _getLogPrefix ( self . protocol ) , self . _realPortNumber ) ) <EOL> self . connected = <NUM_LIT:1> <EOL> self . socket = skt <EOL> self . fileno = self . socket . fileno <EOL> def _connectToProtocol ( self ) : <EOL> self . protocol . makeConnection ( self ) <EOL> self . startReading ( ) <EOL> def doRead ( self ) : <EOL> """<STR_LIT>""" <EOL> read = <NUM_LIT:0> <EOL> while read < self . maxThroughput : <EOL> try : <EOL> data , addr = self . socket . recvfrom ( self . maxPacketSize ) <EOL> except socket . error , se : <EOL> no = se . args [ <NUM_LIT:0> ] <EOL> if no in _sockErrReadIgnore : <EOL> return <EOL> if no in _sockErrReadRefuse : <EOL> if self . _connectedAddr : <EOL> self . protocol . connectionRefused ( ) <EOL> return <EOL> raise <EOL> else : <EOL> read += len ( data ) <EOL> try : <EOL> self . protocol . datagramReceived ( data , addr ) <EOL> except : <EOL> log . err ( ) <EOL> def write ( self , datagram , addr = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _connectedAddr : <EOL> assert addr in ( None , self . _connectedAddr ) <EOL> try : <EOL> return self . socket . send ( datagram ) <EOL> except socket . error , se : <EOL> no = se . args [ <NUM_LIT:0> ] <EOL> if no == EINTR : <EOL> return self . write ( datagram ) <EOL> elif no == EMSGSIZE : <EOL> raise error . MessageLengthError , "<STR_LIT>" <EOL> elif no == ECONNREFUSED : <EOL> self . protocol . connectionRefused ( ) <EOL> else : <EOL> raise <EOL> else : <EOL> assert addr != None <EOL> if not addr [ <NUM_LIT:0> ] . replace ( "<STR_LIT:.>" , "<STR_LIT>" ) . isdigit ( ) and addr [ <NUM_LIT:0> ] != "<STR_LIT>" : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> try : <EOL> return self . socket . sendto ( datagram , addr ) <EOL> except socket . error , se : <EOL> no = se . args [ <NUM_LIT:0> ] <EOL> if no == EINTR : <EOL> return self . write ( datagram , addr ) <EOL> elif no == EMSGSIZE : <EOL> raise error . MessageLengthError , "<STR_LIT>" <EOL> elif no == ECONNREFUSED : <EOL> return <EOL> else : <EOL> raise <EOL> def writeSequence ( self , seq , addr ) : <EOL> self . write ( "<STR_LIT>" . join ( seq ) , addr ) <EOL> def connect ( self , host , port ) : <EOL> """<STR_LIT>""" <EOL> if self . _connectedAddr : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> if not abstract . isIPAddress ( host ) : <EOL> raise ValueError , "<STR_LIT>" <EOL> self . _connectedAddr = ( host , port ) <EOL> self . socket . connect ( ( host , port ) ) <EOL> def _loseConnection ( self ) : <EOL> self . stopReading ( ) <EOL> if self . connected : <EOL> self . reactor . callLater ( <NUM_LIT:0> , self . connectionLost ) <EOL> def stopListening ( self ) : <EOL> if self . connected : <EOL> result = self . d = defer . Deferred ( ) <EOL> else : <EOL> result = None <EOL> self . _loseConnection ( ) <EOL> return result <EOL> def loseConnection ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> self . stopListening ( ) <EOL> def connectionLost ( self , reason = None ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( '<STR_LIT>' % self . _realPortNumber ) <EOL> self . _realPortNumber = None <EOL> base . BasePort . connectionLost ( self , reason ) <EOL> self . protocol . doStop ( ) <EOL> self . socket . close ( ) <EOL> del self . socket <EOL> del self . fileno <EOL> if hasattr ( self , "<STR_LIT:d>" ) : <EOL> self . d . callback ( None ) <EOL> del self . d <EOL> def setLogStr ( self ) : <EOL> """<STR_LIT>""" <EOL> logPrefix = self . _getLogPrefix ( self . protocol ) <EOL> self . logstr = "<STR_LIT>" % logPrefix <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . logstr <EOL> def getHost ( self ) : <EOL> """<STR_LIT>""" <EOL> return address . IPv4Address ( '<STR_LIT>' , * self . socket . getsockname ( ) ) <EOL> class MulticastMixin : <EOL> """<STR_LIT>""" <EOL> def getOutgoingInterface ( self ) : <EOL> i = self . socket . getsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_IF ) <EOL> return socket . inet_ntoa ( struct . pack ( "<STR_LIT>" , i ) ) <EOL> def setOutgoingInterface ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> return self . reactor . resolve ( addr ) . addCallback ( self . _setInterface ) <EOL> def _setInterface ( self , addr ) : <EOL> i = socket . inet_aton ( addr ) <EOL> self . socket . setsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_IF , i ) <EOL> return <NUM_LIT:1> <EOL> def getLoopbackMode ( self ) : <EOL> return self . socket . getsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_LOOP ) <EOL> def setLoopbackMode ( self , mode ) : <EOL> mode = struct . pack ( "<STR_LIT:b>" , operator . truth ( mode ) ) <EOL> self . socket . setsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_LOOP , mode ) <EOL> def getTTL ( self ) : <EOL> return self . socket . getsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_TTL ) <EOL> def setTTL ( self , ttl ) : <EOL> ttl = struct . pack ( "<STR_LIT:B>" , ttl ) <EOL> self . socket . setsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_TTL , ttl ) <EOL> def joinGroup ( self , addr , interface = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return self . reactor . resolve ( addr ) . addCallback ( self . _joinAddr1 , interface , <NUM_LIT:1> ) <EOL> def _joinAddr1 ( self , addr , interface , join ) : <EOL> return self . reactor . resolve ( interface ) . addCallback ( self . _joinAddr2 , addr , join ) <EOL> def _joinAddr2 ( self , interface , addr , join ) : <EOL> addr = socket . inet_aton ( addr ) <EOL> interface = socket . inet_aton ( interface ) <EOL> if join : <EOL> cmd = socket . IP_ADD_MEMBERSHIP <EOL> else : <EOL> cmd = socket . IP_DROP_MEMBERSHIP <EOL> try : <EOL> self . socket . setsockopt ( socket . IPPROTO_IP , cmd , addr + interface ) <EOL> except socket . error , e : <EOL> return failure . Failure ( error . MulticastJoinError ( addr , interface , * e . args ) ) <EOL> def leaveGroup ( self , addr , interface = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return self . reactor . resolve ( addr ) . addCallback ( self . _joinAddr1 , interface , <NUM_LIT:0> ) <EOL> class MulticastPort ( MulticastMixin , Port ) : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . IMulticastTransport ) <EOL> def __init__ ( self , port , proto , interface = '<STR_LIT>' , maxPacketSize = <NUM_LIT> , reactor = None , listenMultiple = False ) : <EOL> """<STR_LIT>""" <EOL> Port . __init__ ( self , port , proto , interface , maxPacketSize , reactor ) <EOL> self . listenMultiple = listenMultiple <EOL> def createInternetSocket ( self ) : <EOL> skt = Port . createInternetSocket ( self ) <EOL> if self . listenMultiple : <EOL> skt . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> if hasattr ( socket , "<STR_LIT>" ) : <EOL> skt . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEPORT , <NUM_LIT:1> ) <EOL> return skt </s>
