예제 #1
0
        public void BeginReceiveMessage(ConnectControl connect)
        {
            connect.socket.BeginReceive(connect.buffer, connect.bufferCount, connect.BUffRemain(), SocketFlags.None, (asyncResult) =>
            {
                ConnectControl connectControl = null;

                connectControl = asyncResult.AsyncState as ConnectControl;
                lock (connectControl)
                {
                    try
                    {
                        int count = connectControl.socket.EndReceive(asyncResult);
                        if (count == 0)
                        {
                            connectControl.Close();
                            return;
                        }

                        connectControl.bufferCount += count;
                        ProcessData(connectControl);

                        BeginReceiveMessage(connectControl);
                    }
                    catch (Exception e)
                    {
                        if (connectControl != null)
                        {
                            connectControl.Close();
                        }

                        //throw e;
                    }
                }
            }, connect);
        }
예제 #2
0
        private void AcceptCallback(IAsyncResult asyncResult)
        {
            try
            {
                Socket toClientSocket = controlSocket.EndAccept(asyncResult);

                int i = NewIndex();

                if (i < 0)
                {
                    toClientSocket.Close();
                }
                else
                {
                    ConnectControl connect = connects[i];
                    connect.Initialize(toClientSocket, i);

                    string ip = connect.GetAddress();

                    UnityEngine.Debug.Log(ip + "已连接");
                    SendConnectMessage(i);
                    BeginReceiveMessage(connect);
                }

                controlSocket.BeginAccept(AcceptCallback, null);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
예제 #3
0
        public void BeginSendMessage(ConnectControl connect, int type, IMessage message)
        {
            ProtobufTool protobuf = new ProtobufTool();

            protobuf.CreatData(type, message);

            BeginSendMessage(connect, protobuf);
        }
예제 #4
0
 private void CreateConnects()
 {
     connects = new ConnectControl[maxConnect];
     for (int i = 0; i < maxConnect; i++)
     {
         connects[i] = new ConnectControl();
     }
 }
예제 #5
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);
            }
        }
예제 #6
0
        public void Close()
        {
            for (int i = 0; i < connects.Length; i++)
            {
                ConnectControl connect = connects[i];

                if (connect == null)
                {
                    continue;
                }
                if (!connect.isUse)
                {
                    continue;
                }

                lock (connect)
                {
                    connect.Close();
                }
            }
        }
예제 #7
0
        public int NewIndex()
        {
            if (connects == null)
            {
                return(-1);
            }

            for (int i = 0; i < connects.Length; i++)
            {
                if (connects[i] == null)
                {
                    connects[i] = new ConnectControl();
                    return(i);
                }

                if (!connects[i].isUse)
                {
                    return(i);
                }
            }

            return(-1);
        }
        private void HeartBeat()
        {
            long timeNow = TimeHelper.GetTimeStamp();

            for (int i = 0; i < controlTerminal.connects.Length; i++)
            {
                ConnectControl connect = controlTerminal.connects[i];
                if (connect == null)
                {
                    continue;
                }
                if (!connect.isUse)
                {
                    continue;
                }
                if (connect.lastTickTime < timeNow - heartBeatTime)
                {
                    lock (connect)
                    {
                        connect.Close();
                    }
                }
            }
        }
예제 #9
0
 public void BeginSendMessage(ConnectControl connect, ProtobufTool protobuf)
 {
     connect.Send(protobuf);
 }