Ejemplo n.º 1
0
        public static LoginModel GPSHander(DataHeader dh)
        {
            var    loginModel = new LoginModel();
            string mDate      = string.Empty;
            double lat        = 0;
            double lng        = 0;
            double c          = 3600000;

            #region 数据列解析
            byte[] content = dh.Content;
            //GPS数据包
            byte[] gps = new byte[20];
            Buffer.BlockCopy(content, 62, gps, 0, 20);
            //日期数据解析
            byte[] date = new byte[6];
            Buffer.BlockCopy(gps, 1, date, 0, 6);
            //日期
            mDate = HexTimeToStrIntTime(ByteToString(date).Replace(" ", ""));
            var dateTime = GetDate(mDate);
            //纬度
            byte[] _lat = new byte[4];
            Buffer.BlockCopy(gps, 7, _lat, 0, 4);

            var    newlat = ByteOrderByDes(_lat);
            double a      = hexStrToInt(ByteToString(newlat).Replace(" ", ""));
            lat = a / c;
            //经度
            byte[] _lng = new byte[4];
            Buffer.BlockCopy(gps, 11, _lng, 0, 4);

            var newlng = ByteOrderByDes(_lng);

            double b = hexStrToInt(ByteToString(newlng).Replace(" ", ""));
            lng = b / c;

            //byte[] _speed = new byte[4];
            //Buffer.BlockCopy(gps, 11, _speed, 0, 4);

            #endregion

            loginModel.Date     = dateTime;
            loginModel.lat      = lat;
            loginModel.lng      = lng;
            loginModel.Type     = dh.Type;
            loginModel.DeviceId = dh.DeviceId.ToString().Trim();

            return(loginModel);
        }
Ejemplo n.º 2
0
        private void AcceptConn(IAsyncResult ar)
        {
            try
            {
                //如果服务器停止了服务,就不能再接收新的客户端
                if (!Started)
                {
                    return;
                }

                //缓冲区的大小
                byte[] buffer = new byte[512];

                //获取用户定义的对象,它限定或包含关于异步操作的信息。
                Socket listener = (Socket)ar.AsyncState;
                //异步接受传入的连接尝试,并创建新的 Socket 来处理远程主机通信。
                Socket client = listener.EndAccept(ar);

                Session state = new Session(m_id++, client);
                state.ChatEvent += new EventHandler <ChatEventArgs>(state_ChatEvent);

                //登录认证,只有等ReceiveLoginValidateCallback执行完后才会继续
                LoginValidataDone.Reset();
                client.BeginReceive(buffer, 0, 512, SocketFlags.None, new AsyncCallback(ReceiveLoginValidateCallback), client);
                LoginValidataDone.WaitOne();


                DataHeader dh = new DataHeader(buffer, num);

                IRec irec = new ReceiveData();
                irec.InsertRawData(DataHander.ByteToString(dh.Content).Replace(" ", ""), "");

                Handshake hh = new Handshake(Encoding.UTF8.GetString(buffer, 0, 512));

                //进行一个初步的协议判定,如果不是websocket协议,直接断开,忽略掉
                if (hh.GetValue("Upgrade").ToLower() != "websocket" && hh.GetValue("Upgrade").ToLower() != "")
                {
                    try
                    {
                        client.Shutdown(SocketShutdown.Both); client.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        using (StreamWriter sw = new StreamWriter(@"C:\Logs\job1" + DateTime.Now.ToString("yyyyMMddHH") + ".txt", true, System.Text.Encoding.UTF8))
                        {
                            sw.WriteLine(ex.Message);
                        }
                    }
                    //继续接受客户端
                    m_server.BeginAccept(new AsyncCallback(AcceptConn), m_server);
                    return;
                }
                if (dh.Type == "1001")
                {
                    state.Send(dh.GetLoginResponse());
                    try
                    {
                        var model = DataHander.LoginHander(dh);
                        irec.InsertGPSData(model);
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    //服务器作出握手回应
                    state.Response(hh.Response);
                }


                state.Start();

                //将客户端信息置于列表中
                if (hh.GetValue("Upgrade").ToLower() == "websocket")
                {
                    m_clients.Add(state.DevId, state);
                }


                //继续接受客户端
                m_server.BeginAccept(new AsyncCallback(AcceptConn), m_server);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //设置异步方法接受客户端连接
                m_server.BeginAccept(new AsyncCallback(AcceptConn), m_server);
                using (StreamWriter sw = new StreamWriter(@"C:\Logs\job2" + DateTime.Now.ToString("yyyyMMddHH") + ".txt", true, System.Text.Encoding.UTF8))
                {
                    sw.WriteLine(ex.Message);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 应客户端要求建立连接后的处理线程
        /// </summary>
        /// <param name="ia"></param>
        private void AcceptCallBack(IAsyncResult ar)
        {
            try
            {
                //判断是否是websocket或socket
                //bool isWebsocket = true;
                //定义一个新的客户端对象
                Session state  = (Session)ar.AsyncState;
                Socket  client = state._workSocket;

                #region === 消息处理 ===

                //登录成功,则进入消息等待中
                int RevNumber = 0;

                if (client.Connected)
                {
                    #region === 正常或非正常的断线处理 ===

                    try
                    {
                        RevNumber = client.EndReceive(ar);
                        if (RevNumber == 0)
                        {
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        using (StreamWriter sw = new StreamWriter(@"C:\Logs\job13" + DateTime.Now.ToString("yyyyMMddHH") + ".txt", true, System.Text.Encoding.UTF8))
                        {
                            sw.WriteLine(ex.Message);
                        }
                        return;
                    }

                    #endregion
                    try
                    {
                        DataHeader dh = new DataHeader(RecvDataBuffer, RevNumber);
                        #region 原始数据插入数据库
                        IRec irec1 = new ReceiveData();
                        irec1.InsertRawData(DataHander.ByteToString(dh.Content).Replace(" ", ""), "");
                        #endregion


                        if (dh.IsValid)
                        {
                            IRec irec = new ReceiveData();
                            switch (dh.Type)
                            {
                            case "1001":
                                var model = DataHander.LoginHander(dh);
                                irec.InsertGPSData(model);
                                break;

                            case "4001":
                                var model1 = DataHander.GPSHander(dh);
                                irec.InsertGPSData(model1);
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        using (StreamWriter sw = new StreamWriter(@"C:\Logs\job7" + DateTime.Now.ToString("yyyyMMddHH") + ".txt", true, System.Text.Encoding.UTF8))
                        {
                            sw.WriteLine(ex.Message);
                        }
                    }
                    //继续接收来自来客户端的数据
                    client.BeginReceive(RecvDataBuffer, 0, RecvDataBuffer.Length, SocketFlags.None,
                                        new AsyncCallback(AcceptCallBack), state);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                using (StreamWriter sw = new StreamWriter(@"C:\Logs\job6" + DateTime.Now.ToString("yyyyMMddHH") + ".txt", true, System.Text.Encoding.UTF8))
                {
                    sw.WriteLine(ex.Message);
                }
            }
        }