Ejemplo n.º 1
0
        private void ProcessDeviceCDM1Command(IWebSocketConnection connection, CmdUnit unit)
        {
            try
            {
                CDM1Unit d10 = (CDM1Unit)unit;

                CDM1Unit ret = null;

                CDM1Device device = new CDM1Device();

                device.delegateSendErrorInfo  = new ECardDevice.SendErrorInfoDelegate(ShowCustInfoPro);
                device.delegateSendReportInfo = new ECardDevice.SendReportInfoDelegate(ShowCustInfoPro);


                ret = device.SendCommand(unit);

                string retBuffer = System.Text.Encoding.UTF8.GetString(ret.LoadData());
                // connection.sendData(retBuffer);
                connection.Send(retBuffer);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        private void MenuItem_CmdAdd_Click(object sender, RoutedEventArgs e)
        {
            using (CmdInputDialog input = new CmdInputDialog()) {
                input.Owner = this;
                input.Title = "Add";
                input.comboBoxContentMode.ItemsSource   = Enum.GetNames(typeof(ServiceRequestContentMode));
                input.comboBoxContentMode.SelectedIndex = 1;
                input.textBoxID.Focus();

                if (input.ShowDialog() == false)
                {
                    return;
                }

                CmdUnit cmd = new CmdUnit();
                cmd.ID          = input.textBoxID.Text;
                cmd.Name        = input.textBoxName.Text;
                cmd.Cmd         = input.textBoxCmd.Text;
                cmd.Encrypt     = input.checkBoxEncrypt.IsChecked == true ? true : false;
                cmd.ContentMode = (ServiceRequestContentMode)Enum.Parse(typeof(ServiceRequestContentMode), input.comboBoxContentMode.SelectedItem.ToString());
                uidata.CmdTable.Add(cmd);
            }
        }
Ejemplo n.º 3
0
        public void Config()
        {
            if (File.Exists(BASE_DIR + CONF_NAME) == false)
            {
                System.Windows.MessageBox.Show(CONF_NAME + ": can't find it.");
                return;
            }

            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(BASE_DIR + CONF_NAME);

                // cmdtable
                foreach (XmlNode item in doc.SelectNodes("/configuration/commands/cmditem"))
                {
                    CmdUnit cmd = new CmdUnit();
                    cmd.ID          = item.Attributes["id"].Value;
                    cmd.Name        = item.Attributes["name"].Value;
                    cmd.Cmd         = item.Attributes["content"].Value;
                    cmd.Encrypt     = bool.Parse(item.Attributes["encrypt"].Value);
                    cmd.ContentMode = (ServiceRequestContentMode)Enum.Parse(typeof(ServiceRequestContentMode), item.Attributes["content-mode"].Value);
                    uidata.CmdTable.Add(cmd);
                }

                /// sockunit
                foreach (XmlNode item in doc.SelectNodes("/configuration/sockets/sockitem"))
                {
                    string[]   str      = item.Attributes["ep"].Value.Split(':');
                    IPEndPoint ep       = new IPEndPoint(IPAddress.Parse(str[0]), int.Parse(str[1]));
                    SockType   sockType = (SockType)Enum.Parse(typeof(SockType), item.Attributes["type"].Value);
                    SockUnit   sockUnit = new SockUnit()
                    {
                        ID      = item.Attributes["id"].Value,
                        Name    = item.Attributes["name"].Value,
                        Type    = sockType,
                        Lep     = sockType == SockType.listen ? ep : null,
                        Rep     = sockType == SockType.connect ? ep : null,
                        State   = SockState.Closed,
                        Autorun = bool.Parse(item.Attributes["autorun"].Value),
                    };
                    uidata.AddSockUnit(sockUnit);

                    if (sockUnit.Autorun)
                    {
                        string id = "service.sesslisten";
                        if (sockType == SockType.connect)
                        {
                            id = "service.sessconnect";
                        }
                        object req = new {
                            id   = id,
                            ip   = ep.Address.ToString(),
                            port = ep.Port,
                        };
                        core.AddServiceRequest(ServiceRequest.Parse(JsonConvert.SerializeObject(req)));
                    }
                }
            } catch (Exception) {
                System.Windows.MessageBox.Show(CONF_NAME + ": syntax error.");
            }
        }
Ejemplo n.º 4
0
        private bool openWebSokcet()
        {
            try
            {
                allSockets = new List <IWebSocketConnection>();

                webSocketServer = new WebSocketServer("ws://0.0.0.0:" + _workSpace.ListenPort.ToString());

                webSocketServer.Start(socket =>
                {
                    //接收线程创建
                    socket.OnOpen = () =>
                    {
                        ShowCustInfoPro(socket.ConnectionInfo.ClientIpAddress.ToString() + "已连接");

                        if (allSockets.Count >= 1)
                        {
                            return;
                        }
                        ShowClientLinkInfo(socket.ConnectionInfo.ClientIpAddress.ToString());
                        allSockets.Add(socket);
                    };
                    //线程关闭
                    socket.OnClose = () =>
                    {
                        ShowCustInfoPro(socket.ConnectionInfo.ClientIpAddress.ToString() + "已断开");

                        allSockets.Remove(socket);

                        ShowClientLinkInfo(String.Empty);
                    };
                    socket.OnMessage = message =>
                    {
                        CmdUnit unit = ECardDevice.CreateDeviceUnit(message);

                        switch (unit.Type)
                        {
                        case (int)CDDeviceType.DCD10:

                            ProcessDeviceDCD10Command(allSockets[0], unit);

                            break;

                        case (int)CDDeviceType.CD_M1:
                            ProcessDeviceCDM1Command(allSockets[0], unit);

                            break;

                        default:
                            break;
                        }
                    };
                });


                ShowCustInfoPro("服务已开启");

                return(true);
            }
            catch
            {
                throw;
            }
        }