Ejemplo n.º 1
0
        private void StartGetMessage(long MaxMsgSize)
        {
            GetMessageThread = new Thread(() =>
            {
                EndPoint From = new IPEndPoint(IPAddress.Any, 0);
                Byte[] bs     = new byte[MaxMsgSize];
                int Blen      = 0;

                try
                {
                    while ((Blen = Server.ReceiveFrom(bs, ref From)) > 0)
                    {
                        MessageModel m = MessageModel.ToModel <MessageModel>(new MemoryStream(bs, 0, Blen).ToArray());
                        GetNewMessage?.Invoke(m, From);
                        bs = new byte[MaxMsgSize];
                    }
                }
                catch (Exception eee)
                {
                    if ((--MaxRestartCount) > 0)
                    {
                        StartGetMessage(MaxMsgSize);
                    }
                    else
                    {
                        ErrorCallBack?.Invoke(eee);
                    }
                }
            });
            GetMessageThread.Start();
        }
Ejemplo n.º 2
0
        public void StartGetMessage()
        {
            EndPoint FromIP = new IPEndPoint(0, 0);

            byte[] bs   = new byte[StaticResource.SocketMaxByte];
            int    blen = 0;

            (GetMessageThread = new Thread(() =>
            {
                try
                {
                    while ((blen = TheSocket.ReceiveFrom(bs, ref FromIP)) > 0)
                    {
                        try
                        {
                            byte[] bsbuf = new MemoryStream(bs, 0, blen).ToArray();
                            MessageBase msg = MessageBase.ToModel(bsbuf);
                            GetNewMessage?.Invoke(bsbuf, msg, FromIP);
                        }
                        catch (Exception error)
                        {
                            Debug.WriteLine("CDSocketUDP----" + error.Message);
                        }
                    }
                }
                catch (Exception error)
                {
                    //如果已经释放内存就不继续重新启动了
                    if (!IsDispose)
                    {
                        StartGetMessage();
                    }
                }
            })).Start();
        }