Пример #1
0
        public void SendRequest(ExperimentInfo experimentInfo)
        {
            ProtobufTool tool = new ProtobufTool();

            tool.CreatData((int)CommandID.ExperimentInfoRequest, experimentInfo);
            serverConnection.BeginSendMessage(tool);
        }
Пример #2
0
        /// <summary>
        /// 获取协议
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <returns></returns>
        public virtual ProtobufTool GetProtobuf <T>(T info) where T : IMessage
        {
            ProtobufTool tool = new ProtobufTool();

            byte[] data = tool.CreatData(MessageType, info);
            return(tool);
        }
Пример #3
0
        private ProtobufTool GetProtobuf(ProtobufTool data, ExperimentInfo experimentInfo)
        {
            ProtobufTool tool = new ProtobufTool();

            tool.CreatData(data.type, experimentInfo);
            return(tool);
        }
Пример #4
0
        public void BeginSendMessage(ConnectControl connect, int type, IMessage message)
        {
            ProtobufTool protobuf = new ProtobufTool();

            protobuf.CreatData(type, message);

            BeginSendMessage(connect, protobuf);
        }
Пример #5
0
        /// <summary>
        /// 收到消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        protected virtual void Receive(int senderID, ProtobufTool data)
        {
            data.DeSerialize(Proto, data.bytes);

            if (handler != null)
            {
                handler.Invoke(senderID, Proto);
            }
        }
Пример #6
0
        /// <summary>
        /// 发送连接消息
        /// </summary>
        /// <param name="i"></param>
        private void SendConnectMessage(int i)
        {
            ConnectInfo info = new ConnectInfo()
            {
                Id = i
            };

            ProtobufTool protobuf = new ProtobufTool();

            protobuf.CreatData((int)CommandID.Connect, info);
            Broadcast(protobuf, i);
        }
Пример #7
0
 private void SendCallback(IAsyncResult ar)
 {
     try
     {
         ProtobufTool connect = ar.AsyncState as ProtobufTool;
         socket.EndSend(ar);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Пример #8
0
 public void Send(ProtobufTool protobuf)
 {
     try
     {
         socket.BeginSend(protobuf.bytes, 0, protobuf.byteLength, SocketFlags.None, SendCallback
                          , protobuf);
     }
     catch (Exception e)
     {
         UnityEngine.Debug.LogError(e);
         //throw e;
     }
 }
Пример #9
0
 public void BeginSendMessage(ProtobufTool protobuf)
 {
     try
     {
         socket.BeginSend(protobuf.bytes, 0, protobuf.byteLength, SocketFlags.None, (asyncResult) =>
         {
         }, socket);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Пример #10
0
        public ProtobufTool GetHeatBeatProtocol()
        {
            ProtobufTool protobuf = new ProtobufTool();

            hearInfo = new HeartBeatInfo()
            {
                CurrentTime = 0,
                Hostip      = "127.0.0.1"
            };

            protobuf.CreatData((int)CommandID.HeartbeatPacketRequest, hearInfo);

            return(protobuf);
        }
Пример #11
0
        /// <summary>
        /// 执行消息事件
        /// </summary>
        /// <param name="key"></param>
        public void OnMessageEvent(int connectID, ProtobufTool protobuf)
        {
            int key = protobuf.type;

            if (msgEvents.ContainsKey(key))
            {
                msgEvents[key](connectID, protobuf);
            }
            if (onceMsgEvents.ContainsKey(key))
            {
                onceMsgEvents[key](connectID, protobuf);
                onceMsgEvents[key] = null;
                onceMsgEvents.Remove(key);
            }
        }
Пример #12
0
        /// <summary>
        /// 获取协议
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ProtobufTool Read(byte[] data)
        {
            ProtobufTool protobuf = new ProtobufTool();

            using (MemoryStream stream = new MemoryStream(data))
            {
                BinaryReader reader     = new BinaryReader(stream);
                int          dataLength = reader.ReadInt32();               //解析数据长度
                int          typeId     = reader.ReadInt32();               //解析消息类型
                byte[]       pddata     = reader.ReadBytes(dataLength - 4); //4为消息类型占数组长度
                protobuf.type       = typeId;
                protobuf.bytes      = pddata;
                protobuf.byteLength = dataLength;
            }
            return(protobuf);
        }
Пример #13
0
        public void Update()
        {
            //消息
            messageDistribution.Update();

            //心跳
            if (status == ConnectStatus.Connected)
            {
                if (Time.time - lastTickTime > heartBeatTime)
                {
                    ProtobufTool protobuf = GetHeatBeatProtocol();

                    BeginSendMessage(protobuf);
                    lastTickTime = Time.time;
                }
            }
        }
Пример #14
0
        public ExperimentEventControl(ControlTerminal controlTerminal, Action <string> actionlog = null)
        {
            this.controlTerminal = controlTerminal;

            experimentInfo = new ExperimentInfo()
            {
                Id         = 0,
                OwnProject = string.Empty,
                Name       = string.Empty,
                IsBack     = false
            };

            this.actionLog = actionlog;

            //收到请求
            MessageDistributionControl.Instance.AddListener((int)CommandID.ExperimentInfoRequest, (int connectID, ProtobufTool data) =>
            {
                data.DeSerialize(experimentInfo, data.bytes);

                ProtobufTool tool = GetProtobuf(data, experimentInfo);

                controlTerminal.Broadcast(tool);

                if (actionLog != null)
                {
                    actionLog("ExperimentInfoRequest");
                }
            });

            MessageDistributionControl.Instance.AddListener((int)CommandID.ExperimentInfoReceipt, (int connectID, ProtobufTool data) =>
            {
                data.DeSerialize(experimentInfo, data.bytes);

                ProtobufTool tool = GetProtobuf(data, experimentInfo);

                controlTerminal.Broadcast(tool);

                if (actionLog != null)
                {
                    actionLog("发送请求,转发出去:ExperimentInfoReceipt");
                }
            });
        }
Пример #15
0
        private void ProcessData(ConnectControl connect)
        {
            if (connect.bufferCount < sizeof(int) + sizeof(int))
            {
                return;
            }

            //消息长度4个字节,消息类型4个字节
            Array.Copy(connect.buffer, connect.lenBytes, sizeof(int));
            //真实消息长度
            connect.msgLength = BitConverter.ToInt32(connect.lenBytes, 0) - sizeof(int);

            if (connect.bufferCount < connect.msgLength + sizeof(int))
            {
                return;
            }
            try
            {
                ProtobufTool protobuf = proto.Read(connect.buffer);

                //添加到消息集合等待处理
                lock (MessageDistributionControl.Instance.msgList)
                {
                    MessageDistributionControl.Instance.msgList.Add(new ReceiveMessageStruct(connect.id, protobuf));
                }

                //清除已处理的数据
                int count = connect.bufferCount - connect.msgLength - sizeof(int) - sizeof(int);
                Array.Copy(connect.buffer, sizeof(int) + connect.msgLength, connect.buffer, 0, count);
                connect.bufferCount = count;

                if (connect.bufferCount > 0)
                {
                    ProcessData(connect);
                }
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }
        }
Пример #16
0
        public void Broadcast(ProtobufTool protobuf, int?sender = null)
        {
            int senderId = sender.HasValue && sender.Value >= 0 ? sender.Value : -1;

            for (int i = 0; i < connects.Length; i++)
            {
                if (i == senderId)
                {
                    continue;
                }
                if (!connects[i].isUse)
                {
                    continue;
                }

                if (connects[i].socket == null)
                {
                    continue;
                }
                BeginSendMessage(connects[i], protobuf);
            }
        }
Пример #17
0
        /// <summary>
        /// 数据处理
        /// </summary>
        private void ProcessData()
        {
            //如果小于长度字节
            if (bufferCount < sizeof(int) + sizeof(int))
            {
                return;
            }

            //消息长度4个字节,消息类型4个字节
            Array.Copy(readBuffer, lenBytes, sizeof(int));

            msgLength = BitConverter.ToInt32(lenBytes, 0) - sizeof(int);

            if (bufferCount < msgLength + sizeof(int))
            {
                return;
            }

            ProtobufTool proto = protobuf.Read(readBuffer);

            Debug.Log("收到消息:" + (CommandID)proto.type);

            lock (messageDistribution.msgList)
            {
                messageDistribution.msgList.Add(new ReceiveMessageStruct(0, proto));
            }

            //清除已处理的消息
            int count = bufferCount - msgLength - sizeof(int) - sizeof(int);

            Array.Copy(readBuffer, sizeof(int) + msgLength, readBuffer, 0, count);
            bufferCount = count;

            if (bufferCount > 0)
            {
                ProcessData();
            }
        }
Пример #18
0
 public void Send(ProtobufTool protobuf)
 {
     BeginSendMessage(protobuf);
 }
Пример #19
0
 protected override void Receive(int senderID, ProtobufTool data)
 {
     base.Receive(senderID, data);
 }
Пример #20
0
 public void BeginSendMessage(ConnectControl connect, ProtobufTool protobuf)
 {
     connect.Send(protobuf);
 }
 public ReceiveMessageStruct(int connectID, ProtobufTool protobuf)
 {
     this.connectID = connectID;
     this.protobuf  = protobuf;
 }
Пример #22
0
 private void HeartBeatCallback(int connectID, ProtobufTool protobuf)
 {
     lastTickTime = TimeHelper.GetTimeStamp();
 }