예제 #1
0
파일: TcpList.cs 프로젝트: chif84/AppQueue
        /// <summary>
        /// Добавление клиента
        /// </summary>
        public bool AddItem(TcpItem item, MessageChannelRequest request)
        {
            lock (internalLock)
            {
                if (item == null || request == null)
                {
                    return(false);
                }

                //отказ если очередь уже слушается
                //if (tcpItemList.Any(i => i.Channels != null && i.Channels.Any(c => request.Channels.Any(rc => rc == c.Name))))
                var connectedItem = tcpItemList.FirstOrDefault(i => i.Channels != null && i.Channels.Any(c => request.Channels.Any(rc => rc == c.Name)));
                if (connectedItem != null)
                {
                    if (connectedItem.IsActived())
                    {
                        return(false);
                    }
                    else
                    {
                        RemoveItem(connectedItem.Uid);
                    }
                }


                //привязка очереди к клиент если доступна
                item.Channels = channelList.Channels.Values.Where(c => request.Channels.Any(rc => rc == c.Name)).ToList();
                item.Channels.ForEach(c => c.Item = item);

                //добавление клиента в список прослушивающих
                tcpItemList.Add(item);

                return(true);
            }
        }
예제 #2
0
파일: TcpList.cs 프로젝트: chif84/AppQueue
        /// <summary>
        /// Начало прослушивания для отправки сообщений
        /// </summary>
        public void ListenList()
        {
            try
            {
                tcpListener = new TcpListener(HostConstants.HostAddress, HostConstants.Port);
                tcpListener.Start();
                Console.WriteLine("await connection");

                while (true)
                {
                    TcpClient tcpClient = tcpListener.AcceptTcpClient();

                    //новые клиенты начинаем слушать в отдельном потоке
                    var clientObject = new TcpItem(tcpClient, this);
                    var clientThread = new Thread(new ThreadStart(clientObject.ListenItem));
                    clientThread.Start();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DisconnectList();
            }
        }