示例#1
0
文件: FormGame.cs 项目: lacti/Lz
        private async void PacketPump()
        {
            try
            {
                _session.Connect(_host, _port);
                _connected = true;
                while (true)
                {
                    var packet = await _session.Receive();

                    Invoke(
                        new MethodInvoker(
                            () => _dispatcher.Dispatch(packet, _session)));
                }
            }
            catch
            {
            }
            _connected = false;
        }
示例#2
0
文件: Listener.cs 项目: lacti/Lz
        private async void ProcessSocket(Socket socket)
        {
            Logger.Write("Connected from: " + socket.RemoteEndPoint);

            var session = new PacketSession(socket);

            _lock.DoWriteLock(() => _sessions.Add(session));
            Dispatch(new ConnectionPacket {
                Connected = true
            }, session);
            try
            {
                while (true)
                {
                    var packet = await session.Receive();

                    Dispatch(packet, session);
                }
            }
            catch (Exception e)
            {
                Logger.Write(e);
            }

            Dispatch(new ConnectionPacket {
                Connected = false
            }, session);
            _lock.DoWriteLock(() => _sessions.Remove(session));

            try
            {
                socket.Shutdown(SocketShutdown.Both);
            }
            catch
            {
            }
        }