Exemple #1
0
        /// <summary>
        /// 将消息加入到消息队列中等待发送
        /// </summary>
        //public static void Enqueue(byte[] data)
        //{
        //    // 发第一个ack的时候连接未建立且状态没有
        //    if (curState[0] == ClientState.Connected)
        //    {
        //        _messages.Enqueue(data);
        //    }
        //}

        public static void Enqueue(NetworkData data)
        {
            if (curState.Count == 0)
            {
                return;
            }
            if (curState[0] == ClientState.Connected)
            {
                _messages_to_send.Enqueue(data);
            }
        }
Exemple #2
0
        public static bool Received        = true;           //收到心跳包回信

        public static void Generate_Package(Message message)
        {
            short   seq     = (short)safe_seqs[seq_num.sseq];
            Package package = PackageUtils.MessageToPackage(message, seq);

            //Console.WriteLine("发送的包序号为" + seq);
            byte[]      byte_data = ByteUtils.getBytes(package);
            NetworkData data      = new NetworkData();

            data.data = byte_data;
            data.seq  = seq;
            data.type = message.Type;
            Enqueue(data);
            // 发送一个包之后sseq直接+1
            safe_seqs[seq_num.sseq]++;
            //return package;
        }
Exemple #3
0
 public static void _Send()
 {
     Console.WriteLine("消息发送处理线程已经开启...");
     while (true)
     {
         if (_messages_to_send.Count > 0)
         {
             NetworkData pack;
             _messages_to_send.TryPeek(out pack);
             Console.WriteLine("pack seq:" + pack.seq);
             Console.WriteLine("sack:" + safe_seqs[seq_num.sack]);
             if (pack.seq > safe_seqs[seq_num.sack] && pack.data.Length != 4)
             {
                 Console.WriteLine("pack seq:" + pack.seq);
                 Console.WriteLine("sack:" + safe_seqs[seq_num.sack]);
                 Console.WriteLine("数据类型:" + pack.type.ToString());
                 try
                 {
                     Thread.Sleep(2000);
                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e.ToString());
                 }
                 if (pack.seq > safe_seqs[seq_num.sack] && pack.data.Length != 4)
                 {
                     Console.WriteLine("已经超时,重新发送上一个包,包的类型为" + temp_package.type.ToString() +
                                       "包的序号为" + temp_package.seq.ToString());
                     _client_socket.SendTo(temp_package.data, _remote_ep);
                     continue;
                 }
             }
             _ = _messages_to_send.TryDequeue(out _);
             Console.WriteLine("发送包的序号为" + pack.seq + ",类别为" + pack.type.ToString());
             try
             {
                 _client_socket.SendTo(pack.data, _remote_ep);
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
             }
             if (pack.data.Length != 4)
             {
                 temp_package = pack;
             }
         }
         //if (_messages.Count > 0)
         //{
         //    byte[] data = _messages.Dequeue();
         //    try
         //    {
         //        _client_socket.SendTo(data, _remote_ep);
         //    }
         //    catch(Exception e)
         //    {
         //        Console.WriteLine(e.Message);
         //    }
         //    //Console.WriteLine("已经发送一条消息。");
         //}
         try
         {
             Thread.Sleep(10);
         }
         catch (Exception e)
         {
             Console.WriteLine(e.ToString());
         }
     }
 }