private void SocketClient_OnDisconnect(SocketConn scktConn) { m_ClientConnStates = ClientConnStates.Disconnected; Trace(EventType.Info, string.Format("{0} - Connection is closed: {1}", LOG_TAG, scktConn.RemoteEndPoint)); OnConnStatusChanged?.Invoke(ConnType.Send, false, scktConn.ConnID); }
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)); } } }
private void SocketClient_OnConnect(SocketConn scktConn) { m_ClientConnStates = ClientConnStates.SendListServices; Trace(EventType.Info, string.Format("{0} - Connection established: {1}", LOG_TAG, scktConn.RemoteEndPoint)); OnConnStatusChanged?.Invoke(ConnType.Send, true, scktConn.ConnID); }
private void ThreadTask() { try { while (!m_Terminate) { switch (m_ClientConnStates) { case ClientConnStates.Disconnected: { break; } case ClientConnStates.SendListServices: { CommandEtherNetIPHeader msgListServices = new CommandEtherNetIPHeader { Command = EncapsulationCommands.ListServices }; SendMessage(msgListServices, null); m_ClientConnStates = ClientConnStates.WaitListServicesReply; break; } case ClientConnStates.SendRegisterSession: { MsgRegisterSessionRequest msg = new MsgRegisterSessionRequest(); CommandEtherNetIPHeader header = new CommandEtherNetIPHeader { Command = EncapsulationCommands.RegisterSession }; msg.CommandSpecificDataRegisterSession = new CommandSpecificDataRegisterSession { ProtocolVersion = 1 }; header.Length = msg.CommandSpecificDataRegisterSession.SizeOf(); SendMessage(header, msg); m_ClientConnStates = ClientConnStates.WaitRegisterSessionReply; break; } case ClientConnStates.SendReceive: { if (m_DataToSend != null) { CommandEtherNetIPHeader header = new CommandEtherNetIPHeader { Command = EncapsulationCommands.SendRRData, SessionHandle = m_SessionHandle, SenderContext = BitConverter.GetBytes(m_SenderContext) }; MsgUnconnectedSendRequest msg = new MsgUnconnectedSendRequest { CommandSpecificDataSendRRData = new CommandSpecificDataSendRRData { InterfaceHandle = 0, Timeout = 0, ItemCount = 2, List = new CommandSpecificDataSendRRDataItem[] { new CommandSpecificDataSendRRDataItem { TypeID = 0, Length = 0 }, new CommandSpecificDataSendRRDataItem { TypeID = CommonPacketItemID.UnconnectedMessage, Length = 0 } } }, CommonIndustrialProtocolRequest = new CommonIndustrialProtocolRequest { Service = 0x52, // Unconnected Send Service ID RequestPathSize = 2, PathSegmentList = new List <PathSegment> { new LogicalPathSegment8bits { PathSegmentType = 0x20, LogicalValue = 0x06 // Connection Manager Class ID }, new LogicalPathSegment8bits { PathSegmentType = 0x24, LogicalValue = 0x01 // Connection Manager Instance ID } } }, CIPConnectionManagerUnconnSnd = new CIPConnectionManagerUnconnSnd { PriorityAndPickTime = 0x07, TimeOutTicks = 233, MessageRequestSize = 0, CommonIndustrialProtocolRequest = new CommonIndustrialProtocolRequest { Service = 0x4d, // CIP Write Data Service RequestPathSize = 0, }, CIPClassGeneric = new CIPClassGeneric { DataType = m_DataTypeToSend, SpecificDataSize = (ushort)(m_DataToSend.Length / CIPClassGeneric.GetDataTypeSize(m_DataTypeToSend)), CIPClassGenericCmdSpecificData = m_DataToSend, }, Pad = null, RoutePathSize = 0, Reserved = 0, RoutePath = new List <PathSegment> { new PortPathSegment { PathSegmentType = 0x01, OptionalLinkAddressSize = null, OptionalExtendedPortIdentifier = null, LinkAddress = new byte[] { m_LinkAddress }, Pad = null } } } }; msg.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.PathSegmentList = new List <PathSegment>(); string[] segs = m_Symbol.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); foreach (string seg in segs) { msg.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.PathSegmentList.Add( new DataPathSegmentANSISymb { PathSegmentType = 0x91, DataSize = (byte)seg.Length, ANSISymbol = Encoding.ASCII.GetBytes(seg.Length % 2 == 0 ? seg : seg + "\0") }); } msg.CIPConnectionManagerUnconnSnd.Pad = (msg.CIPConnectionManagerUnconnSnd.SizeOf() % 2) == 0 ? null : (byte?)0; msg.CIPConnectionManagerUnconnSnd.RoutePathSize = (byte)(msg.CIPConnectionManagerUnconnSnd.RoutePath.Sum(a => a.SizeOf()) / 2); msg.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.RequestPathSize = (byte)(msg.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.PathSegmentList.Sum(a => a.SizeOf()) / 2); msg.CIPConnectionManagerUnconnSnd.MessageRequestSize = (ushort)(msg.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.SizeOf() + msg.CIPConnectionManagerUnconnSnd.CIPClassGeneric.SizeOf()); msg.CommandSpecificDataSendRRData.List[1].Length = (ushort)(msg.CommonIndustrialProtocolRequest.SizeOf() + msg.CIPConnectionManagerUnconnSnd.SizeOf()); header.Length = msg.SizeOf(); SendMessage(header, msg); } else { CommandEtherNetIPHeader msgListServices = new CommandEtherNetIPHeader { Command = EncapsulationCommands.NOP }; SendMessage(msgListServices, null); } break; } } if (!m_Terminate) { m_ThreadResetEvent.WaitOne(2000); } } } catch (Exception exc) { Trace(EventType.Error, string.Format("{0} - Thread exception: {1}", LOG_TAG, exc.Message)); Trace(exc); } }