Esempio n. 1
0
        /// <summary>
        /// Accept connections
        /// </summary>
        /// <returns></returns>
        private async Task AcceptConnections()
        {
            listener = new TcpListener(Server.Address, Server.Port);
            var bufferSize = Buffer; // Get the current buffer size on start

            listener.Start();
            Running = true;

            // If there is an exception we want to output the message to the console for debugging
            try
            {
                // While the Running bool is true, the listener is not null and there is no cancellation requested
                while (Running && listener != null && !cancellationTokenSource.Token.IsCancellationRequested)
                {
                    var client = await listener.AcceptTcpClientAsync().WithWaitCancellation(cancellationTokenSource.Token);

                    if (client != null)
                    {
                        if (ClientList.Count(definition => definition.ServerEndPoint == client.Client.LocalEndPoint) == 0)
                        {
                            var oldClients = ClientList.Where(definition => definition.ServerEndPoint == client.Client.LocalEndPoint)
                                             .ToList();
                            foreach (var vncRepeaterDefinition in oldClients)
                            {
                                ClientList.Remove(vncRepeaterDefinition);
                            }
                        }

                        // Proxy the data from the client to the server until the end of stream filling the buffer.
                        if (ClientList.Count(definition => definition.ServerEndPoint == client.Client.LocalEndPoint) == 0)
                        {
                            var definition = new VNCRepeaterDefinition()
                            {
                                Authenticated  = false,
                                FirstRequest   = DateTime.UtcNow,
                                ClientEndPoint = (IPEndPoint)client.Client.RemoteEndPoint,
                                ServerEndPoint = (IPEndPoint)client.Client.LocalEndPoint
                            };

                            this.ClientList.Add(definition);

                            // We havent dealt with this one before.
                            await ProxyClientConnection(client, definition, bufferSize);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            listener.Stop();
        }
Esempio n. 2
0
        private void RandomMove_Button_Click(object sender, RoutedEventArgs e)
        {
            int    clientCount = clientList.Count();
            Random r           = new Random();

            foreach (var client in clientList)
            {
                PKS_CS_MOVE packet = new PKS_CS_MOVE();
                packet.command = PACKET_COMMAND.PACKET_CS_MOVE;
                packet.size    = (uint)Marshal.SizeOf <PKS_CS_MOVE>();
                packet.dest    = new Vector3(r.Next(0, 300), r.Next(0, 300), r.Next(0, 300));
                client.Send(packet);
            }
        }
        public virtual void Broadcast(string Msg)
        {
            if (ClientList == null || ClientList.Count() == 0)//没有客户端连接
            {
                //if (Sended != null)
                //{
                //    Sended(this, new SendEventArgs()
                //    {
                //        AsyncResult = null,//为null表示没有客户端连接
                //        Msg = Msg,
                //        Error = null
                //    });
                //}
                return;
            }
            List <ClientWrapper> removes = new List <ClientWrapper>();

            foreach (var cl in ClientList)
            {
                if (cl.Socket.Connected)
                {
                    //byte[] buffer = Encoding.UTF8.GetBytes(Msg);

                    byte[] buffer = AddMessageLength(Encoding.UTF8.GetBytes(Msg));//考虑Flash版获取不到消息长度,才加上。
                    //cl.Buffer = buffer;//不能这样做,接收时会取到此Buffer中的数据。因为cl内存地址不变的。此处保存buffer仅仅是为了异步回调时随cl一起传递,以便作其他处理。
                    //cl.Buffer只用于从客户端接收值
                    List <object> list = new List <object>();
                    list.Add(cl);
                    list.Add(Msg);
                    //try
                    //{
                    cl.Socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendComplete), list);
                    //}
                    //catch (Exception ex)
                    //{

                    //}
                }
                else
                {
                    removes.Add(cl);
                    //ClientList.Remove(cl);//这里修改了ClientList然后又遍历ClientList就要出错,正在遍历时不要更改。
                }
            }
            foreach (var cl in removes)
            {
                ClientList.Remove(cl);
            }
        }