Exemplo n.º 1
0
        static void OnReceiveLoginRequest(IAsyncResult ar)
        {
            //接收到客户端的登陆请求
            //请求中带有客户端输入的学生ID,本服务端需要和学生信息进行匹配,
            //首先学号查找与学号对应的卡号,然后查找卡号是否已经读取到,如果已经读取到,则广播发送登陆信息给客户端
            //如果卡号尚未读取到,则客户端可以等待,然后读取到卡号的时候会发送登陆信息
            //接收到的数据格式
            // [id,data,epc,data]  对应的正则表达式  \[id,(?<id>\w+),epc,(?<epc>\w{0,})\]   必须要求有ID存在
            try
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint   epSender  = (EndPoint)ipeSender;

                Login_ServerSocket.EndReceiveFrom(ar, ref epSender);

                string strReceived = Encoding.UTF8.GetString(byteData);

                Array.Clear(byteData, 0, byteData.Length);
                Debug.WriteLine("OnReceiveLoginRequest => " + strReceived);

                string          strToBroadcast = string.Empty;
                Regex           regex          = new Regex(@"\[id,(?<id>\w+),epc,(?<epc>\w{0,})\]");
                MatchCollection matches        = regex.Matches(strReceived);
                foreach (Match mc in matches)
                {
                    string epc_match = mc.Groups["epc"].Value;
                    string id_match  = mc.Groups["id"].Value;
                    Debug.WriteLine(string.Format("Match => epc = {0}   student ID = {1}", epc_match, id_match));
                    //根据接收到的id查找绑定的卡号
                    Person person = MemoryTable.getPersonByID(id_match);
                    if (person != null)
                    {
                        string epc = person.epc;
                        //查找是否已经读取到卡号
                        if (epcList.Contains(epc))
                        {
                            string            equipmentID = string.Empty;
                            equipmentPosition ep          = MemoryTable.getEquipmentInfoByEpc(epc);
                            if (ep == null)
                            {
                                ep = MemoryTable.getEquipmentInfoNotUsed();
                            }
                            if (ep != null)
                            {
                                equipmentID = ep.equipmentID;
                            }
                            strToBroadcast = string.Format("[id,{0},epc,{1},equipmentID,{2}]", person.id_num, epc, equipmentID);
                            Broadcast(strToBroadcast);
                        }
                    }
                }

                //Start listening to the message send by the user
                Login_ServerSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                                                    new AsyncCallback(OnReceiveLoginRequest), epSender);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnReceiveRFID => " + ex.Message);
            }
        }
Exemplo n.º 2
0
        static void OnReceiveRFID(IAsyncResult ar)
        {
            try
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint   epSender  = (EndPoint)ipeSender;

                RFID_ServerSocket.EndReceiveFrom(ar, ref epSender);

                string strReceived = Encoding.UTF8.GetString(byteData);

                Array.Clear(byteData, 0, byteData.Length);
                Debug.WriteLine("OnReceiveRFID => " + strReceived);
                //先假设接收到的都是卡号
                //查找学生信息里面有没有该卡号,如果有的话,广播卡号和学号,可以附带未使用的设备号
                //格式为 [epc,id,equipmentID]
                string epc = strReceived.Substring(0, strReceived.IndexOf("\0"));
                epcList.Add(epc);

                string strToBroadcast = string.Empty;
                Person person         = MemoryTable.getPersonByEpc(epc);
                if (person != null)
                {
                    string            equipmentID = string.Empty;
                    equipmentPosition ep          = MemoryTable.getEquipmentInfoByEpc(epc);
                    if (ep == null)
                    {
                        ep = MemoryTable.getEquipmentInfoNotUsed();
                    }
                    if (ep != null)
                    {
                        equipmentID = ep.equipmentID;
                    }
                    strToBroadcast = string.Format("[id,{0},epc,{1},equipmentID,{2}]", person.id_num, epc, equipmentID);
                    //strToBroadcast = string.Format("[id,{0},epc,{1}]", person.id_num, epc);
                    Broadcast(strToBroadcast);

                    //Regex regex = new Regex(@"\[(?<epc>\w+),(?<id>\w+)\]");
                    //MatchCollection matches = regex.Matches(strToBroadcast);
                    //foreach (Match mc in matches)
                    //{
                    //    string epc_match = mc.Groups["epc"].Value;
                    //    string id_match = mc.Groups["id"].Value;
                    //    Debug.WriteLine(string.Format("Match => epc = {0}   student ID = {1}", epc_match, id_match));
                    //}
                    //equipmentPosition ep = MemoryTable.getEquipmentInfoByEpc(epc);
                    //if (ep == null)//学生尚未绑定座位位置,为其选择一个空的位置
                    //{

                    //}
                    //else
                    //{
                    //    strToBroadcast = string.Format("[{0},{1}]", epc, person.id_num);
                    //}
                }
                //Start listening to the message send by the user
                RFID_ServerSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                                                   new AsyncCallback(OnReceiveRFID), epSender);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnReceiveRFID => " + ex.Message);
            }
        }