コード例 #1
0
        private void ReadConnectionStream(byte[] buffer)
        {
            if (_protocol.ReadConnectionStream(buffer))
            {
                byte[] remain = _protocol.StripRemainByteArray();

                Type type = GetTypeByCommand(_protocol.command);

                object message;
                if (type != null)
                {
                    // Deserialize Message
                    MemoryStream stream = new MemoryStream(_protocol.message);
                    message = Serializer.NonGeneric.Deserialize(type, stream);

                    Log(string.Format("read << {0} type:{1} \n {2}", _protocol.FormattedString(), type, (message as ProtoBuf.IExtensible).FormattedString()));
//					Log (string.Format ("raw_bytes: \n {0}", _protocol.message.ToHexString ()));
                }
                else
                {
                    // Extract Message Raw Bytes
                    Log(string.Format("read << {0} type:RAW_BYTES \n ${1}", _protocol.FormattedString(), _protocol.message.ToHexString()));
                    message = _protocol.message.Clone();
                }

                TriggerHandlesWithMessage(_protocol.command, message);

                _protocol.Clear();
                if (remain != null)
                {
                    ReadConnectionStream(remain);
                }
            }
        }
コード例 #2
0
        public void Send <T>(ushort command, T message) where T : ProtoBuf.IExtensible
        {
            if (!Connected)
            {
                Debug.LogError("Client's not connected!");
                return;
            }

            // Setup Protocol Head
            ProtocolPackage protocol = new ProtocolPackage();

            protocol.command = command;
            protocol.uin     = uin;
            protocol.index   = ++_sequence;

            // Serialize Message
            MemoryStream stream = new MemoryStream();

            Serializer.Serialize <T> (stream, message);

            byte[] data = protocol.EncodePackage(stream.ToArray());

            // Log Raw Bytes of Sent Message
            Log(string.Format("send >> {0} \n {1}", protocol.FormattedString(), message.FormattedString()));
//			Log (string.Format ("raw_bytes: \n {0}", data.ToHexString ()));

            ApolloResult result;

            if (_type == ProtocolType.TCP)
            {
                result = _connector.WriteData(data);
            }
            else
            {
                result = _connector.WriteUdpData(data);
            }

            DispatchConnectEvent(ConnectEventType.SEND, result);
        }