示例#1
0
 private void Listener()
 {
     sqlHelper.CurrConn = Utils.DbType.Local;
     int port = Convert.ToInt32(sqlHelper.ExecuteScalar("select keyport from Interface", null));
     if (port == 0)
     {
         CurrConnSta = ConnSta.NoPort;
         return;
     }
     IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
     BollenSocket serverSocket = new BollenSocket(endPoint);
     if (serverSocket.Listen())
     {
         while (Valid)
         {
             CurrConnSta = ConnSta.Listen;
             try
             {
                 TcpClient clientTcp = serverSocket.TCPListener.AcceptTcpClient();
                 ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveData), clientTcp);
             }
             catch
             {
                 //listening error.
                 break;
             }
         }
     }
 }
示例#2
0
 public FrmMain()
 {
     InitializeComponent();
     clientSocket = null;
     reconn = true;
 }
示例#3
0
        public void ConnectToInterface()
        {
            sqlHelper.CurrConn = Utils.DbType.Local;
            string sql = "select IP, IfPort from Interface";
            DataRow row = sqlHelper.ExecuteDataSet(sql, null,null).Tables[0].Rows[0];
            if (row == null)
                return;
            string ip = row["IP"].ToString();
            int port = Convert.ToInt32(row["IfPort"]);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
            InterfaceClient = new BollenSocket(endPoint);
            try
            {
                if (InterfaceClient.Connect())
                {

                    while (Valid && IsReceive)
                    {
                        CurrInterfaceConnSta = ConnSta.Connected;
                        int size;
                        byte[] buffer = BollenSocket.Read(InterfaceClient.NetStream, out size);

                        if (size == 0)
                        {
                            break;
                        }
                        string rec = Encoding.UTF8.GetString(buffer, 0, size);
                        this.CommLog(rec + "  <- From Pms");

                        //去掉消息前,后缀
                        rec = rec.Replace(InstructMap["PR"],"");
                        rec = rec.Replace(InstructMap["SU"],"");

                        Dictionary<string, string> newInstruct = InstructTransform(rec);
                        if (newInstruct == null)
                        {
                            this.CommLog("Instruct map is empty,Pls configure interface specification.");
                            continue;
                        }
                        string kc, err;
                        Command cmd;
                        if (!VerifyInstruct(newInstruct, out kc, out cmd, out err))
                        {
                            this.CommLog(err);
                            continue;
                        }

                        TcpClient host = null;
                        foreach (TcpClient tcpClient in ClientList.Keys)
                        {
                            if (ClientList[tcpClient] == kc)
                            {
                                host = tcpClient;
                            }
                        }

                        if (host == null)
                        {
                            this.CommLog("[ " + kc + " ]corresponding host not found");
                            continue;
                        }

                        NetworkStream stream = host.GetStream();

                        switch (cmd)
                        {
                            case Command.KR: //issue card
                                PackageInstruct(newInstruct);
                                BollenSocket.Send(stream, new SocketEntity(cmd, Data, "", ""));
                                break;
                            case Command.KD: //delete card
                                BollenSocket.Send(stream, new SocketEntity(cmd, new string[] { }, "", ""));
                                break;
                            case Command.KG: //read card
                                BollenSocket.Send(stream, new SocketEntity(cmd, new string[] { Auth_Code }, "", ""));
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            catch
            {
            }
            InterfaceClient = null;
            CurrInterfaceConnSta = ConnSta.Disconnect;
            SetConnectButtonText("Connect");
        }
示例#4
0
        private void ConnectToServer()
        {
            string workstation = string.Empty;
            reconn = false;
            Dictionary<string,string> content;

            if ((content = CheckKeyContent()) == null)
            {
                reconn = true;
                return;
            }
            byte[] data = new byte[1024];
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(content["IP"]), Convert.ToInt32(content["Port"]));
            clientSocket = new BollenSocket(endPoint);
            if (clientSocket.Connect())
            {
                while (true)
                {
                    SocketEntity entity;
                    try
                    {
                        entity = BollenSocket.Receive(clientSocket.NetStream);
                        if (entity == null)
                        {
                            continue;
                        }

                        string cardNo, err, hex;

                        ShowCardStatus("");

                        switch(entity.Cmd)
                        {
                            case Command.IF:
                                workstation = entity.ClientIP;
                                this.Invoke(new DelegateSetConnectStatus(SetConnectStatus), "Connected to ORBITA server.[" + entity.ServerIP + "]", Status.info);
                                BollenSocket.Send(clientSocket.NetStream,
                                    new SocketEntity(
                                        Command.RQ,
                                        new String[] { this.txtKeyCoderName.Text },
                                        "",
                                        ""));
                                break;
                            case Command.KD:  //delete card
                                if (!myEncoder.DeleteCard(out cardNo, out err, out hex))
                                {
                                    ShowCardStatus(err);
                                }

                                /* key answer data struct
                                 * data[0] key type
                                 * data[1] result
                                 * data[2] card no
                                 * data[3] workstation
                                 * data[4] date time
                                 */
                                BollenSocket.Send(
                                    clientSocket.NetStream,
                                    new SocketEntity(
                                        Command.KA,
                                            new string[] {"KD", hex, cardNo, workstation, DateTime.Now.ToString() },
                                            "", ""
                                ));
                                break;
                            case Command.KR:  //issue card
                                if (!myEncoder.IssueCard(entity.Data,out cardNo, out err, out hex))
                                {
                                    ShowCardStatus(err);
                                }

                                /* key answer data struct
                                 * data[0] key type
                                 * data[1] result
                                 * data[2] guid
                                 * data[3] card no
                                 * data[4] workstation
                                 * data[5] data time
                                 */
                                BollenSocket.Send(
                                    clientSocket.NetStream,
                                    new SocketEntity(
                                        Command.KA,
                                            new string[] {"KR", hex, entity.Data[0]/*guid*/, cardNo, workstation, DateTime.Now.ToString() },
                                            "", ""
                                ));
                                break;
                            case Command.KG: //read card
                                string[] keys = myEncoder.ReadCard(entity.Data[0]/*auth*/, out err, out hex);
                                if (keys == null)
                                {
                                    ShowCardStatus(err);
                                }

                                /* read card data struct
                                 * data[0] Card No
                                 * data[1] Building
                                 * data[2] Room
                                 * data[3] Common Doors
                                 * data[4] Arrival
                                 * data[5] Departure
                                 */
                                string key =
                                    "CTGuest Card" + "|" +
                                    "CN" + keys[0] + "|" +
                                    "BD" + keys[1] + "|" +
                                    "RN" + keys[2] + "|" +
                                    "CD" + keys[3] + "|" +
                                    "AD" + keys[4] + "|" +
                                    "DD" + keys[5];

                                /* key answer data struct
                                 * data[0] key type
                                 * data[1] result
                                 */
                                BollenSocket.Send(
                                        clientSocket.NetStream,
                                        new SocketEntity(
                                            Command.KA,
                                                new string[] {"KG", hex},
                                                "", ""
                                ));
                                break;
                            default:
                                break;
                        }
                    }
                    catch
                    {
                        this.Invoke(new DelegateSetConnectStatus(SetConnectStatus), "Receive Error.", Status.error);
                        break;
                    }
                }
            }

            if (clientSocket == null || !clientSocket.TCPClient.Connected)
            {
                this.Invoke(new DelegateSetConnectStatus(SetConnectStatus), "Not connected to the server.", Status.error);
                reconn = true;
                return;
            }
        }