Ejemplo n.º 1
0
        /// <summary>
        /// 解析枚举数据项个数部分
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceiveEnumDataCount(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat("客户端{0}:In ProcessReceiveEnumDataCount", token.Socket.RemoteEndPoint.ToString());
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                for (int i = e.Offset; i < e.BytesTransferred; i++)
                {
                    token.resultBytes.Add(e.Buffer[i]);
                }

                if (token.resultBytes.Count < token.Length)
                {
                    e.SetBuffer(e.Offset, token.Length - token.resultBytes.Count);
                    token.CommandType = CommandType.EnumDataCount;
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    EnumPackage package = SocketHelper.BytesToStruct <EnumPackage>(token.resultBytes.ToArray());
                    package.Count = SocketHelper.NetworkToHost(package.Count);
                    token.resultBytes.Clear();
                    token.MappedFieldCount = package.Count;
                    LogHelper.Log.InfoFormat(
                        "客户端{0}:接收到{1}个枚举数据",
                        token.Socket.RemoteEndPoint.ToString(),
                        token.MappedFieldCount);
                    token.Size = PackgeSize.EnumDataPackageSize * token.MappedFieldCount;
                    if (token.Size > 1024 * 10)
                    {
                        this.CloseClientSocket(e);
                        LogHelper.Log.InfoFormat("非法数据包,大小为:" + token.Size + string.Empty);
                    }
                    else
                    {
                        e.SetBuffer(e.Offset, PackgeSize.EnumDataPackageSize);
                        token.CommandType = CommandType.EnumDataRecord;
                        bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                        if (!willRaiseEvent)
                        {
                            this.ProcessReceive(e);
                        }
                    }
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 处理接收推送数据项的个数部分
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceivePushDataCount(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat("客户端{0}:In ProcessReceivePushDataCount", token.Socket.RemoteEndPoint.ToString());
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                for (int i = e.Offset; i < e.BytesTransferred; i++)
                {
                    token.resultBytes.Add(e.Buffer[i]);
                }

                if (token.resultBytes.Count < token.Length)
                {
                    e.SetBuffer(e.Offset, token.Length - token.resultBytes.Count);
                    token.CommandType = CommandType.PushDataCount;
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    PushHeadPackage package = SocketHelper.BytesToStruct <PushHeadPackage>(token.resultBytes.ToArray());
                    package.Count          = SocketHelper.NetworkToHost(package.Count);
                    token.CollectDataCount = package.Count;
                    LogHelper.Log.InfoFormat(
                        "客户端{0}:接收到{1}个推送数据",
                        token.Socket.RemoteEndPoint.ToString(),
                        token.CollectDataCount);
                    token.resultBytes.Clear();
                    if (package.Count == 0)
                    {
                        token.CommandType = CommandType.PackagePrefix;
                    }
                    else
                    {
                        token.CommandType = CommandType.PushDataRecordHead;
                    }

                    e.SetBuffer(e.Offset, PackgeSize.BasicPackageSize);
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理推送的数据项记录前8个字节获取数据内容的长度
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceivePushDataRecordHead(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat(
                "客户端{0}:In ProcessReceivePushDataRecordHead",
                token.Socket.RemoteEndPoint.ToString());
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                for (int i = e.Offset; i < e.BytesTransferred; i++)
                {
                    token.resultBytes.Add(e.Buffer[i]);
                }

                if (token.resultBytes.Count < token.Length)
                {
                    e.SetBuffer(e.Offset, token.Length - token.resultBytes.Count);
                    token.CommandType = CommandType.PushDataRecordHead;
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    PushBodyPackage package = SocketHelper.BytesToStruct <PushBodyPackage>(token.resultBytes.ToArray());
                    package.DID  = SocketHelper.NetworkToHost(package.DID);
                    package.DLEN = SocketHelper.NetworkToHost(package.DLEN);
                    token.DID    = package.DID;
                    token.Length = package.DLEN;
                    token.DT     = package.DT;
                    token.resultBytes.Clear();
                    token.CommandType = CommandType.PushDataRecordBody;
                    e.SetBuffer(e.Offset, token.Length);
                    LogHelper.Log.InfoFormat(
                        "客户端{0}:接收到数据项值长度为{1}",
                        token.Socket.RemoteEndPoint.ToString(),
                        token.Length);
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 解析8字节命令部分
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceivePackagePrefix(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat("客户端{0}:In ProcessReceivePackagePrefix", token.Socket.RemoteEndPoint.ToString());
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                for (int i = e.Offset; i < e.BytesTransferred; i++)
                {
                    token.resultBytes.Add(e.Buffer[i]);
                }

                if (token.resultBytes.Count < token.Length)
                {
                    e.SetBuffer(e.Offset, token.Length - token.resultBytes.Count);
                    token.CommandType = CommandType.PackagePrefix;
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    BasicPackage package = SocketHelper.BytesToStruct <BasicPackage>(token.resultBytes.ToArray());
                    package.PID = SocketHelper.NetworkToHost(package.PID);
                    package.CID = SocketHelper.NetworkToHost(package.CID);
                    package.VER = SocketHelper.NetworkToHost(package.VER);
                    token.resultBytes.Clear();
                    if (package.VER == FuncCode.ALPVERSION)
                    {
                        token.BasicPackage = package;
                        this.ParseCommand(e);
                    }
                    else
                    {
                        this.CloseClientSocket(e);
                        LogHelper.Log.InfoFormat("协议版本错误,当前版为:{0},需求版本为:{1}", package.VER, FuncCode.ALPVERSION);
                    }
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 解析具体枚举数据项
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceiveEnumDataRecord(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat("客户端{0}:In ProcessReceiveEnumDataRecord", token.Socket.RemoteEndPoint.ToString());
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                for (int i = e.Offset; i < e.BytesTransferred; i++)
                {
                    token.resultBytes.Add(e.Buffer[i]);
                }

                if (token.resultBytes.Count < token.Length)
                {
                    e.SetBuffer(e.Offset, token.Length - token.resultBytes.Count);
                    token.CommandType = CommandType.EnumDataRecord;
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    EnumDataRecordPackage package =
                        SocketHelper.BytesToStruct <EnumDataRecordPackage>(token.resultBytes.ToArray());
                    package.DID = SocketHelper.NetworkToHost(package.DID);
                    token.Machine.Map.MapArray.Add(
                        new MapArray()
                    {
                        Id = package.DID.ToString(), Value = package.NAME
                    });
                    LogHelper.Log.InfoFormat(
                        "客户端{0}:接收枚举数据项编号{1},名称{2}",
                        token.Socket.RemoteEndPoint.ToString(),
                        package.DID.ToString(),
                        package.NAME);
                    token.InsertedCount++;
                    token.resultBytes.Clear();
                    if (token.InsertedCount < token.MappedFieldCount)
                    {
                        e.SetBuffer(e.Offset, PackgeSize.EnumDataPackageSize);
                        token.CommandType = CommandType.EnumDataRecord;
                        bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                        if (!willRaiseEvent)
                        {
                            this.ProcessReceive(e);
                        }
                    }
                    else
                    {
                        token.InsertedCount = 0;
                        if (token.MongoHelper.InsertMappedFiled(token.Machine) == 1)
                        {
                            RequestPushPackage package6 = new RequestPushPackage();
                            package6.PID = SocketHelper.HostToNetwork((ushort)0x0000);
                            package6.CID = SocketHelper.HostToNetwork(FuncCode.PushDataRequest);
                            package6.UID = (byte)token.UserName;
                            package6.ST  = 0;
                            package6.VER = SocketHelper.HostToNetwork((ushort)0x1000);
                            package6.RV  = new byte[8] {
                                0, 0, 0, 0, 0, 0, 0, 0
                            };
                            token.CommandType = CommandType.PushDataRequest;
                            var result = SocketHelper.StructToBytes(package6);
                            e.SetBuffer(e.Offset, PackgeSize.RequestPushPackageSize);
                            Buffer.BlockCopy(result, e.Offset, e.Buffer, 0, PackgeSize.RequestPushPackageSize);
                            LogHelper.Log.InfoFormat(
                                "向客户端{0}:发送推送数据请求报文,长度为{1}",
                                token.Socket.RemoteEndPoint.ToString(),
                                result.Length);
                            bool willRaiseEvent = token.Socket.SendAsync(e);
                            if (!willRaiseEvent)
                            {
                                this.ProcessSend(e);
                            }
                        }
                        else
                        {
                            this.CloseClientSocket(e);
                        }
                    }
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }