Merge pull request #8 from hahahannes/master
combined multicast and unicast sockets to one socket
This commit is contained in:
commit
ce5dbc7018
1 changed files with 2 additions and 12 deletions
|
@ -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,9 +248,6 @@ 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._unicast_socket.sendto(message, (host, port))
|
|
||||||
else:
|
|
||||||
self._socket.sendto(message, (host, port))
|
self._socket.sendto(message, (host, port))
|
||||||
|
|
||||||
def add_resource(self, path, resource):
|
def add_resource(self, path, resource):
|
||||||
|
|
Loading…
Reference in a new issue