Beispiel #1
0
        public async void ReceiveData()
        {
            await Task.Run(async() =>
            {
                var reader = new DataReader(Socket.InputStream);

                try
                {
                    while (Working)
                    {
                        var sizeFieldCount = await reader.LoadAsync(sizeof(uint));

                        var stringLength       = reader.ReadUInt32();
                        var actualStringLength = await reader.LoadAsync(stringLength);
                        if (stringLength != actualStringLength || sizeFieldCount != sizeof(uint))
                        {
                            //数据接收中断开连接
                            reader.DetachStream();
                            OnStartFailed?.Invoke(new Exception("断开连接"));
                            Dispose();
                            return;
                        }
                        //接受数据
                        var dataArray = new byte[actualStringLength];
                        reader.ReadBytes(dataArray);
                        //转为json字符串
                        var dataJson = Encoding.UTF8.GetString(dataArray);
                        //反序列化为数据对象
                        var data = JsonConvert.DeserializeObject <MessageModel>(dataJson);
                        //新消息到达通知
                        MsgReceivedAction?.Invoke(data);
                    }
                }
                catch (Exception exception)
                {
                    if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                    {
                    }
                    Debug.WriteLine(string.Format("Received data: \"{0}\"",
                                                  "Read stream failed with error: " + exception.Message));
                    reader.DetachStream();
                    OnStartFailed?.Invoke(exception);
                    Dispose();
                }
            });
        }
Beispiel #2
0
        private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            Writer = null;
            ClientSockets.Add(args.Socket);

            var reader = new DataReader(args.Socket.InputStream);

            try
            {
                while (Working)
                {
                    var sizeFaildCount = await reader.LoadAsync(sizeof(uint));

                    var stringLength       = reader.ReadUInt32();
                    var actualStringLength = await reader.LoadAsync(stringLength);

                    if (sizeFaildCount != sizeof(uint) || actualStringLength != stringLength)
                    {
                        reader.DetachStream();
                        reader.Dispose();
                        ClientSockets?.Remove(args.Socket);
                        return;
                    }

                    var dataArrary = new byte[actualStringLength];
                    reader.ReadBytes(dataArrary);
                    var dataJson = Encoding.UTF8.GetString(dataArrary);
                    var data     = JsonConvert.DeserializeObject <MessageModel>(dataJson);
                    await SendMsg(data, args.Socket);

                    MsgReceivedAction?.Invoke(data);
                }
            }
            catch (Exception e)
            {
                if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown)
                {
                }
                Debug.WriteLine(string.Format("Received data: \"{0}\"", "Read stream failed with error: " + e.Message));
                reader.DetachStream();
                reader.Dispose();
                ClientSockets?.Remove(args.Socket);
            }
        }