コード例 #1
0
        private void OnImageMessageReceived(byte[] data)
        {
            var imageMessage = Serializer.Deserialize <ImageMessage>(data);

            ImageMessageReceived?.Invoke(this, imageMessage);
        }
コード例 #2
0
        private Task Receiver(NetworkStream stream, CancellationTokenSource cts)
        {
            return(new Task(() =>
            {
                Trace.TraceInformation("Receiver task starting...");
                //stream.ReadTimeout = 5000;
                try
                {
                    while (true)
                    {
                        if (cts.Token.IsCancellationRequested)
                        {
                            break;
                        }
                        DataPackage dataPackage = DataPackage.FromStream(stream);
                        switch (dataPackage.Type)
                        {
                        case MessageType.SYSTEM_LOGIN_OK:
                            {
                                LoginInfo info = Serializer.Deserialize <LoginInfo>(dataPackage.Data);
                                Name = info.Name;
                                LoginOK?.Invoke(this, info);
                            }
                            break;

                        case MessageType.SYSTEM_LOGIN_FAILED:
                            {
                                LoginInfo info = Serializer.Deserialize <LoginInfo>(dataPackage.Data);
                                LoginFailed?.Invoke(this, info);
                            }
                            break;

                        case MessageType.SYSTEM_JOIN_ROOM_OK:
                            {
                                RoomInfo roomInfo = Serializer.Deserialize <RoomInfo>(dataPackage.Data);
                                JoinedInRoom?.Invoke(this, roomInfo);
                            }
                            break;

                        case MessageType.SYSTEM_LEAVE_ROOM_OK:
                            {
                                RoomInfo roomInfo = Serializer.Deserialize <RoomInfo>(dataPackage.Data);
                                LeavedFromRoom?.Invoke(this, roomInfo);
                            }
                            break;

                        case MessageType.CLIENT_MESSAGE:
                            {
                                Message message = Serializer.Deserialize <Message>(dataPackage.Data);
                                MessageReceived?.Invoke(this, message);
                            }
                            break;

                        case MessageType.CLIENT_IMAGE_MESSAGE:
                            {
                                var imageMessage = Serializer.Deserialize <ImageMessage>(dataPackage.Data);
                                ImageMessageReceived?.Invoke(this, imageMessage);
                            }
                            break;

                        case MessageType.CLIENT_FILE_MESSAGE:
                            {
                                var fileMessage = Serializer.Deserialize <FileMessage>(dataPackage.Data);
                                FileMessageReived?.Invoke(this, fileMessage);
                            }
                            break;

                        case MessageType.SYSTEM_FILE_TRANSFER:
                            {
                                var fileMessage = Serializer.Deserialize <FileMessage>(dataPackage.Data);
                                FileReived?.Invoke(this, fileMessage);
                            }
                            break;

                        case MessageType.SYSTEM_MESSAGE_OK:
                            {
                                Receipt receipt = Serializer.Deserialize <Receipt>(dataPackage.Data);
                                MessageArrivied?.Invoke(this, receipt);
                            }
                            break;

                        case MessageType.SYSTEM_MESSAGE:
                            {
                                var message = Encoding.UTF8.GetString(dataPackage.Data);
                                SystemMessageReceived?.Invoke(this, message);
                                break;
                            }

                        case MessageType.CLIENT_LOGOUT:
                            {
                                Name = null;
                                LogoutOK?.Invoke(this, null);
                                goto default;
                            }

                        default:
                            UnknownMessageReceived?.Invoke(this, new SystemMessageEventArgs(dataPackage.Type, dataPackage.Data));
                            break;
                        }
                    }
                }
                catch (EndOfStreamException ex)
                {
                    Debug.WriteLine($"Client {Name} Receiver: {ex.Message}");
                }
                catch (IOException ex)
                {
                    if (ex.InnerException is SocketException)
                    {
                        SocketExceptionRaising?.Invoke(this, (SocketException)ex.InnerException);
                    }
                    else
                    {
                        Trace.TraceInformation($"{ex}");
                    }
                }
                ReceiverTaskExited?.Invoke(this, null);
                Trace.TraceInformation("Receiver task exited");
            }, TaskCreationOptions.LongRunning));
        }