當前位置: 首頁>>代碼示例>>Python>>正文


Python Listener.broken_conn方法代碼示例

本文整理匯總了Python中listener.Listener.broken_conn方法的典型用法代碼示例。如果您正苦於以下問題:Python Listener.broken_conn方法的具體用法?Python Listener.broken_conn怎麽用?Python Listener.broken_conn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在listener.Listener的用法示例。


在下文中一共展示了Listener.broken_conn方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: VpnServer

# 需要導入模塊: from listener import Listener [as 別名]
# 或者: from listener.Listener import broken_conn [as 別名]
class VpnServer(object):

    def __init__(self, port, shared_key, connected_callback, broken_conn_callback, 
                 debug_continue, debug, app):
        self.port = port
        self.shared_key = shared_key
        self.connected_callback = connected_callback
        self.broken_conn_callback = broken_conn_callback
        self.send_queue = Queue()
        self.receive_queue = Queue()
        self.authenticated = False
        self.debug_continue = debug_continue
        self.debug = debug
        self.waiting = True
        self.sender = None
        self.receiver = None
        self.is_server = True
        self.sessionkey=''
        self.app = app

    def setup(self):
        try:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error:
            return (-1, "Could not create socket")

        try:
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.socket.bind(('', self.port))
            Logger.log("Listening for connections...", self.is_server)
            self.socket.listen(1) 
        except socket.error:
            return (-1, "Could not bind socket to port " + str(self.port))

        return (0, "VPN server set to listen on port " + str(self.port))

    def send(self, msg):
        if (self.authenticated):
            Logger.log("sessionkey: " +self.sessionkey, self.is_server)
            emsg = self.auth.encrypt_message(msg, self.auth.get_sessionkey())
            self.send_queue.put(emsg)
            Logger.log("Put message on send queue: "+ msg, self.is_server)
        else:
            self.send_queue.put(msg)
            Logger.log("Put message on send queue: "+ msg, self.is_server)
    
    def receive(self):
        if not self.receive_queue.empty():
            msg = self.receive_queue.get()
            if (self.authenticated):
                msg, valid = self.auth.decrypt_message(msg, self.auth.get_sessionkey())

                if valid is False:
                    return None

                Logger.log("Decrypted msg: "+ msg, self.is_server)
            return msg
        else:
            return None

    def start(self, callback=None):
        self.listener = Listener(self.socket, self.shared_key, self, self.connected_callback, self.app)
        self.listener.start()

    def bind(self, client_socket):
        self.debug_continue.disabled = self.debug
        self.sender = Sender(client_socket, self.send_queue, self)
        self.receiver = Receiver(client_socket, self.receive_queue, self)
        self.sender.start()
        self.receiver.start()
    
    def clear_queues(self):
        self.receive_queue.queue.clear()
        self.send_queue.queue.clear()

    def broken_conn(self):
        Logger.log("Broken connection", self.is_server)
        self.send_queue.queue.clear()
        self.receive_queue.queue.clear()
        self.sender.close()
        self.receiver.close()
        self.waiting = True
        self.authenticated = False
        if (self.listener):
            self.listener.broken_conn()

    def close(self):
        Logger.log("Connection closing", self.is_server)
        self.send_queue.queue.clear()
        self.receive_queue.queue.clear()
        self.listener.close()
        self.socket.close()
        self.authenticated = False
        if self.sender:
            self.sender.close()
        if self.receiver:
            self.receiver.close()
開發者ID:EmilMaric,項目名稱:CPEN442-VPN,代碼行數:99,代碼來源:server.py


注:本文中的listener.Listener.broken_conn方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。