private void HandleSendReliable(byte[] source, ref int offset, ref int commandLength)
        {
            // Skip 1 byte
            offset++;
            commandLength--;
            ReadByte(out byte messageType, source, ref offset);
            commandLength--;

            int operationLength = commandLength;
            var payload         = new Protocol16Stream(operationLength);

            payload.Write(source, offset, operationLength);
            payload.Seek(0L, SeekOrigin.Begin);

            offset += operationLength;
            switch ((MessageType)messageType)
            {
            case MessageType.OperationRequest:
            {
                OperationRequest requestData = Protocol16Deserializer.DeserializeOperationRequest(payload);
                OnRequest(requestData.OperationCode, requestData.Parameters);
                break;
            }

            case MessageType.OperationResponse:
            {
                OperationResponse responseData = Protocol16Deserializer.DeserializeOperationResponse(payload);
                OnResponse(responseData.OperationCode, responseData.ReturnCode, responseData.DebugMessage, responseData.Parameters);
                break;
            }

            case MessageType.Event:
            {
                EventData eventData = Protocol16Deserializer.DeserializeEventData(payload);
                OnEvent(eventData.Code, eventData.Parameters);
                break;
            }
            }
        }
Example #2
0
        private void HandleSendReliable(byte[] source, ref int offset, ref int commandLength)
        {
            if (commandLength < 0)
            {
                Debug.WriteLine("Something wrong with packet | content=" + Convert.ToBase64String(source));
            }

            byte[] payloadBytes = new byte[commandLength];
            Array.Copy(source, offset, payloadBytes, 0, commandLength);
            // Skip 1 byte
            offset += commandLength;

            int index = 0;

            ReadByte(out byte flag, payloadBytes, ref index);

            bool flag3 = flag != 243 && flag != 253;

            if (flag3)
            {
                return;
            }

            ReadByte(out byte messageDeets, payloadBytes, ref index);
            byte messageType = (byte)(messageDeets & 127);
            bool special     = (messageDeets & 128) > 0;

            Protocol16Stream payload;

            if (!special)
            {
                int operationLength = payloadBytes.Length - index;
                payload = new Protocol16Stream(operationLength);
                payload.Write(payloadBytes, index, operationLength);
                payload.Seek(0L, SeekOrigin.Begin);
            }
            else
            {
                byte[] actualContent = new byte[payloadBytes.Length - index];
                Array.Copy(payloadBytes, index, actualContent, 0, actualContent.Length);
                byte[] result = OnSpecial(actualContent);

                if (result.Length == 0)
                {
                    return;
                }

                int operationLength = result.Length;
                payload = new Protocol16Stream(operationLength);
                payload.Write(result, 0, operationLength);
                payload.Seek(0L, SeekOrigin.Begin);
            }

            switch ((MessageType)messageType)
            {
            case MessageType.OperationRequest:
            {
                OperationRequest requestData = Protocol16Deserializer.DeserializeOperationRequest(payload);
                OnRequest(requestData.OperationCode, requestData.Parameters);
                break;
            }

            case MessageType.OperationResponse:
            {
                OperationResponse responseData = Protocol16Deserializer.DeserializeOperationResponse(payload);
                OnResponse(responseData.OperationCode, responseData.ReturnCode, responseData.DebugMessage, responseData.Parameters);
                break;
            }

            case MessageType.Event:
            {
                EventData eventData = Protocol16Deserializer.DeserializeEventData(payload);
                OnEvent(eventData.Code, eventData.Parameters);
                break;
            }

            case MessageType.InternalOperationRequest:
            {
                OperationRequest intReqData = Protocol16Deserializer.DeserializeOperationRequest(payload);
                intReqData.Parameters.Add(88, TickCount);
                OnRequest(intReqData.OperationCode, intReqData.Parameters);
                break;
            }

            case MessageType.InternalOperationResponse:
            {
                OperationResponse intResponseData = Protocol16Deserializer.DeserializeOperationResponse(payload);
                OnResponse(intResponseData.OperationCode, intResponseData.ReturnCode, intResponseData.DebugMessage, intResponseData.Parameters);
                break;
            }
            }
        }