コード例 #1
0
ファイル: NetworkServer.cs プロジェクト: yjn88/Destroy
        /// <summary>
        /// 更新(接受, 读取, 发送)
        /// </summary>
        public void Update()
        {
            //异步接收客户端
            if (accept)
            {
                try
                {
                    acceptAsync = server.BeginAccept(null, null);
                    accept      = false;
                }
                catch (Exception)
                {
                }
            }
            if (acceptAsync.IsCompleted)
            {
                try
                {
                    Socket socket = server.EndAccept(acceptAsync);
                    Client client = new Client(true, socket);
                    clients.Add(client);
                    OnConnected?.Invoke(client);
                }
                catch (Exception)
                {
                }
                finally
                {
                    accept = true;
                }
            }
            //异步接收消息
            foreach (Client client in clients)
            {
                try
                {
                    while (client.Socket.Available > 0)
                    {
                        NetworkUtils.UnpackTCPMessage(client.Socket,
                                                      out ushort cmd1, out ushort cmd2, out byte[] data);
                        uint key = NetworkUtils.EnumToKey(cmd1, cmd2);
                        if (callbackDict.ContainsKey(key))
                        {
                            //执行回调方法
                            callbackDict[key](client, data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    RemoveClient(client, ex.Message);
                }
            }
            List <Client> deleteClients = new List <Client>();

            //异步发送消息
            while (messagesToBeSend.Count > 0)
            {
                ClientMessage message = messagesToBeSend.Dequeue();
                Client        client  = message.Client;
                if (!client.Connected)
                {
                    deleteClients.Add(client);
                }
                else
                {
                    try
                    {
                        client.Socket.Send(message.Data);
                    }
                    catch (Exception ex)
                    {
                        RemoveClient(client, ex.Message);
                        deleteClients.Add(client);
                    }
                }
            }
            foreach (Client item in deleteClients)
            {
                clients.Remove(item);
            }
        }
コード例 #2
0
 /// <summary>
 /// 发送
 /// </summary>
 /// <param name="ip">IP地址</param>
 /// <param name="port">端口号</param>
 /// <param name="cmd1">指令1</param>
 /// <param name="cmd2">指令2</param>
 /// <param name="data">数据</param>
 public void Send(string ip, int port, ushort cmd1, ushort cmd2, byte[] data)
 {
     byte[] packData = NetworkUtils.PackUDPMessage(cmd1, cmd2, data);
     messagesToBeSend.Enqueue(new UdpMessage(new IPEndPoint(IPAddress.Parse(ip), port), packData));
 }
コード例 #3
0
ファイル: NetworkServer.cs プロジェクト: yjn88/Destroy
 /// <summary>
 /// 向客户端发送数据
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="cmd1">指令1</param>
 /// <param name="cmd2">指令2</param>
 /// <param name="data">数据</param>
 public void Send(Client client, ushort cmd1, ushort cmd2, byte[] data)
 {
     byte[] packData = NetworkUtils.PackTCPMessage(cmd1, cmd2, data);
     messagesToBeSend.Enqueue(new ClientMessage(client, packData));
 }
コード例 #4
0
ファイル: NetworkClient.cs プロジェクト: yjn88/Destroy
 /// <summary>
 /// 向服务器发送数据
 /// </summary>
 /// <param name="cmd1">指令1</param>
 /// <param name="cmd2">指令2</param>
 /// <param name="data">数据</param>
 public void Send(ushort cmd1, ushort cmd2, byte[] data)
 {
     byte[] packData = NetworkUtils.PackTCPMessage(cmd1, cmd2, data);
     messagesToBeSend.Enqueue(packData);
 }