private void decode_Response(PhotonCmdHeader cmd_hdr, BinaryReader packet) { // see func ReliableMessage() decode in... // https://github.com/broderickhyman/photon_spectator/blob/master/photon_command.go var opResponse = new ReliableMessage_Response(); opResponse.ChannelID = cmd_hdr.ChannelID; opResponse.OperationCode = packet.ReadByte(); // Operation Code opResponse.OperationResponseCode = packet.ReadUInt16(); // Operation Response Code opResponse.OperationDebugByte = packet.ReadByte(); // Operation Debug Byte opResponse.ParameterCount = packet.ReadInt16(); // Parameter Count (?? is this valid for all msg types?) Event_Photon_Cmd_Info?.Invoke("Photon Reliable Response - " + opResponse.OperationResponseCode); var parameters = new ReliableMessage_Response.Paramaters(); // decode the paramaters for (int i = 0; i < opResponse.ParameterCount; i++) { var paramID = packet.ReadByte(); // paramID PhotonParamType paramType = (PhotonParamType)packet.ReadByte(); // paramType var param_value = Decode_PhotonValueType.Decode(packet, paramType); parameters[paramID] = param_value; Event_Photon_Info?.Invoke("ParamID: " + paramID + " value: " + param_value.ToString()); } opResponse.ParamaterData = parameters; Event_Photon_ReliableResponse?.Invoke(opResponse); }
public void decodeUDPPacket(BinaryReader packet) { const int CMD_HDR_LEN = 12; // read Photon Header packet.ReadUInt16(); // PeerID packet.ReadByte(); // CrcEnabled int cmd_count = (int)packet.ReadByte(); // Command Count packet.ReadUInt32(); // Timestamp packet.ReadInt32(); // Challenge Event_Photon_Info?.Invoke(String.Format("Photon Packet with ({0}) commands", cmd_count)); for (int cmd_number = 0; cmd_number < cmd_count; cmd_number++) { // ********SSSS***************************** // Decode Photon Command Header // ************************************* var cmd_hdr = new PhotonCmdHeader(); cmd_hdr.type /**/ = (CommandType)packet.ReadByte(); // Type cmd_hdr.ChannelID /**/ = packet.ReadByte(); // ChannelID cmd_hdr.flags /**/ = packet.ReadByte(); // Flags cmd_hdr.ReservedByte /**/ = packet.ReadByte(); // ReservedByte cmd_hdr.CmdLength /**/ = packet.ReadInt32(); // Length -- ? uint32 ? cmd_hdr.ReliableSequenceNumber /**/ = packet.ReadInt32(); // reliablesequencenumber -- ? uint32 ? int data_length = cmd_hdr.CmdLength - CMD_HDR_LEN; Event_Photon_Cmd_Info?.Invoke( String.Format(" [{0}] Photon Cmd - {1}:{2} len {3}", cmd_number, cmd_hdr.type.ToString(), (int)cmd_hdr.type, cmd_hdr.CmdLength)); // ***** read command data **** byte[] data = packet.ReadBytes(data_length); try { decode_PhotonPacket(cmd_hdr, data); } #if HIDE_PARSE_ERRORS catch (System.IO.EndOfStreamException ex) { } #endif finally {} } // for loop }
private void decode_PhotonPacket(PhotonCmdHeader cmd_hdr, byte[] data) { var packet = new BeBinaryReader(new MemoryStream(data, 0, data.Length)); // ************************************* // Decode Photon Packet Payload // ************************************* switch (cmd_hdr.type) { case CommandType.Acknowledge: // 8 bytes of parms packet.ReadUInt32(); // RecvRelSeqNum packet.ReadUInt32(); // RecvSentTime break; case CommandType.SendUnreliable: packet.ReadUInt32(); // UnRelSeqNum break; case CommandType.SendReliableFragment: // 20 bytes of parms packet.ReadUInt32(); // Frag_start_seq_num packet.ReadUInt32(); // Frag_frag_count packet.ReadUInt32(); // Frag_frag_num packet.ReadUInt32(); // Frag_total_len packet.ReadUInt32(); // Frag_frag_off // TODO: implement fragment reassambly // https://github.com/broderickhyman/photon_spectator/blob/master/fragment_buffer.go break; case CommandType.SendReliable: // **** RELIABLE COMMAND HEADER **** int rel_msg_sig = packet.ReadByte(); // Reliable Message Signature RelMessageTypes rel_msg_type = (RelMessageTypes)packet.ReadByte(); // reliable message type if ((int)rel_msg_type > 128) { // encrypted message types not supported Event_Photon_Info?.Invoke("Decode_Photon: ignoring encrypted message type: " + rel_msg_type.ToString()); break; } // ************************************* // Decode Photon Reliable Message Types // ************************************* switch (rel_msg_type) { case RelMessageTypes.Request: decode_Request(cmd_hdr, packet); break; case RelMessageTypes.EventData: decode_EventData(cmd_hdr, packet); break; case RelMessageTypes.Response: case RelMessageTypes.ResponseAlt: decode_Response(cmd_hdr, packet); break; default: break; } break; case CommandType.Connect: case CommandType.VerifyConnect: case CommandType.Ping: break; default: Event_Photon_Info?.Invoke("Decode_Photon: ignoring unknown command type: " + cmd_hdr.type.ToString()); break; } }