示例#1
0
        private void MessageFactory(CommandEtherNetIPHeader header, byte[] bodyBytes)
        {
            int pointer = 0;

            Trace(EventType.Full, string.Format("{0} - Receive msg. '{1}' with status {2}", LOG_TAG, header.Command, header.Status));
            long headerSize = Marshal.SizeOf(typeof(CommandEtherNetIPHeader));

            switch (header.Command)
            {
            case EncapsulationCommands.ListServices:
            {
                if (bodyBytes.Length > 0)
                {
                    MsgListServiceReply msgReply = (MsgListServiceReply)MsgListServiceReply.Deserialize(typeof(MsgListServiceReply), bodyBytes, ref pointer);
                    if ((msgReply.CommandSpecificDataListServices.Items[0].CapabilityFlags & 0x20) == 0)
                    {
                        throw new Exception("The order side doesn't support TCP messages");
                    }
                    m_ClientConnStates = ClientConnStates.SendRegisterSession;
                }
                break;
            }

            case EncapsulationCommands.RegisterSession:
            {
                if (header.Status == 0 && header.SessionHandle != 0)
                {
                    MsgRegisterSessionReply msgReply = (MsgRegisterSessionReply)MsgRegisterSessionReply.Deserialize(typeof(MsgRegisterSessionReply), bodyBytes, ref pointer);
                    Trace(EventType.Info, string.Format("{0} - Registration session number: {1}", LOG_TAG, header.SessionHandle));
                    m_SessionHandle    = header.SessionHandle;
                    m_ClientConnStates = ClientConnStates.SendReceive;
                    OnRegistrationSession?.Invoke(m_SessionHandle);
                }
                else
                {
                    throw new Exception(string.Format("Error on registration session. Error: {0}", header.Status));
                }
                break;
            }

            case EncapsulationCommands.SendRRData:
            {
                MsgUnconnectedSendReply msgReply = (MsgUnconnectedSendReply)MsgUnconnectedSendReply.Deserialize(typeof(MsgUnconnectedSendReply), bodyBytes, ref pointer);
                long sendContext = BitConverter.ToInt64(header.SenderContext, 0);
                if (sendContext != 0 && sendContext == m_SenderContext)
                {
                    m_SendStatusResult = msgReply.CommonIndustrialProtocolReply.GeneralStatus;
                    m_SendResetEvent.Set();
                }
                break;
            }

            case EncapsulationCommands.UnRegisterSession:
            {
                break;
            }

            default:
            {
                throw new Exception(string.Format("Command {0} not implemented", header.Command));
            }
            }
        }
示例#2
0
        private void MessageFactory(CommandEtherNetIPHeader header, byte[] bodyBytes)
        {
            try
            {
                Trace(EventType.Full, string.Format("{0} - Receive msg. '{1}'", LOG_TAG, header.Command));
                long headerSize = Marshal.SizeOf(typeof(CommandEtherNetIPHeader));
                switch (header.Command)
                {
                case EncapsulationCommands.ListServices:
                {
                    if (bodyBytes.Length == 0)
                    {
                        MsgListServiceReply msg = new MsgListServiceReply();
                        msg.CommandSpecificDataListServices           = new CommandSpecificDataListServices();
                        msg.CommandSpecificDataListServices.ItemCount = 1;
                        msg.CommandSpecificDataListServices.Items     = new CommandSpecificDataListServicesItem[1];
                        msg.CommandSpecificDataListServices.Items[0]  = new CommandSpecificDataListServicesItem
                        {
                            TypeCode        = CommonPacketItemID.ListServicesResponse,
                            Version         = 1,
                            CapabilityFlags = Convert.ToUInt16("100100000", 2),
                            ServiceName     = Encoding.ASCII.GetBytes("Communications\0\0")
                        };
                        header.Length = msg.SizeOf();
                        msg.CommandSpecificDataListServices.Items[0].Length =
                            (ushort)(msg.CommandSpecificDataListServices.Items[0].SizeOf() - 2 - 2);
                        SendMessage(header, msg);
                    }
                    break;
                }

                case EncapsulationCommands.RegisterSession:
                {
                    int pointer = 0;
                    CommandSpecificDataRegisterSession cmdSpecData =
                        (CommandSpecificDataRegisterSession)CommandSpecificDataRegisterSession.Deserialize(
                            typeof(CommandSpecificDataRegisterSession), bodyBytes, ref pointer);
                    MsgRegisterSessionReply msg = new MsgRegisterSessionReply();
                    // TODO: check the protocol version to accept the registration
                    msg.CommandSpecificDataRegisterSession = cmdSpecData;
                    m_SessionHandle      = (uint)((DateTime.Now.Ticks / 10) & 0xFFFFFFFF);
                    header.SessionHandle = m_SessionHandle;
                    header.Length        = msg.SizeOf();
                    SendMessage(header, msg);
                    Trace(EventType.Info, string.Format("{0} - Registration session number: {1}", LOG_TAG, header.SessionHandle));
                    break;
                }

                case EncapsulationCommands.SendRRData:
                {
                    if (header.SessionHandle != m_SessionHandle)
                    {
                        throw new Exception(string.Format("Received invalid session handle (unregistred) in SendRRData message: ", header.SessionHandle));
                    }
                    int pointer = 0;
                    MsgUnconnectedSendRequest unconnSndReq = (MsgUnconnectedSendRequest)MsgUnconnectedSendRequest.Deserialize(
                        typeof(MsgUnconnectedSendRequest), bodyBytes, ref pointer);
                    string             symbol   = Encoding.ASCII.GetString(((DataPathSegmentANSISymb)unconnSndReq.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.PathSegmentList[0]).ANSISymbol);
                    ElementaryDataType dataType = unconnSndReq.CIPConnectionManagerUnconnSnd.CIPClassGeneric.DataType;
                    OnReceiveData?.Invoke(m_ScktConn.RemoteEndPoint, symbol.Replace("\0", ""), dataType, unconnSndReq.CIPConnectionManagerUnconnSnd.CIPClassGeneric.CIPClassGenericCmdSpecificData);
                    MsgUnconnectedSendReply msg = new MsgUnconnectedSendReply();
                    msg.CommandSpecificDataSendRRData = unconnSndReq.CommandSpecificDataSendRRData;
                    CommandSpecificDataSendRRDataItem item = msg.CommandSpecificDataSendRRData.List.First(
                        a => a.TypeID == CommonPacketItemID.UnconnectedMessage);
                    msg.CommonIndustrialProtocolReply = new CommonIndustrialProtocolReply {
                        Service              = 0xcd,
                        Reserved             = 0x0,
                        GeneralStatus        = 0x0, // <- success
                        AdditionalStatusSize = 0x0,
                        AdditionalStatus     = new ushort[0]
                    };
                    item.Length   = msg.CommonIndustrialProtocolReply.SizeOf();
                    header.Length = msg.SizeOf();
                    SendMessage(header, msg);
                    break;
                }

                case EncapsulationCommands.UnRegisterSession:
                {
                    break;
                }

                default:
                {
                    Trace(EventType.Error, string.Format("{0} - Command {1} not implemented", LOG_TAG, header.Command));
                    break;
                }
                }
            }
            catch (Exception e)
            {
                Trace(e);
            }
        }