Esempio n. 1
0
        public void Update()
        {
            //接受消息
            if (client.Available > 0)
            {
                NetworkSerializer.UnpackTCPMessage(client, out ushort cmd1, out ushort cmd2, out byte[] data);
                int key = NetworkSerializer.EnumToKey(cmd1, cmd2);

                if (events.ContainsKey(key))
                {
                    events[key](data);
                }
            }

            //发送消息
            while (messages.Count > 0)
            {
                byte[] data = messages.Dequeue();
                client.Send(data);
            }
        }
Esempio n. 2
0
        public void Update()
        {
            //异步接收客户端
            if (accept)
            {
                try
                {
                    acceptAsync = server.BeginAccept(null, null);
                    accept      = false;
                }
                catch (Exception) { }
            }
            if (acceptAsync.IsCompleted)
            {
                try
                {
                    Socket socket = server.EndAccept(acceptAsync);
                    clients.Add(new Client(true, socket));
                    OnConnected?.Invoke(socket);
                }
                catch (Exception) { }
                finally { accept = true; }
            }

            //异步接收消息
            foreach (Client client in clients)
            {
                if (!client.Connected) //不接受断开连接的消息
                {
                    continue;
                }
                Socket socket = client.Socket;

                if (socket.Available > 0)
                {
                    try
                    {
                        NetworkSerializer.UnpackTCPMessage(socket, out ushort cmd1, out ushort cmd2, out byte[] data);
                        int key = NetworkSerializer.EnumToKey(cmd1, cmd2);

                        if (events.ContainsKey(key))
                        {
                            events[key](socket, data);
                        }
                    }
                    catch (Exception ex)
                    {
                        socket.Close();
                        client.Connected = false;
                        OnDisconnected?.Invoke(ex.Message, socket);
                    }
                }
            }

            //异步发送消息
            while (messages.Count > 0)
            {
                Message message = messages.Dequeue();
                Socket  socket  = message.Socket;

                bool pass = false;
                foreach (Client client in clients)
                {
                    if (client.Socket == socket && client.Connected) //存在该客户端并且该客户端激活
                    {
                        pass = true;
                        break;
                    }
                }
                if (!pass)
                {
                    continue;
                }

                try
                {
                    message.Send();
                }
                catch (Exception ex)
                {
                    socket.Close();
                    //禁用该Socket
                    foreach (Client client in clients)
                    {
                        if (client.Socket == socket)
                        {
                            client.Connected = false;
                        }
                    }
                    OnDisconnected?.Invoke(ex.Message, socket);
                    break;
                }
            }
        }