コード例 #1
0
        public void OnConnectorDisconnectHandler(object sender, object disconnectionState)
        {
            IConnector connector = sender as IConnector;

            if (connector == null)
            {
                return;
            }

            connector.DisconnectEvent -= OnConnectorDisconnectHandler;

            TeamPainterTCPClient client = null;

            lock (_cSync)
            {
                long clientID;
                _clientConnectionAssociation.TryGetAndRemove(connector.UniqueID, out clientID);
                _clients.TryGetAndRemove(clientID, out client);
            }
            if (client != null)
            {
                client.Connector.DisconnectEvent  -= OnConnectorDisconnectHandler;
                client.Connector.ReceiveAction    -= OnReceveDataHandler;
                client.Connector.ReceiveComAction -= OnReceiveCommandHandler;
            }
            NetLogger.Log(connector.UniqueID + " is disconnected");

            DisconnectEvent?.Invoke(client, disconnectionState);
        }
コード例 #2
0
        private void miInfo_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = null;

            lvClients.Invoke((MethodInvoker) delegate
            {
                if (lvClients.SelectedItems != null)
                {
                    if (lvClients.SelectedItems != null && lvClients.SelectedItems.Count > 0)
                    {
                        lvi = lvClients.SelectedItems[0];
                    }
                }
            });

            if (lvi == null)
            {
                return;
            }
            long id = 0; long.TryParse(lvi.Name, out id);

            if (id != 0)
            {
                string s = lvi.SubItems[1].Text;
                if (string.IsNullOrEmpty(s) || s == "No autorized")
                {
                    return;
                }
                TeamPainterTCPClient client = _worker.GetClient(id);
                if (client != null)
                {
                    client.ShowInfo();
                }
            }
        }
コード例 #3
0
        private void NCAutorizeNewClient(IConnector connector, ArgComAutorize arg)
        {
            if (arg == null || connector == null)
            {
                return;
            }

            ATCPConnector atcpcon = connector as ATCPConnector;

            if (atcpcon == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(arg.Login) && arg.Password == "Password")
            {
                _autorizator.RemoveConnector(connector.UniqueID);

                ArgUniqueID argClientID = new ArgUniqueID(NComSendClientID.uniqueID);
                argClientID.UniqueID = Uid64.CreateNewSync().Data;
                TeamPainterTCPClient client = new TeamPainterTCPClient(atcpcon, argClientID.UniqueID, arg.Login);

                bool added = _worker.AddClient(client);
                if (!added)
                {
                    client.Disconnect(); return;
                }

                lvClients.Invoke((MethodInvoker) delegate
                {
                    ListViewItem lvi = lvClients.Items[client.Connector.UniqueID.ToString()];
                    if (lvi != null)
                    {
                        lvi.Name             = client.UniqueID.ToString();
                        lvi.SubItems[0].Text = client.UniqueID.ToString();
                        lvi.SubItems[1].Text = arg.Login;

                        lvi.BackColor = Color.WhiteSmoke;
                        lvi.ForeColor = Color.Black;

                        lvClients.Invalidate();
                    }
                });

                connector.SendCommand(argClientID);
            }
        }
コード例 #4
0
        private void miDisconnect_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = null;

            lvClients.Invoke((MethodInvoker) delegate
            {
                if (lvClients.SelectedItems != null)
                {
                    if (lvClients.SelectedItems != null && lvClients.SelectedItems.Count > 0)
                    {
                        lvi = lvClients.SelectedItems[0];
                    }
                }
            });

            if (lvi == null)
            {
                return;
            }
            long id = 0; long.TryParse(lvi.Name, out id);

            if (id != 0)
            {
                string s = lvi.SubItems[1].Text;
                if (s == "No autorized")
                {
                    IConnector con = _autorizator.GetConnector(id);
                    if (con != null)
                    {
                        con.Disconnect();
                    }
                }
                else
                {
                    TeamPainterTCPClient client = _worker.GetClient(id);
                    if (client != null)
                    {
                        client.Disconnect();
                    }
                }
            }
        }
コード例 #5
0
        public bool AddClient(TeamPainterTCPClient client)
        {
            if (client == null || client.Connector == null)
            {
                return(false);
            }
            long connectionID = client.Connector.UniqueID;

            lock (_cSync)
            {
                TeamPainterTCPClient regcl;
                _clients.TryGetValue(client.UniqueID, out regcl);

                if (regcl != null)
                {
                    NetLogger.Log("This client is already exist");
                    return(false);
                }

                if (_clients.Count >= _maxClients)
                {
                    NetLogger.Log("Max connection");
                    return(false);
                }

                _clientConnectionAssociation[connectionID] = client.UniqueID;
                _clients[client.UniqueID] = client;

                client.Connector.DisconnectEvent += OnConnectorDisconnectHandler;

                client.Connector.ReceiveAction     = null;
                client.Connector.ReceiveAction    += OnReceveDataHandler;
                client.Connector.ReceiveComAction  = null;
                client.Connector.ReceiveComAction += OnReceiveCommandHandler;
                return(true);
            }
        }
コード例 #6
0
        private void TickSendCommandsToAllClients()
        {
            List <ICommandArg> args = null;

            lock (_astSync)
            {
                args = _argsSendToAllClients;
                _argsSendToAllClients = new List <ICommandArg>();
            }

            if (args != null)
            {
                TeamPainterTCPClient[] clients = _worker.GetAllClients();
                if (clients == null || clients.Length == 0)
                {
                    return;
                }

                for (int i = 0; i < clients.Length; i++)
                {
                    TeamPainterTCPClient cl = clients[i];
                    cl.Traffic.Update(AppTime.Time);
                    if (cl == null || !cl.Syncronized)
                    {
                        continue;                                //remove this
                    }
                    if (args.Count == 0)
                    {
                        cl.Connector.Send(null, null); continue;
                    }
                    for (int c = 0; c < args.Count; c++)
                    {
                        cl.Connector.SendCommand(args[c]);
                    }
                }
            }
        }