Пример #1
0
        public void ReceiveData(object newclient)
        {
            TcpClient client = newclient as TcpClient; // 一个接入的对象

            if (client == null)
            {
                return;
            }
            while (true)
            {
                if (!client.Connected || (client.Client.Poll(20, SelectMode.SelectRead) &&
                                          client.Available == 0))
                {
                    LogWriter.Info($"来自 {client.Client.RemoteEndPoint} 的连接断开");
                    client.Close();
                    return;
                }
                //在接收数据时,如果可读字节流是 0 ,则跳过
                if (client.Available < 1)
                {
                    continue;
                }
                //因为有包粘连,所以一次尽可能读取长的字节
                //而非读取一部分,其它留在缓冲区
                byte[] buffer = new byte[client.Available];
                try
                {
                    var stream = client.GetStream();
                    var read   = new BinaryReader(stream);
                    buffer = read.ReadBytes(buffer.Length);
                    ActiveMQHelper.InsertMQ(buffer); //插入MQ

                    var clientsync = new ClientSync();
                    clientsync.ClientsOnline(buffer, stream); // 响应上线
                }
                catch (SocketException ex)
                {
                    // 当对方主动断开链接时,ReadBytes 会报错,此时断开链接
                    LogWriter.Info($"来自 {client.Client.RemoteEndPoint} 的连接断开");
                    LogWriter.Error(ex.ToString());
                    client.Close();
                    return;
                }
                catch (Exception ex)
                {
                    LogWriter.Error("发生未知错误 : ");
                    LogWriter.Error(ex.ToString());
                    client.Close();
                    return;
                }
            }
        }
        private void Connect()
        {
            if (!_stop)
            {
                Stop();
            }
            else
            {
                SaveSelectedInputAndOutput();

                try
                {
                    //process hostname
                    var ipAddr = Dns.GetHostAddresses(GetAddressFromTextBox());

                    if (ipAddr.Length > 0)
                    {
                        _resolvedIp = ipAddr[0];
                        _port       = GetPortFromTextBox();

                        _client = new ClientSync(_clients, _guid);
                        _client.TryConnect(new IPEndPoint(_resolvedIp, _port), ConnectCallback);

                        StartStop.Content   = "Connecting...";
                        StartStop.IsEnabled = false;
                        Mic.IsEnabled       = false;
                        Speakers.IsEnabled  = false;
                        MicOutput.IsEnabled = false;
                    }
                    else
                    {
                        //invalid ID
                        MessageBox.Show("Invalid IP or Host Name!", "Host Name Error", MessageBoxButton.OK,
                                        MessageBoxImage.Error);
                    }
                }
                catch (Exception ex) when(ex is SocketException || ex is ArgumentException)
                {
                    MessageBox.Show("Invalid IP or Host Name!", "Host Name Error", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
            }
        }
Пример #3
0
        private void Stop()
        {
            StartStop.Content   = "Connect";
            StartStop.IsEnabled = true;
            Mic.IsEnabled       = true;
            Speakers.IsEnabled  = true;
            try
            {
                _audioManager.StopEncoding();
            }
            catch (Exception ex)
            {
            }

            _stop = true;

            if (_client != null)
            {
                _client.Disconnect();
                _client = null;
            }
        }
Пример #4
0
    GameObject turrent;               // Turrent


    // Use this for initialization
    void Start () {
        offSet = new Vector3(0f, CameraHeight, 0f);
        tank = GameObject.FindWithTag("Driver");
        clientSync = GetComponent<ClientSync>();
    }