Пример #1
0
        /// <summary>
        /// 设备登录
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceiveLogin(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat("客户端{0}:In ProcessReceiveLogin", token.Socket.RemoteEndPoint.ToString());
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                if (token.BasicPackage.ST == 0)
                {
                    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.Login;
                        bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                        if (!willRaiseEvent)
                        {
                            this.ProcessReceive(e);
                        }
                    }
                    else
                    {
                        token.IsLogined = true;
                        token.resultBytes.Clear();
                        BasicPackage package5 = new BasicPackage();
                        package5.PID      = SocketHelper.HostToNetwork((ushort)0x0000);
                        package5.CID      = SocketHelper.HostToNetwork(FuncCode.EnumData);
                        package5.ST       = 0;
                        package5.UID      = Convert.ToByte(token.UserName);
                        package5.VER      = SocketHelper.HostToNetwork((ushort)0x1000);
                        token.CommandType = CommandType.EnumData;
                        var result = SocketHelper.StructToBytes(package5);
                        e.SetBuffer(e.Offset, 8);
                        Buffer.BlockCopy(result, e.Offset, e.Buffer, 0, 8);
                        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);
            }
        }
Пример #2
0
 public AsyncUserToken()
 {
     this.Machine      = new Machine();
     this.DataItems    = new List <DataItem>();
     this.BasicPackage = new BasicPackage();
     this.resultBytes  = new List <byte>();
     this.CommandType  = CommandType.PackagePrefix;
     this.MongoHelper  = new MongoHelper();
 }
Пример #3
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);
            }
        }
Пример #4
0
        /// <summary>
        /// 获取数据项的值部分
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceivePushDataRecordBody(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat(
                "客户端{0}:In ProcessReceivePushDataRecordBody",
                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.PushDataRecordBody;
                    bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    token.Value = token.resultBytes.ToArray();
                    var item = new DataItem()
                    {
                        Id = token.DID, Type = token.DT, Value = token.Value
                    };
                    LogHelper.Log.InfoFormat(
                        "客户端{0}:接收到数据项编号 - {1},类型 - {2},值 - {3}",
                        token.Socket.RemoteEndPoint.ToString(),
                        token.DID,
                        token.DT,
                        SocketHelper.GetValueByType(item));
                    token.DataItems.Add(item);
                    token.InsertedCount++;
                    token.resultBytes.Clear();
                    if (token.InsertedCount < token.CollectDataCount)
                    {
                        token.CommandType = CommandType.PushDataRecordHead;
                        e.SetBuffer(e.Offset, PackgeSize.BasicPackageSize);
                        bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                        if (!willRaiseEvent)
                        {
                            this.ProcessReceive(e);
                        }
                    }
                    else
                    {
                        if (token.MongoHelper.InsertValueToMongo(token.Machine, token.DataItems) == 1)
                        {
                            LogHelper.Log.InfoFormat(
                                "客户端{0}:所有推送数据已接收,等待下一次推送",
                                token.Socket.RemoteEndPoint.ToString());
                            token.DataItems.Clear();
                            token.InsertedCount = 0;
                            LogHelper.Log.InfoFormat("客户端{0}:发送推送数据回复", token.Socket.RemoteEndPoint.ToString());
                            token.CommandType = CommandType.PushDataReply;
                            BasicPackage package8 = new BasicPackage();
                            package8.PID = SocketHelper.HostToNetwork((ushort)0x0000);
                            package8.CID = SocketHelper.HostToNetwork(FuncCode.PushDataReply);
                            package8.ST  = 0;
                            package8.UID = Convert.ToByte(token.UserName);
                            package8.VER = SocketHelper.HostToNetwork((ushort)0x1000);
                            var result = SocketHelper.StructToBytes(package8);
                            e.SetBuffer(e.Offset, 8);
                            Buffer.BlockCopy(result, e.Offset, e.Buffer, 0, 8);
                            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);
            }
        }
Пример #5
0
        /// <summary>
        /// 解析命令
        /// </summary>
        /// <param name="token"></param>
        private void ParseCommand(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            LogHelper.Log.InfoFormat("客户端{0}:In ParseCommand", token.Socket.RemoteEndPoint.ToString());
            if (e.SocketError == SocketError.Success)
            {
                bool willRaiseEvent = false;
                switch (token.BasicPackage.CID)
                {
                case FuncCode.Bootstrap:
                    e.SetBuffer(e.Offset, PackgeSize.MacBootstrapPackageSize);
                    token.CommandType = CommandType.Bootstrap;
                    willRaiseEvent    = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }

                    break;

                case FuncCode.Login:
                    e.SetBuffer(e.Offset, PackgeSize.LoginPackageSize - PackgeSize.BasicPackageSize);
                    token.CommandType = CommandType.Login;
                    willRaiseEvent    = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }

                    break;

                case FuncCode.EnumData:
                    e.SetBuffer(e.Offset, PackgeSize.LoginPackageSize - PackgeSize.BasicPackageSize);
                    token.CommandType = CommandType.EnumDataCount;
                    willRaiseEvent    = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }

                    break;

                case FuncCode.PushDataRequest:
                    e.SetBuffer(e.Offset, PackgeSize.LoginPackageSize - PackgeSize.BasicPackageSize);
                    token.CommandType = CommandType.PushDataRequest;
                    willRaiseEvent    = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }

                    break;

                case FuncCode.PushData:
                    e.SetBuffer(e.Offset, PackgeSize.LoginPackageSize - PackgeSize.BasicPackageSize);
                    token.CommandType = CommandType.PushDataCount;
                    willRaiseEvent    = token.Socket.ReceiveAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessReceive(e);
                    }

                    break;

                case FuncCode.NullCommand:
                    BasicPackage package7 = new BasicPackage();
                    package7.PID = SocketHelper.HostToNetwork((ushort)0x0000);
                    package7.CID = SocketHelper.HostToNetwork(FuncCode.NullCommandReply);
                    package7.ST  = 0;
                    package7.UID = Convert.ToByte(token.UserName);
                    package7.VER = SocketHelper.HostToNetwork((ushort)0x1000);
                    var result = SocketHelper.StructToBytes(package7);
                    token.CommandType = CommandType.NullCommand;
                    e.SetBuffer(e.Offset, 8);
                    Buffer.BlockCopy(result, e.Offset, e.Buffer, 0, 8);
                    LogHelper.Log.InfoFormat(
                        "向客户端{0}发送心跳包回复报文,长度为{1}",
                        token.Socket.RemoteEndPoint.ToString(),
                        result.Length);
                    willRaiseEvent = token.Socket.SendAsync(e);
                    if (!willRaiseEvent)
                    {
                        this.ProcessSend(e);
                    }

                    break;

                case FuncCode.Close:
                    this.CloseClientSocket(e);
                    break;

                default: break;
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }