Beispiel #1
0
 private void DecoderOnMessageDecoded(object sender, MessageDecodedEventArgs eventArgs)
 {
     if (eventArgs.Message != null)
     {
         try
         {
             if (eventArgs.Message is DataFrame dataFrame && dataFrame.Items != null)
             {
                 DataPointValue[] files = dataFrame.Items.Where(i => i is OPCUAFile)
                                          .ToArray();
                 foreach (OPCUAFile file in files)
                 {
                     dataFrame.Items.Remove(file);
                     FileReceived?.Invoke(this, new FileReceivedEventArgs(file, eventArgs.Topic, dataFrame.NetworkMessageHeader.PublisherID.Value));
                 }
                 if (dataFrame.Items.Count > 0)
                 {
                     MessageReceived?.Invoke(this, eventArgs);
                 }
             }
             else if (eventArgs.Message is MetaFrame metaFrame)
             {
                 MetaMessageReceived?.Invoke(this, eventArgs);
             }
             else
             {
                 UnknownMessageReceived?.Invoke(this, eventArgs);
             }
         }
Beispiel #2
0
        private void WebSocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            ICommandResponse response;

            if (TryGetCommandResponse(e.Message, out response))
            {
                HandleResponse(response);
                return;
            }
            IEvent evnt;

            if (TryGetEvent(e.Message, out evnt))
            {
                HandleEvent(evnt);
                return;
            }
            UnknownMessageReceived?.Invoke(e.Message);
        }
Beispiel #3
0
 protected virtual void OnUnknownMessageReceived(string data)
 {
     UnknownMessageReceived?.Invoke(this, data);
 }
Beispiel #4
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));
        }