private void HandleASDU(ASDU asdu) { DebugLog("Handle received ASDU"); bool messageHandled = false; switch (asdu.TypeId) { case TypeID.C_IC_NA_1: /* 100 - interrogation command */ DebugLog("Rcvd interrogation command C_IC_NA_1"); if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.DEACTIVATION)) { if (server.interrogationHandler != null) { InterrogationCommand irc = (InterrogationCommand)asdu.GetElement(0); if (server.interrogationHandler(server.InterrogationHandlerParameter, this, asdu, irc.QOI)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDUInternal(asdu); } break; case TypeID.C_CI_NA_1: /* 101 - counter interrogation command */ DebugLog("Rcvd counter interrogation command C_CI_NA_1"); if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.DEACTIVATION)) { if (server.counterInterrogationHandler != null) { CounterInterrogationCommand cic = (CounterInterrogationCommand)asdu.GetElement(0); if (server.counterInterrogationHandler(server.counterInterrogationHandlerParameter, this, asdu, cic.QCC)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDUInternal(asdu); } break; case TypeID.C_RD_NA_1: /* 102 - read command */ DebugLog("Rcvd read command C_RD_NA_1"); if (asdu.Cot == CauseOfTransmission.REQUEST) { DebugLog("Read request for object: " + asdu.Ca); if (server.readHandler != null) { ReadCommand rc = (ReadCommand)asdu.GetElement(0); if (server.readHandler(server.readHandlerParameter, this, asdu, rc.ObjectAddress)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDUInternal(asdu); } break; case TypeID.C_CS_NA_1: /* 103 - Clock synchronization command */ DebugLog("Rcvd clock sync command C_CS_NA_1"); if (asdu.Cot == CauseOfTransmission.ACTIVATION) { if (server.clockSynchronizationHandler != null) { ClockSynchronizationCommand csc = (ClockSynchronizationCommand)asdu.GetElement(0); if (server.clockSynchronizationHandler(server.clockSynchronizationHandlerParameter, this, asdu, csc.NewTime)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDUInternal(asdu); } break; case TypeID.C_TS_NA_1: /* 104 - test command */ DebugLog("Rcvd test command C_TS_NA_1"); if (asdu.Cot != CauseOfTransmission.ACTIVATION) { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; } else { asdu.Cot = CauseOfTransmission.ACTIVATION_CON; } this.SendASDUInternal(asdu); messageHandled = true; break; case TypeID.C_RP_NA_1: /* 105 - Reset process command */ DebugLog("Rcvd reset process command C_RP_NA_1"); if (asdu.Cot == CauseOfTransmission.ACTIVATION) { if (server.resetProcessHandler != null) { ResetProcessCommand rpc = (ResetProcessCommand)asdu.GetElement(0); if (server.resetProcessHandler(server.resetProcessHandlerParameter, this, asdu, rpc.QRP)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDUInternal(asdu); } break; case TypeID.C_CD_NA_1: /* 106 - Delay acquisition command */ DebugLog("Rcvd delay acquisition command C_CD_NA_1"); if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.SPONTANEOUS)) { if (server.delayAcquisitionHandler != null) { DelayAcquisitionCommand dac = (DelayAcquisitionCommand)asdu.GetElement(0); if (server.delayAcquisitionHandler(server.delayAcquisitionHandlerParameter, this, asdu, dac.Delay)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDUInternal(asdu); } break; } if ((messageHandled == false) && (server.asduHandler != null)) { if (server.asduHandler(server.asduHandlerParameter, this, asdu)) { messageHandled = true; } } if (messageHandled == false) { asdu.Cot = CauseOfTransmission.UNKNOWN_TYPE_ID; this.SendASDUInternal(asdu); } }
/// <summary> /// 处理消息 /// </summary> /// <param name="socket"></param> /// <param name="buffer"></param> /// <param name="msgSize"></param> /// <returns></returns> private bool HandleMessage(Socket socket, byte[] buffer, int msgSize) { if ((buffer[2] & 1) == 0) { if (debugOutput) { Console.WriteLine("Received I frame"); } if (msgSize < 7) { if (debugOutput) { Console.WriteLine("I msg too small!"); } return(false); } IncreaseReceivedMessageCounters(); if (isActive) { bool messageHandled = false; ASDU asdu = new ASDU(parameters, buffer, msgSize); //根据类型来分别处理 switch (asdu.TypeId) { case TypeID.C_IC_NA_1: /* 100 - interrogation command 总召*/ if (debugOutput) { Console.WriteLine("Rcvd interrogation command C_IC_NA_1"); } if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.DEACTIVATION)) { if (server.interrogationHandler != null) { InterrogationCommand irc = (InterrogationCommand)asdu.GetElement(0); if (server.interrogationHandler(server.InterrogationHandlerParameter, this, asdu, irc.QOI)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDU(asdu); } break; case TypeID.C_CI_NA_1: /* 101 - counter interrogation command 累计量总召*/ if (debugOutput) { Console.WriteLine("Rcvd counter interrogation command C_CI_NA_1"); } if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.DEACTIVATION)) { if (server.counterInterrogationHandler != null) { CounterInterrogationCommand cic = (CounterInterrogationCommand)asdu.GetElement(0); if (server.counterInterrogationHandler(server.counterInterrogationHandlerParameter, this, asdu, cic.QCC)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDU(asdu); } break; case TypeID.C_RD_NA_1: /* 102 - read command 读命令*/ if (debugOutput) { Console.WriteLine("Rcvd read command C_RD_NA_1"); } if (asdu.Cot == CauseOfTransmission.REQUEST) { if (debugOutput) { Console.WriteLine("Read request for object: " + asdu.Ca); } if (server.readHandler != null) { ReadCommand rc = (ReadCommand)asdu.GetElement(0); if (server.readHandler(server.readHandlerParameter, this, asdu, rc.ObjectAddress)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDU(asdu); } break; case TypeID.C_CS_NA_1: /* 103 - Clock synchronization command 时钟同步 */ if (debugOutput) { Console.WriteLine("Rcvd clock sync command C_CS_NA_1"); } if (asdu.Cot == CauseOfTransmission.ACTIVATION) { if (server.clockSynchronizationHandler != null) { ClockSynchronizationCommand csc = (ClockSynchronizationCommand)asdu.GetElement(0); if (server.clockSynchronizationHandler(server.clockSynchronizationHandlerParameter, this, asdu, csc.NewTime)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDU(asdu); } break; case TypeID.C_TS_NA_1: /* 104 - test command */ if (debugOutput) { Console.WriteLine("Rcvd test command C_TS_NA_1"); } //直接处理 if (asdu.Cot != CauseOfTransmission.ACTIVATION) { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; } else { asdu.Cot = CauseOfTransmission.ACTIVATION_CON; } this.SendASDU(asdu); messageHandled = true; break; case TypeID.C_RP_NA_1: /* 105 - Reset process command 复位命令*/ if (debugOutput) { Console.WriteLine("Rcvd reset process command C_RP_NA_1"); } if (asdu.Cot == CauseOfTransmission.ACTIVATION) { if (server.resetProcessHandler != null) { ResetProcessCommand rpc = (ResetProcessCommand)asdu.GetElement(0); if (server.resetProcessHandler(server.resetProcessHandlerParameter, this, asdu, rpc.QRP)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDU(asdu); } break; case TypeID.C_CD_NA_1: /* 106 - Delay acquisition command 延时获得*/ if (debugOutput) { Console.WriteLine("Rcvd delay acquisition command C_CD_NA_1"); } if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.SPONTANEOUS)) { if (server.delayAcquisitionHandler != null) { DelayAcquisitionCommand dac = (DelayAcquisitionCommand)asdu.GetElement(0); if (server.delayAcquisitionHandler(server.delayAcquisitionHandlerParameter, this, asdu, dac.Delay)) { messageHandled = true; } } } else { asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION; this.SendASDU(asdu); } break; } //都没处理,就直接当通用ASDU处理掉了 if ((messageHandled == false) && (server.asduHandler != null)) { if (server.asduHandler(server.asduHandlerParameter, this, asdu)) { messageHandled = true; } } //依旧没处理,那就返回未知 if (messageHandled == false) { asdu.Cot = CauseOfTransmission.UNKNOWN_TYPE_ID; this.SendASDU(asdu); } } else { // connection not activated --> skip message if (debugOutput) { Console.WriteLine("Message not activated. Skip I message"); } } return(true); } // Check for TESTFR_ACT message else if ((buffer[2] & 0x43) == 0x43) { if (debugOutput) { Console.WriteLine("Send TESTFR_CON"); } socket.Send(TESTFR_CON_MSG); } // Check for STARTDT_ACT message else if ((buffer[2] & 0x07) == 0x07) { if (debugOutput) { Console.WriteLine("Send STARTDT_CON"); } this.isActive = true; socket.Send(STARTDT_CON_MSG); } // Check for STOPDT_ACT message else if ((buffer[2] & 0x13) == 0x13) { if (debugOutput) { Console.WriteLine("Send STOPDT_CON"); } this.isActive = false; socket.Send(STOPDT_CON_MSG); } // S-message S帧(用于确认对方的接收序号) else if (buffer[2] == 0x01) { int messageCount = (buffer[4] + buffer[5] * 0x100) / 2; Console.WriteLine("Recv S(" + messageCount + ") (own sendcounter = " + sendCount + ")"); } else { Console.WriteLine("Unknown message"); return(true); } return(true); }