Beispiel #1
0
        private byte[] GetAmsMessage(byte[] tcpHeader)
        {
            uint responseLength = AmsHeaderHelper.GetResponseLength(tcpHeader);

            byte[] response = new byte[responseLength];
            GetMessage(response);
            return(response);
        }
Beispiel #2
0
        //This is different thread!
        private void ReadCallback(object sender, AmsSocketResponseArgs args)
        {
            if (args.Error != null)
            {
                // Lock the list.
                lock (pendingResultsLock)
                {
                    if (pendingResults.Count > 0)
                    {
                        foreach (var adsCommandResult in pendingResults.ToList())
                        {
                            adsCommandResult.UnknownException = args.Error;
                            adsCommandResult.Callback.Invoke(adsCommandResult);
                        }
                    }
                    else
                    {
                        throw args.Error;
                    }
                }
            }

            if ((args.Response != null) && (args.Response.Length > 0) && (args.Error == null))
            {
                uint amsErrorCode   = AmsHeaderHelper.GetErrorCode(args.Response);
                uint invokeId       = AmsHeaderHelper.GetInvokeId(args.Response);
                bool isNotification = (AmsHeaderHelper.GetCommandId(args.Response) == AdsCommandId.DeviceNotification);

                if (AmsPortTarget != AmsHeaderHelper.GetAmsPortSource(args.Response))
                {
                    return;
                }
                if (!AmsNetIdTarget.Bytes.SequenceEqual(AmsHeaderHelper.GetAmsNetIdSource(args.Response)))
                {
                    return;
                }

                //If notification then just start the callback
                if (isNotification && (OnNotification != null))
                {
                    var notifications = AdsNotification.GetNotifications(args.Response);
                    foreach (var notification in notifications)
                    {
                        var notificationRequest = NotificationRequests.FirstOrDefault(n => n.NotificationHandle == notification.NotificationHandle);
                        if (notificationRequest != null)
                        {
                            notificationRequest.ByteValue = notification.ByteValue;

                            if ((args.Context != null) && (RunNotificationsOnMainThread))
                            {
                                args.Context.Post(
                                    new SendOrPostCallback(delegate
                                {
                                    OnNotification(null, new AdsNotificationArgs(notificationRequest));
                                }), null);
                            }
                            else
                            {
                                OnNotification(null, new AdsNotificationArgs(notificationRequest));
                            }
                        }
                    }
                }

                //If not a notification then find the original command and call async callback
                if (!isNotification)
                {
                    AdsCommandResponse adsCommandResult = null;

                    // Do some locking before fiddling with the list.
                    lock (pendingResultsLock)
                    {
                        adsCommandResult = pendingResults.FirstOrDefault(r => r.CommandInvokeId == invokeId);
                        if (adsCommandResult == null)
                        {
                            return;
                        }
                        //throw new AdsException("I received a response from a request I didn't send?");

                        pendingResults.Remove(adsCommandResult);
                    }

                    if (amsErrorCode > 0)
                    {
                        adsCommandResult.AmsErrorCode = amsErrorCode;
                    }
                    else
                    {
                        adsCommandResult.SetResponse(args.Response);
                    }
                    adsCommandResult.Callback.Invoke(adsCommandResult);
                }
            }
        }