public static void Register(ENUM_CMD type, CallBackFunction func) { if (!_callbacks.ContainsKey(type)) { _callbacks.Add(type, func); } }
public Result API_UpdataEnterUpdateMode ( ) { #if _TRACE_OUT_PUT PrintMagToTxt("API_UpdataEnterUpdateMode"); #endif Result result = Result.OK; ENUM_CMD TxCmd = ENUM_CMD.NOTHING; CommonParms common = new CommonParms(); byte[] buf = new byte[57]; Array.Clear(buf, 0, buf.Length); TxCmd = ENUM_CMD.UPDAUE_ENTER_UPDATE_MODE; buf[0] = 0x0D; //Magic buf[1] = 0xF0; //Magic buf[2] = (byte)((int)TxCmd & 0xF); //command buf[3] = 0; //command buf[4] = 1; //Length buf[5] = 0; //Length buf[6] = 0; //Reserved buf[7] = 0; //Reserved buf[8] = 0; //0=>Enter update mode if (false == this.SendData(buf)) { return(Result.FAILURE); } //Run report packet progress common.callback = null; common.callbackCode = IntPtr.Zero; common.context = IntPtr.Zero; if (CheckTagAccessPkt(common) == false) { return(Result.FAILURE); } Array.Clear(buf, 0, buf.Length); //Check receive data if (false == this.ReceiveData(TxCmd, ref buf)) { return(Result.FAILURE); } //Check result result = ConvertResult(buf[4]); if (Result.OK != result) { return(result); } return(Result.OK); }
public static void SendPkgToServer <T>(ENUM_CMD cmdID, T obj, Action <object> callback = null) { //Debug.Log("SendPkgToServer,cmd=" + cmdID); if (IsSingle && LocalServer.ProcessRequest(cmdID, obj, callback)) { return; } byte[] data = Serialize(obj); int length = data.Length; List <byte> bytes = new List <byte>(); bytes.AddRange(BitConverter.GetBytes((ushort)cmdID)); bytes.AddRange(BitConverter.GetBytes((uint)length)); bytes.AddRange(data); _message.Enqueue(bytes.ToArray()); if (callback != null) { if (!callbackDict.ContainsKey(cmdID)) { callbackDict[cmdID] = new List <NetworkCallback>(); } callbackDict[cmdID].Add(new NetworkCallback { func = callback, isDelete = true }); } }
public static bool ProcessRequest <T>(ENUM_CMD cmdID, T obj, Action <object> callback = null) { if (dict.ContainsKey(cmdID)) { dict[cmdID](obj, callback); return(true); } return(false); }
public static void Register(ENUM_CMD type, Action <byte[]> _func) { if (!callbackDict.ContainsKey(type)) { callbackDict[type] = new List <NetworkCallback>(); } callbackDict[type].Add(new NetworkCallback { func = _func, isDelete = false }); }
protected bool ReceiveData ( ENUM_CMD r_TxCmd, ref byte[] r_btData ) { ENUM_CMD result = ENUM_CMD.NOTHING; byte [] rdBuf = new byte[128]; Array.Clear(rdBuf, 0, rdBuf.Length); //Reset Receive flow ResetRxFlow(ENMU_PKT_TYPE.UPDATE_COMMON); while (result != r_TxCmd) { result = this.Regular(ref rdBuf, true); #if _TRACE_OUT_PUT PrintMagToTxt("ReceiveData"); #endif if (result == ENUM_CMD.TIME_OUT) //Timeout { #if _TRACE_OUT_PUT PrintMagToTxt("TIME OUT ReceiveData"); #endif //Try to connect the device again clsPacket.Mid_API_Close(); //clsPacket.TRANS_API_Close(); //this.LinkDevice(); //End by Wayne for implement high baud rate function, 2015-06-05 return(false); } TimeBeginPeriod(1); System.Threading.Thread.Sleep(5); TimeEndPeriod(1); } //Data ~ Data over //only copy Data int Length = ENMU_UPDATE_INDEX.PKT_CRC - ENMU_UPDATE_INDEX.PKT_DATA; Array.Copy(rdBuf, (int)ENMU_UPDATE_INDEX.PKT_DATA, r_btData, 0, Length); return(true); }
//Add by Wayne for improve FW upgrade feature for integrate M03X model, 2015-03-31 public Result API_UpdateGetModelName ( ref uint MainModelName ) { #if _TRACE_OUT_PUT PrintMagToTxt("API_UpdateGetModelName"); #endif Result result = Result.OK; ENUM_CMD TxCmd = ENUM_CMD.NOTHING; byte[] buf = new byte[57]; Array.Clear(buf, 0, buf.Length); byte[] ModelName_buf = new byte[4]; TxCmd = ENUM_CMD.UPDATE_GET_MODEL_NAME; buf[0] = 0x0D; //Magic buf[1] = 0xF0; //Magic buf[2] = (byte)((int)TxCmd & 0xF); //command buf[3] = 0; //command buf[4] = 0; //Length buf[5] = 0; //Length buf[6] = 0; //Reserved buf[7] = 0; //Reserved if (false == this.SendData(buf)) { return(Result.FAILURE); } Array.Clear(buf, 0, buf.Length); //Check receive data if (false == this.ReceiveData(TxCmd, ref buf)) { return(Result.FAILURE); } //Check result result = ConvertResult(buf[4]); if (Result.OK != result) { return(result); } ModelName_buf[0] = buf[8]; ModelName_buf[1] = buf[9]; ModelName_buf[2] = buf[10]; ModelName_buf[3] = buf[11]; MainModelName = BitConverter.ToUInt32(ModelName_buf, 0); return(Result.OK); }
//==========================Command=================================== public Result API_UpdataGetVersion ( ref MacBootLoaderVersion r_Version ) { #if _TRACE_OUT_PUT PrintMagToTxt("API_UpdataGetVersion"); #endif Result result = Result.OK; ENUM_CMD TxCmd = ENUM_CMD.NOTHING; byte[] buf = new byte[57]; Array.Clear(buf, 0, buf.Length); TxCmd = ENUM_CMD.UPDAUE_GET_VERSION; buf[0] = 0x0D; //Magic buf[1] = 0xF0; //Magic buf[2] = (byte)((int)TxCmd & 0xF); //command buf[3] = 0; //command buf[4] = 0; //Length buf[5] = 0; //Length buf[6] = 0; //Reserved buf[7] = 0; //Reserved if (false == this.SendData(buf)) { return(Result.FAILURE); } Array.Clear(buf, 0, buf.Length); //Check receive data if (false == this.ReceiveData(TxCmd, ref buf)) { return(Result.FAILURE); } //Check result result = ConvertResult(buf[4]); if (Result.OK != result) { return(result); } r_Version.major = buf[11]; r_Version.minor = buf[10]; r_Version.release = buf[09]; return(Result.OK); }
public static void Enqueue <T>(Socket socket, ENUM_CMD type, T obj) { using (MemoryStream stream = new MemoryStream()) { ProtoBuf.Serializer.Serialize(stream, obj); Package package = new Package { socket = socket, type = type, data = stream.ToArray() }; _message.Enqueue(package); } }
public Result API_UpdataComplete ( ) { #if _TRACE_OUT_PUT PrintMagToTxt("API_UpdataReset"); #endif Result result = Result.OK; ENUM_CMD TxCmd = ENUM_CMD.NOTHING; byte[] buf = new byte[57]; Array.Clear(buf, 0, buf.Length); TxCmd = ENUM_CMD.UPDAUE_COMPLETE; buf[0] = 0x0D; //Magic buf[1] = 0xF0; //Magic buf[2] = (byte)((int)TxCmd & 0xF); //command; buf[3] = 0; //command buf[4] = 0; //Length buf[5] = 0; //Length buf[6] = 0; //Reserved buf[7] = 0; //Reserved if (false == this.SendData(buf)) { return(Result.FAILURE); } Array.Clear(buf, 0, buf.Length); //Check receive data if (false == this.ReceiveData(TxCmd, ref buf)) { return(Result.FAILURE); } //Check result result = ConvertResult(buf[4]); if (Result.OK != result) { return(result); } return(Result.OK); }
public static void Notify(ENUM_CMD cmd, object data) { if (callbackDict.ContainsKey(cmd)) { List <NetworkCallback> callbackList = callbackDict[cmd]; for (int i = callbackList.Count - 1; i >= 0; i--) { NetworkCallback callback = callbackList[i]; callback.func(data); if (callback.isDelete) { callbackList.RemoveAt(i); } } } }
static void Receive(object obj) { Player player = obj as Player; while (true) { byte[] data = new byte[6]; int receive = 0; try { receive = player.socket.Receive(data); } catch { PlayerDisconnect(player); break; } if (receive > 0) { MemoryStream stream = new MemoryStream(data); BinaryReader binary = new BinaryReader(stream, Encoding.UTF8); ENUM_CMD type = (ENUM_CMD)binary.ReadUInt16(); uint length = binary.ReadUInt32(); data = new byte[length]; player.socket.Receive(data); stream = new MemoryStream(data); if (type != ENUM_CMD.CS_HERO_MOVE_REQ) { Ultilities.Print($"recieved message, type={type}, length={length}"); } if (_callbacks.ContainsKey(type)) { CallBackFunction func = _callbacks[type]; func(player, stream); } } else { PlayerDisconnect(player); break; } } }
protected bool ReceiveData( ENUM_CMD r_TxCmd, ref byte[] r_btData ) { ENUM_CMD result = ENUM_CMD.NOTHING; byte [] rdBuf = new byte[128]; Array.Clear(rdBuf, 0, rdBuf.Length); //Reset Receive flow ResetRxFlow(ENMU_PKT_TYPE.UPDATE_COMMON); while (result != r_TxCmd) { result = this.Regular( ref rdBuf, true); #if _TRACE_OUT_PUT PrintMagToTxt("ReceiveData"); #endif if (result == ENUM_CMD.TIME_OUT) //Timeout { #if _TRACE_OUT_PUT PrintMagToTxt("TIME OUT ReceiveData"); #endif //Try to connect the device again clsPacket.TRANS_API_Close(); this.LinkDevice(); return false; } TimeBeginPeriod(1); System.Threading.Thread.Sleep( 5 ); TimeEndPeriod(1); } //Data ~ Data over //only copy Data int Length = ENMU_UPDATE_INDEX.PKT_CRC - ENMU_UPDATE_INDEX.PKT_DATA; Array.Copy( rdBuf, (int)ENMU_UPDATE_INDEX.PKT_DATA, r_btData, 0, Length); return true; }
public Result API_UpdataSendUpdateData ( Updatedata r_strcData ) { #if _TRACE_OUT_PUT PrintMagToTxt("API_UpdataSendUpdateData"); #endif Result result = Result.OK; ENUM_CMD TxCmd = ENUM_CMD.NOTHING; byte[] buf = new byte[57]; Array.Clear(buf, 0, buf.Length); TxCmd = ENUM_CMD.UPDAUE_SEND_UPDATE_DATA; buf[0] = 0x0D; //Magic buf[1] = 0xF0; //Magic buf[2] = (byte)((int)TxCmd & 0xF); //command; buf[3] = 0; //command buf[4] = (byte)(r_strcData.usSize & 0xFF); //Length buf[5] = (byte)((r_strcData.usSize >> 8) & 0xFF); //Length buf[6] = 0; //Reserved buf[7] = 0; //Reserved //Data======== //Addr buf[8] = (byte)(r_strcData.uiOffset & 0xFF); buf[9] = (byte)((r_strcData.uiOffset >> 8) & 0xFF); buf[10] = (byte)((r_strcData.uiOffset >> 16) & 0xFF); buf[11] = (byte)((r_strcData.uiOffset >> 24) & 0xFF); //Flag buf[16] = (byte)(r_strcData.bRealRunFlag == true ? 0 : 1); //Flag = 1 => Test mode //Data Array.Copy(r_strcData.btData, 0, buf, 20, r_strcData.btData.Length); //CRC uint uiCrc = 0; uiCrc = API_OtherCrc32(API_OtherCrc32(uiCrc, buf, 0, 12), buf, 16, (uint)(r_strcData.usSize - 2) * 4); buf[12] = (byte)(uiCrc & 0xFF); buf[13] = (byte)((uiCrc >> 8) & 0xFF); buf[14] = (byte)((uiCrc >> 16) & 0xFF); buf[15] = (byte)((uiCrc >> 24) & 0xFF); if (false == this.SendData(buf)) { return(Result.FAILURE); } Array.Clear(buf, 0, buf.Length); //Check receive data if (false == this.ReceiveData(TxCmd, ref buf)) { return(Result.FAILURE); } //Check result result = ConvertResult(buf[4]); if (Result.OK != result) { return(result); } return(Result.OK); }
public CCommandQueueItem(ENUM_CMD Command, int Channel, double TargetTemperature) { this.Command = Command; this.Channel = Channel; this.Temperature = TargetTemperature; }
protected ENUM_CMD Regular( [In, Out] ref byte[] RxBuf, [In] bool bCheckTimeOut ) { ENUM_CMD result = ENUM_CMD.NOTHING; TRANS_DEV_TYPE enumDev = clsPacket.TRANS_API_AskDevType(); RxCmd = ENUM_CMD.NOTHING; //Check time out if (bCheckTimeOut == true) { TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - m_timeBefReg.Ticks); if (ts.TotalMilliseconds > 1500) { #if _TRACE_OUT_PUT PrintMagToTxt("Regular: TimeOut"); #endif RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; return ENUM_CMD.TIME_OUT; } } #if _TRACE_OUT_PUT PrintMagToTxt("Regular"); #endif UInt32 RevCount = clsPacket.TRANS_API_AskRevCount(); //USB receives one report at one time. Don't need to check every byte. if (enumDev == TRANS_DEV_TYPE.USB) { if ( 0 == clsPacket.TRANS_API_Read(RxBuf, RevCount) ) return result; //USB receives one report at one time. Don't need to find Pkt ID and Header. RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; if (PutPktToData(enumDev, RxBuf) == ENMU_REV_STATE.REVEICE_DONE) { result = RxCmd; } return result; } for (int i = 0; i < RevCount; i++) { switch (RxStep) { case ENMU_PKT_FLOW.STEP_HEADER_1: RxCnt = 0; usDataSingleLenDw = 0; usPktSingleLenBt = 0; usDivideNum = 0; usDivideSeq = 0; usRxCRC = 0; RxCmd = ENUM_CMD.NOTHING; result = ENUM_CMD.NOTHING; Array.Clear(RxBuf, 0, RxBuf.Length); //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; //Check Header switch (RxByte[0]) { case (byte)'B': //Begin case (byte)'E': //End case (byte)'I': //Inventory case (byte)'A': //Tag Access case (byte)'R': //Common Response case (byte)'S': //Update Common Response case (byte)'F': //Update Enter Firmware configuration RxCnt++; RxStep++; break; default: break; } break; case ENMU_PKT_FLOW.STEP_HEADER_2: //Header //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; if (RxByte[0] == 'I') { RxCnt++; RxStep++; } else { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; } break; case ENMU_PKT_FLOW.STEP_HEADER_3: //Header //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; if (RxByte[0] == 'T') { RxCnt++; RxStep++; } else { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; } break; case ENMU_PKT_FLOW.STEP_HEADER_4: //Header //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; if (RxByte[0] == 'M') { RxCnt++; RxStep++; } else { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; } break; case ENMU_PKT_FLOW.STEP_HANDLE: if (PutPktToData(enumDev, RxBuf) == ENMU_REV_STATE.REVEICE_DONE) { result = RxCmd; return result; } break; default: RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; break; }//end switch }//end for return result; }
static void Receive() { while (connected) { NetworkStream stream = tcpClient.GetStream(); byte[] data = new byte[6]; IAsyncResult headerResult = stream.BeginRead(data, 0, data.Length, null, null); while (!headerResult.IsCompleted) { Thread.Sleep(1); } int readNum = 0; try { readNum = stream.EndRead(headerResult); } catch { Disconnect(); } if (readNum > 0) { MemoryStream lengthStream = new MemoryStream(data); BinaryReader binary = new BinaryReader(lengthStream, Encoding.UTF8); ENUM_CMD type = (ENUM_CMD)binary.ReadUInt16(); uint length = binary.ReadUInt32(); MemoryStream bodyStream = new MemoryStream(); int totalBytesRead = 0; byte[] bufdata; if (length > 1024) { bufdata = new byte[1024]; } else { bufdata = new byte[length]; } int tick1 = Environment.TickCount; do { if (stream.DataAvailable) { int bytesRead = stream.Read(bufdata, 0, bufdata.Length); totalBytesRead += bytesRead; bodyStream.Write(bufdata, 0, bytesRead); } else { Thread.Sleep(1); } } while (totalBytesRead < length); float s = (Environment.TickCount - tick1) / 1000f; //Debug.Log("time = " + s); if (type != ENUM_CMD.CS_PLAYER_MOVE_NOTIFY) { Debug.Log("receive message, type=" + type + ",length=" + length + ",totalBytesRead=" + totalBytesRead); } Package package = new Package(); package.type = type; package.data = bodyStream.ToArray(); lock (packageQueue) { packageQueue.Enqueue(package); } } else { Disconnect(); } } }
protected ENMU_REV_STATE PutPktToData( [In] TRANS_DEV_TYPE enumDevice, [In] byte[] RxBuf ) { int Length = 0; int CrcLen = 0; UInt16 usCRC = 0; #if _TRACE_OUT_PUT PrintMagToTxt("PutPktToData"); #endif try { //Get Per Packet size and remaining bytes switch (RxBuf[0]) { //Tag Access===================== case (byte)'B': //Begin case (byte)'E': //End RxPktTpye = ENMU_PKT_TYPE.TAG_ACCESS; usPktSingleLenBt = 24; Length = usPktSingleLenBt - (int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM; break; case (byte)'I': //Inventory case (byte)'A': //Tag Access RxPktTpye = ENMU_PKT_TYPE.TAG_ACCESS; usPktSingleLenBt = 64; Length = usPktSingleLenBt - (int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM; break; //Common========================== case (byte)'R': //Common RxPktTpye = ENMU_PKT_TYPE.COMMON; usPktSingleLenBt = 16; Length = (int)ENMU_PKT_INDEX.END - (int)ENMU_PKT_INDEX.PKT_DEVIDE_ID; break; //Update========================== case (byte)'F': //Update firmware mode RxPktTpye = ENMU_PKT_TYPE.UPDATE_ENTER; usPktSingleLenBt = 24; Length = usPktSingleLenBt - (int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM; break; case (byte)'S': //Update Common RxPktTpye = ENMU_PKT_TYPE.UPDATE_COMMON; usPktSingleLenBt = 64; Length = usPktSingleLenBt - (int)ENMU_UPDATE_INDEX.PKT_DEVIDE_ID; break; default: RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; return ENMU_REV_STATE.RECEIVE_FAULT; break; } //Receive data if (enumDevice == TRANS_DEV_TYPE.SERIAL) { //Data is not ready. Break and wait for next loop to get. if (clsPacket.TRANS_API_AskRevCount() < Length) { System.Threading.Thread.Sleep(5); return ENMU_REV_STATE.WAIT_DATA; } byte[] tmp = new byte[Length]; Array.Clear(tmp, 0, tmp.Length); //Receive data fail. if (Length != clsPacket.TRANS_API_Read(tmp, (uint)Length)) { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; return ENMU_REV_STATE.RECEIVE_FAULT; } Array.Copy(tmp, 0, RxBuf, RxCnt, tmp.Length); } RxCnt += Length; RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; //Handle Data switch (RxPktTpye) { case ENMU_PKT_TYPE.COMMON: { RxCmd = (ENUM_CMD)RxBuf[(int)ENMU_PKT_INDEX.PKT_COMMAND_ID]; usRxCRC = BitConverter.ToUInt16(RxBuf, (int)ENMU_PKT_INDEX.PKT_CRC); CrcLen = (int)(ENMU_PKT_INDEX.PKT_CRC - ENMU_PKT_INDEX.PKT_HEADER); break; } case ENMU_PKT_TYPE.TAG_ACCESS: { usDivideNum = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM]; usDivideSeq = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_SEQ]; RxCmd = (ENUM_CMD)(BitConverter.ToInt16 (RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_TYPE) | 0xF0); /* Mod by james for large user memory, 2012-12-04 */ if (usDivideSeq == 1) { usDataSingleLenDw = BitConverter.ToUInt16(RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_LENGTH); } else { if (usDivideNum - usDivideSeq == 0) { usDataSingleLenDw = (UInt16)(BitConverter.ToUInt16(RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_LENGTH) - 12 * (usDivideNum - 1)); } else { usDataSingleLenDw = 12; } } /* End by james for large user memory, 2012-12-04 */ usRxCRC = BitConverter.ToUInt16(RxBuf, usPktSingleLenBt - 2); CrcLen = (ushort)(usPktSingleLenBt - 2); break; } case ENMU_PKT_TYPE.UPDATE_ENTER: { usDivideNum = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM]; usDivideSeq = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_SEQ]; //It uses TAG_ACCESS Packet, but reture 0x300B. Redefine to ENUM_CMD. RxCmd = ENUM_CMD.UPDAUE_ENTER_UPDATE_MODE; usDataSingleLenDw = BitConverter.ToUInt16 (RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_LENGTH); usRxCRC = BitConverter.ToUInt16(RxBuf, usPktSingleLenBt - 2); CrcLen = (ushort)(usPktSingleLenBt - 2); break; } case ENMU_PKT_TYPE.UPDATE_COMMON: { RxCmd = (ENUM_CMD)(RxBuf[(int)ENMU_UPDATE_INDEX.PKT_DATA_RECMD] | 0xE0); usRxCRC = BitConverter.ToUInt16(RxBuf, usPktSingleLenBt - 2); CrcLen = (ushort)(usPktSingleLenBt - 2); break; } default: break; } //Check CRC. CRC Legnth doesn't include CRC(2 bytes) usCRC = CrcCul(RxBuf, 0, (ushort)(CrcLen * 8)); //8 bits usCRC = (UInt16)(~usCRC & 0xFFFF); } catch (Exception e) { #if _TRACE_OUT_PUT this.PrintMagToTxt("Exception Error : PutPktToData"); #endif clsPacket.TRANS_API_ClearBuffer(); return ENMU_REV_STATE.RECEIVE_FAULT; } if (usCRC == usRxCRC) { return ENMU_REV_STATE.REVEICE_DONE; } else { return ENMU_REV_STATE.RECEIVE_FAULT; } }