Merge pull request #8 from hahahannes/master

combined multicast and unicast sockets to one socket
This commit is contained in:
Giacomo Tanganelli 2018-09-20 18:07:15 +02:00 committed by GitHub
commit ce5dbc7018
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -82,16 +82,10 @@ class CoAP(object):
# Join group # Join group
if addrinfo[0] == socket.AF_INET: # IPv4 if addrinfo[0] == socket.AF_INET: # IPv4
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Allow multiple copies of this program on one machine
# (not strictly needed)
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._socket.bind((defines.ALL_COAP_NODES, self.server_address[1])) self._socket.bind(('', self.server_address[1]))
mreq = struct.pack("4sl", socket.inet_aton(defines.ALL_COAP_NODES), socket.INADDR_ANY) mreq = struct.pack("4sl", socket.inet_aton(defines.ALL_COAP_NODES), socket.INADDR_ANY)
self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
self._unicast_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._unicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._unicast_socket.bind(self.server_address)
else: else:
self._socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self._socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
@ -107,7 +101,6 @@ class CoAP(object):
self._unicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self._unicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self._unicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._unicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._unicast_socket.bind(self.server_address) self._unicast_socket.bind(self.server_address)
else: else:
if addrinfo[0] == socket.AF_INET: # IPv4 if addrinfo[0] == socket.AF_INET: # IPv4
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@ -255,10 +248,7 @@ class CoAP(object):
logger.debug("send_datagram - " + str(message)) logger.debug("send_datagram - " + str(message))
serializer = Serializer() serializer = Serializer()
message = serializer.serialize(message) message = serializer.serialize(message)
if self.multicast: self._socket.sendto(message, (host, port))
self._unicast_socket.sendto(message, (host, port))
else:
self._socket.sendto(message, (host, port))
def add_resource(self, path, resource): def add_resource(self, path, resource):
""" """