当前位置: 首页>>代码示例>>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;未经允许,请勿转载。