コード例 #1
0
        /// <summary>
        /// 序列化
        /// </summary>
        public void SerializeClass(TcpClient _client, Message03 _sendmsg)
        {
            MemoryStream ms = new MemoryStream();

            s_bf.Serialize(ms, _sendmsg);
            byte[] buffer = ms.ToArray();
            _client.GetStream().Write(buffer, 0, buffer.Length);
        }
コード例 #2
0
        /// <summary>
        /// 反序列化
        /// </summary>
        public Message03 DeserializeClass(TcpClient _client)
        {
            NetworkStream clientStream = _client.GetStream();

            byte[] buffer = new byte[_client.Available];
            clientStream.Read(buffer, 0, buffer.Length);

            MemoryStream ms     = new MemoryStream(buffer);
            Message03    result = (Message03)s_bf.Deserialize(ms);

            return(result);
        }
コード例 #3
0
ファイル: Tranmitter.cs プロジェクト: jasonben677/ChatRoom
        private void HandleReceiveMessage()
        {
            mMessage = serialManager.DeserializeClass(mClient);
            int num = mMessage.msgType;

            if (num == -1)
            {
                Console.WriteLine("error");
            }
            else
            {
                mDelegates[num]?.DynamicInvoke(this, mMessage);
            }
        }
コード例 #4
0
    public bool Connect(string _address, int _port)
    {
        tranmitter = new Tranmitter(new TcpClient());

        if (tranmitter.Connect(_address, _port))
        {
            clientMessage = tranmitter.mMessage;
            return(true);
        }
        else
        {
            clientMessage = null;
            return(false);
        }
    }
コード例 #5
0
        public void TestTopic()
        {
            var  exchangeName = "test.topic";
            byte queueCount   = 4;

            ITopicSender publisher       = new TopicSender(_conn, exchangeName, queueCount, confirmEnabled: true, debugEnabled: true);
            var          sentMessageDict = new ConcurrentDictionary <string, Tuple <bool, IMessageTransportationContext> >();

            publisher.OnMessageSent += (sender, e) =>
            {
                sentMessageDict.TryAdd(e.Context.GetMessageId(), new Tuple <bool, IMessageTransportationContext>(false, e.Context));
            };
            publisher.OnMessageSendingSucceeded += (sender, e) =>
            {
                sentMessageDict.AddOrUpdate(e.Context.GetMessageId(), new Tuple <bool, IMessageTransportationContext>(true, e.Context), (key, originVal) => new Tuple <bool, IMessageTransportationContext>(true, originVal.Item2));
            };
            int sendSequence = 0;

            while (true)
            {
                try
                {
                    sendSequence++;
                    if (sendSequence % 3 == 0)
                    {
                        var message = new Message01(new Guid("58437EDC-87B7-4995-A5C0-BB5FD0FE49E0"))
                        {
                            Sequence = random.Next(1, 20)
                        };
                        Envelope envelopedMessage = Envelope.Create(message, $"{message.HostingFilialeId}_{Guid.NewGuid()}");
                        Task.Run(() => publisher.SendMessage(envelopedMessage));
                    }
                    else if (sendSequence % 3 == 1)
                    {
                        var message = new Message02(new Guid("7AE62AF0-EB1F-49C6-8FD1-128D77C84698"));
                        Task.Run(() => publisher.SendMessage(Envelope.Create(message, $"{message.SaleFilialeId}")));
                    }
                    else
                    {
                        var message = new Message03(new Guid("4AE62AF0-EB1F-49C6-8FD1-128D77C84698"));
                        Task.Run(() => publisher.SendMessage(Envelope.Create(message, $"{message.SaleFilialeId}")));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.GetType().FullName}");
                }
                if (sendSequence == 10000000)
                {
                    Thread.Sleep(3000);
                    Task.Run(() =>
                    {
                        publisher.Dispose();
                    });
                    break;
                }
            }
            var timeout        = sendSequence / 10;
            var executeSeconds = 0;

            while (sentMessageDict.Count != sendSequence || sentMessageDict.Values.Any(w => !w.Item1 && w.Item2.LastException == null))
            {
                if (executeSeconds > timeout)
                {
                    break;
                }
                Thread.Sleep(1000);
                executeSeconds++;
            }
            var failList = sentMessageDict.Values.Where(w => !w.Item1).ToList();
        }
コード例 #6
0
 public void Handle(Message03 message, IMessageTransportationContext context)
 {
     Thread.Sleep(700);
     Console.WriteLine($"Message03 from queue {context.QueueName}");
 }
コード例 #7
0
ファイル: Tranmitter.cs プロジェクト: jasonben677/ChatRoom
 public Tranmitter(TcpClient _client = null)
 {
     mClient  = _client;
     mMessage = new Message03();
 }