# Path: opendiamond/helpers.py
# def daemonize():
# def signalname(signum):
# def murmur(data):
#     def __init__(self):
#     def __call__(self, service, address):
#     def __init__(self, error):
#     def __call__(self, service, address):
# def split_scheme(url):
# class _TcpWrappers(object):
# class _DummyTcpWrappers(object):
# 
# Path: opendiamond/protocol.py
# NONCE_LEN = 16
# 
# NULL_NONCE = b'\x00' * NONCE_LEN


from builtins import object
from weakref import WeakValueDictionary
from opendiamond.helpers import connection_ok
from opendiamond.protocol import NONCE_LEN, NULL_NONCE
import binascii
import errno
import logging
import os
import select
import socket
    '''A connection still in the matchmaking process, which is:
    1. Accept connection
    2. Read NONCE_LEN bytes, non-blockingly.
    3. If all zero, this is a control channel connection.  Generate NONCE_LEN
       bytes of nonce and send them back down the connection, then monitor
       the connection for closures so we can clean up our own state.
    4. If nonzero, this is a blast channel connection.  Look up the nonce to
       see which control channel it corresponds to.  If not found, close the
       connection.  Otherwise, we have a pairing; send the nonce back down
       the connection and start handling RPCs.'''

    def __init__(self, sock, peer):
        self.sock = sock
        self.sock.setblocking(0)
        self.peer = peer
        self.nonce = b''

    def read_nonce(self):
        '''Try to read the nonce.  Returns CONTROL if this is a control
        connection, DATA if a data connection, None if the caller should
        call back later.  The nonce is in self.nonce.'''
        if len(self.nonce) < NONCE_LEN:
            data = self.sock.recv(NONCE_LEN - len(self.nonce))
            if not data:
                raise _ConnectionClosed()
            self.nonce += data
            if len(self.nonce) == NONCE_LEN:
                # We're done with non-blocking mode
                self.sock.setblocking(1)
                if self.nonce == NULL_NONCE:

# Path: opendiamond/helpers.py
# def daemonize():
# def signalname(signum):
# def murmur(data):
#     def __init__(self):
#     def __call__(self, service, address):
#     def __init__(self, error):
#     def __call__(self, service, address):
# def split_scheme(url):
# class _TcpWrappers(object):
# class _DummyTcpWrappers(object):
# 
# Path: opendiamond/protocol.py
# NONCE_LEN = 16
# 
# NULL_NONCE = b'\x00' * NONCE_LEN


# Path: opendiamond/server/listen.py
from builtins import object
from weakref import WeakValueDictionary
from opendiamond.helpers import connection_ok
from opendiamond.protocol import NONCE_LEN, NULL_NONCE
import binascii
import errno
import logging
import os
import select
import socket

    '''A connection still in the matchmaking process, which is:
    1. Accept connection
    2. Read NONCE_LEN bytes, non-blockingly.
    3. If all zero, this is a control channel connection.  Generate NONCE_LEN
       bytes of nonce and send them back down the connection, then monitor
       the connection for closures so we can clean up our own state.
    4. If nonzero, this is a blast channel connection.  Look up the nonce to
       see which control channel it corresponds to.  If not found, close the
       connection.  Otherwise, we have a pairing; send the nonce back down
       the connection and start handling RPCs.'''

    def __init__(self, sock, peer):
        self.sock = sock
        self.sock.setblocking(0)
        self.peer = peer
        self.nonce = b''

    def read_nonce(self):
        '''Try to read the nonce.  Returns CONTROL if this is a control
        connection, DATA if a data connection, None if the caller should
        call back later.  The nonce is in self.nonce.'''
        if len(self.nonce) < NONCE_LEN:
            data = self.sock.recv(NONCE_LEN - len(self.nonce))
            if not data:
                raise _ConnectionClosed()
            self.nonce += data
            if len(self.nonce) == NONCE_LEN:
                # We're done with non-blocking mode
                self.sock.setblocking(1)
                if self.nonce == NULL_NONCE:

                    self.nonce = os.urandom(NONCE_LEN)