Пример #1
0
 internal void OnAmsReceive(IntPtr pAmsCmd)
 {
     if (pAmsCmd != IntPtr.Zero)
     {
         Marshal.Copy(pAmsCmd, this.hdrBuffer, 0, TcAmsHeaderSize.TCAMSHEADER);
         TcAmsHeader amsHeader   = new TcAmsHeader(this.hdrBuffer);
         IntPtr      source      = new IntPtr(pAmsCmd.ToInt64() + TcAmsHeaderSize.TCAMSHEADER);
         byte[]      destination = new byte[amsHeader.CbData];
         Marshal.Copy(source, destination, 0, (int)amsHeader.CbData);
         TcAmsCommand item = new TcAmsCommand(amsHeader, destination);
         if (item.AmsHeader.CommandId == 8)
         {
             object syncRoot = this._receiveQueueNotify.SyncRoot;
             lock (syncRoot)
             {
                 this._receiveQueueNotify.Enqueue(item);
             }
             this._queueEventNotify.Set();
         }
         else
         {
             object syncRoot = this._receiveQueue.SyncRoot;
             lock (syncRoot)
             {
                 this._receiveQueue.Enqueue(item);
             }
             this._queueEvent.Set();
         }
     }
 }
Пример #2
0
        internal void callReceiveHandler(TcAmsCommand amsCmd)
        {
            TcAmsServerReceiveEventHandler handler = this._amsServerReceiveDelegate;

            if (handler != null)
            {
                handler(amsCmd);
            }
        }
Пример #3
0
        internal unsafe AdsErrorCode Send(TcAmsCommand amsCmd)
        {
            AdsErrorCode noError   = AdsErrorCode.NoError;
            TcAmsHeader  amsHeader = amsCmd.AmsHeader;
            IntPtr       ptr       = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(TcMarshallableAmsHeader)) + ((int)amsHeader.CbData)));

            Marshal.StructureToPtr(amsCmd.AmsHeader.GetMarshallableHeader(), ptr, false);
            if (amsHeader.CbData > 0)
            {
                IntPtr destination = new IntPtr(ptr.ToInt64() + Marshal.SizeOf(typeof(TcMarshallableAmsHeader)));
                Marshal.Copy(amsCmd.Data, 0, destination, (int)amsHeader.CbData);
            }
            void *pAmsCmd = ref ptr.ToPointer();

            noError = (AdsErrorCode)UnsafeNativeMethods.AmsSend(this._server.Port, pAmsCmd);
            Marshal.FreeHGlobal(ptr);
            return(noError);
        }
Пример #4
0
 internal AdsErrorCode Send(TcAmsCommand amsCmd) =>
 this._serverMarshaller.Send(amsCmd);
Пример #5
0
        private void OnAmsReceive(TcAmsCommand amsCmd)
        {
            byte[] data            = amsCmd.Data;
            uint   indexGroup      = 0;
            uint   cbLength        = 0;
            uint   cbWriteLength   = 0;
            uint   numStampHeaders = 0;

            if (amsCmd.AmsHeader.StateFlags != 4)
            {
                if (amsCmd.AmsHeader.StateFlags == 5)
                {
                    AdsErrorCode code;
                    switch (amsCmd.AmsHeader.CommandId)
                    {
                    case 1:
                    {
                        AdsVersion version = new AdsVersion();
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsReadDeviceInfoCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode, null, version);
                            return;
                        }
                        code             = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        version.Version  = data[4];
                        version.Revision = data[5];
                        version.Build    = TcAdsParser.ReadUShort(data, 6);
                        this.AdsReadDeviceInfoCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code, TcAdsParser.ReadString(data, 8, 0x10), version);
                        return;
                    }

                    case 2:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsReadCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode, 0, null);
                            return;
                        }
                        code     = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        cbLength = TcAdsParser.ReadUInt(data, 4);
                        this.AdsReadCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code, cbLength, TcAdsParser.AdsData(data, 8, cbLength));
                        return;

                    case 3:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsWriteCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode);
                            return;
                        }
                        code = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        this.AdsWriteCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code);
                        return;

                    case 4:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsReadStateCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode, AdsState.Invalid, 0);
                            return;
                        }
                        code = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        this.AdsReadStateCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code, (AdsState)((short)TcAdsParser.ReadUShort(data, 4)), TcAdsParser.ReadUShort(data, 6));
                        return;

                    case 5:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsWriteControlCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode);
                            return;
                        }
                        code = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        this.AdsWriteControlCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code);
                        return;

                    case 6:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsAddDeviceNotificationCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode, 0);
                            return;
                        }
                        code = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        this.AdsAddDeviceNotificationCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code, TcAdsParser.ReadUInt(data, 4));
                        return;

                    case 7:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsDelDeviceNotificationCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode);
                            return;
                        }
                        code = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        this.AdsDelDeviceNotificationCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code);
                        return;

                    case 8:
                        break;

                    case 9:
                        if (amsCmd.AmsHeader.ErrCode != 0)
                        {
                            this.AdsReadWriteCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, (AdsErrorCode)amsCmd.AmsHeader.ErrCode, 0, null);
                            break;
                        }
                        code     = (AdsErrorCode)TcAdsParser.ReadUInt(data, 0);
                        cbLength = TcAdsParser.ReadUInt(data, 4);
                        this.AdsReadWriteCon(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, code, cbLength, TcAdsParser.AdsData(data, 8, cbLength));
                        return;

                    default:
                        return;
                    }
                }
            }
            else
            {
                switch (amsCmd.AmsHeader.CommandId)
                {
                case 1:
                    this.AdsReadDeviceInfoInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser);
                    return;

                case 2:
                    indexGroup = TcAdsParser.ReadUInt(data, 0);
                    this.AdsReadInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, indexGroup, TcAdsParser.ReadUInt(data, 4), TcAdsParser.DataLength(data, 8));
                    return;

                case 3:
                    indexGroup = TcAdsParser.ReadUInt(data, 0);
                    cbLength   = TcAdsParser.DataLength(data, 8);
                    this.AdsWriteInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, indexGroup, TcAdsParser.ReadUInt(data, 4), cbLength, TcAdsParser.AdsData(data, 12, cbLength));
                    return;

                case 4:
                    this.AdsReadStateInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser);
                    return;

                case 5:
                {
                    AdsState adsState = TcAdsParser.AdsState(data);
                    cbLength = TcAdsParser.DataLength(data, 4);
                    this.AdsWriteControlInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, adsState, TcAdsParser.ReadUShort(data, 2), cbLength, TcAdsParser.AdsData(data, 8, cbLength));
                    return;
                }

                case 6:
                    indexGroup = TcAdsParser.ReadUInt(data, 0);
                    this.AdsAddDeviceNotificationInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, indexGroup, TcAdsParser.ReadUInt(data, 4), TcAdsParser.DataLength(data, 8), (AdsTransMode)TcAdsParser.ReadUInt(data, 12), TcAdsParser.ReadUInt(data, 0x10), TcAdsParser.ReadUInt(data, 20));
                    return;

                case 7:
                {
                    uint hNotification = TcAdsParser.ReadUInt(data, 0);
                    this.AdsDelDeviceNotificationInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, hNotification);
                    return;
                }

                case 8:
                    if (!this._useSingleNotificationEventHandler)
                    {
                        goto TR_0008;
                    }
                    else
                    {
                        using (AdsBinaryReader reader = new AdsBinaryReader(new AdsStream(data)))
                        {
                            cbLength        = reader.ReadUInt32();
                            numStampHeaders = reader.ReadUInt32();
                            this.AdsDeviceNotificationInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, numStampHeaders, reader);
                            break;
                        }
                        goto TR_0008;
                    }
                    break;

                case 9:
                    indexGroup    = TcAdsParser.ReadUInt(data, 0);
                    cbWriteLength = TcAdsParser.ReadUInt(data, 12);
                    this.AdsReadWriteInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, indexGroup, TcAdsParser.ReadUInt(data, 4), TcAdsParser.ReadUInt(data, 8), cbWriteLength, TcAdsParser.AdsData(data, 0x10, cbWriteLength));
                    return;

                default:
                    return;
                }
            }
            return;

TR_0008:
            cbLength        = TcAdsParser.ReadUInt(data, 0);
            numStampHeaders = TcAdsParser.ReadUInt(data, 4);
            this.AdsDeviceNotificationInd(amsCmd.AmsHeader.Sender, amsCmd.AmsHeader.HUser, numStampHeaders, TcAdsParser.ReadNotificationStampHeaders(data, numStampHeaders));
        }