Beispiel #1
0
 public void RemoveCryptor(MessageCryptoType cryptoType)
 {
     if (cryptorDic.ContainsKey(cryptoType))
     {
         cryptorDic.Remove(cryptoType);
     }
 }
Beispiel #2
0
 public void AddCryptor(MessageCryptoType cryptoType, IMessageCryptor cryptor)
 {
     if (!cryptorDic.ContainsKey(cryptoType))
     {
         cryptorDic.Add(cryptoType, cryptor);
     }
 }
Beispiel #3
0
        private void Desrialize(byte[] bytes, out int messageID, out byte seriousIndex, out byte[] dataBytes)
        {
            int offsetIndex = 0;

            messageID    = BitConverter.ToInt32(bytes, offsetIndex);
            offsetIndex += sizeof(int);

            seriousIndex = bytes[offsetIndex];
            offsetIndex += sizeof(byte);

            if (bytes.Length - 1 > offsetIndex)
            {
                MessageCompressType compressType = (MessageCompressType)bytes[offsetIndex];
                offsetIndex += sizeof(byte);
                MessageCryptoType cryptoType = (MessageCryptoType)bytes[offsetIndex];
                offsetIndex += sizeof(byte);

                dataBytes = new byte[bytes.Length - offsetIndex];
                Array.Copy(bytes, offsetIndex, dataBytes, 0, dataBytes.Length);

                dataBytes = netHandler.DecodeMessage(cryptoType, dataBytes);
                dataBytes = netHandler.UncompressMessage(compressType, dataBytes);
            }
            else
            {
                dataBytes = null;
            }
        }
Beispiel #4
0
        private void Serialize(int messageID, MessageCompressType compressType, MessageCryptoType cryptoType, byte[] dataBytes)
        {
            byte[] bodyBytes = dataBytes;
            if (dataBytes != null && dataBytes.Length > 0)
            {
                bodyBytes = netHandler.CompressMessage(compressType, bodyBytes);
                bodyBytes = netHandler.EncodeMessage(cryptoType, bodyBytes);
            }

            int messageLen = sizeof(int) + sizeof(byte);

            if (bodyBytes != null && bodyBytes.Length > 0)
            {
                messageLen += sizeof(byte);
                messageLen += sizeof(byte);
                messageLen += bodyBytes.Length;
            }
            byte[] messageLenBytes = BitConverter.GetBytes(messageLen);
            sendingMessageStream.Write(messageLenBytes, 0, messageLenBytes.Length);
            byte[] messageIDBytes = BitConverter.GetBytes(messageID);
            sendingMessageStream.Write(messageIDBytes, 0, messageIDBytes.Length);
            sendingMessageStream.WriteByte(sendedMessageSeriousIndex);
            if (bodyBytes != null && bodyBytes.Length > 0)
            {
                sendingMessageStream.WriteByte((byte)compressType);
                sendingMessageStream.WriteByte((byte)cryptoType);
                sendingMessageStream.Write(bodyBytes, 0, bodyBytes.Length);
            }

            ++sendedMessageSeriousIndex;
        }
Beispiel #5
0
 public byte[] EncodeMessage(MessageCryptoType cryptoType, byte[] dataBytes)
 {
     if (cryptorDic.TryGetValue(cryptoType, out var cryptor))
     {
         return(cryptor.Encode(dataBytes));
     }
     return(dataBytes);
 }
Beispiel #6
0
        public void Send(int messageID, MessageCompressType compressType, MessageCryptoType cryptoType, byte[] dataBytes)
        {
            if (!IsConnected)
            {
                return;
            }

            lock (sendingMessageStreamLocker)
            {
                Serialize(messageID, compressType, cryptoType, dataBytes);
            }

            DoSend();
        }
Beispiel #7
0
        public void SendMessage(int messageID, MessageCompressType compressType, MessageCryptoType cryptoType, byte[] dataBytes)
        {
            lock (cachedWillSendMessageLocker)
            {
                var willSendMessage = willSendMessagePool.Get();
                willSendMessage.ID           = messageID;
                willSendMessage.CompressType = compressType;
                willSendMessage.CryptoType   = cryptoType;
                willSendMessage.DataBytes    = dataBytes;

                cachedWillSendMessages.Add(willSendMessage);
            }

            DoSend();
        }
Beispiel #8
0
        public byte[] Serialize(int messageID, MessageCompressType compressType, MessageCryptoType cryptoType, byte[] dataBytes)
        {
            serializeMessageStream.Clear();

            byte[] bodyBytes = dataBytes;
            if (dataBytes != null && dataBytes.Length > 0)
            {
                if (compressorDic.TryGetValue(compressType, out var compressor))
                {
                    bodyBytes = compressor.Compress(bodyBytes);
                }
                if (cryptorDic.TryGetValue(cryptoType, out var cryptor))
                {
                    bodyBytes = cryptor.Encode(bodyBytes);
                }
            }

            ++serializedSeriousIndex;

            int messageLen = sizeof(int) + sizeof(byte);

            if (bodyBytes != null && bodyBytes.Length > 0)
            {
                messageLen += sizeof(byte);
                messageLen += sizeof(byte);
                messageLen += bodyBytes.Length;
            }
            byte[] messageLenBytes = BitConverter.GetBytes(messageLen);
            serializeMessageStream.Write(messageLenBytes, 0, messageLenBytes.Length);
            byte[] messageIDBytes = BitConverter.GetBytes(messageID);
            serializeMessageStream.Write(messageIDBytes, 0, messageIDBytes.Length);
            serializeMessageStream.WriteByte(serializedSeriousIndex);
            if (bodyBytes != null && bodyBytes.Length > 0)
            {
                serializeMessageStream.WriteByte((byte)compressType);
                serializeMessageStream.WriteByte((byte)cryptoType);
                serializeMessageStream.Write(bodyBytes, 0, bodyBytes.Length);
            }

            return(serializeMessageStream.ToArray());
        }
Beispiel #9
0
        public byte Desrialize(byte[] bytes, out int messageID, out byte[] dataBytes)
        {
            dataBytes = null;

            int offsetIndex = 0;

            messageID    = BitConverter.ToInt32(bytes, offsetIndex);
            offsetIndex += sizeof(int);
            byte seriousIndex = bytes[offsetIndex];

            offsetIndex += sizeof(byte);

            if (bytes.Length - 1 > offsetIndex)
            {
                MessageCompressType compressType = (MessageCompressType)bytes[offsetIndex];
                offsetIndex += sizeof(byte);
                MessageCryptoType cryptoType = (MessageCryptoType)bytes[offsetIndex];
                offsetIndex += sizeof(byte);

                dataBytes = new byte[bytes.Length - offsetIndex];
                Array.Copy(bytes, offsetIndex, dataBytes, 0, dataBytes.Length);
            }
            return(seriousIndex);
        }