Ejemplo n.º 1
0
        public async void Start()
        {
            if (Active)
            {
                return;
            }
            _cancen = new CancellationTokenSource();
            Active  = true;

            string ip   = _plugin.Controller.Ip;
            int    port = _plugin.Controller.Port;

            while (!_cancen.IsCancellationRequested)
            {
                _client = null;
                NetworkStream stream = null;
                try
                {
                    _client = new TcpClient();
                    _plugin.Log("connect to " + ip + ":" + port.ToString());
                    await _client.ConnectAsync(ip, port);

                    stream = _client.GetStream();
                    byte[] buffer        = new byte[_client.ReceiveBufferSize];
                    int    offset        = 0;
                    int    endPosition   = 0;
                    int    startPosition = 0;
                    int    size          = 0;
                    _plugin.Log("recv start");
                    while (true)
                    {
                        var i = await stream.ReadAsync(buffer, offset, buffer.Length - offset, _cancen.Token);

                        if (i < 1 || _cancen.IsCancellationRequested)
                        {
                            break;
                        }
                        size          = offset + i;
                        startPosition = 0;
                        while ((endPosition = Array.IndexOf(buffer, (byte)10, offset, size - offset)) > -1)
                        {
                            string data = Encoding.UTF8.GetString(buffer, startPosition, endPosition - startPosition + 1);
#if DEBUG
                            _plugin.Log(data.TrimEnd());
#endif
                            toAct(data.TrimEnd());
                            offset = startPosition = endPosition + 1;
                            if (offset == size)
                            {
                                break;
                            }
                        }
                        if (size > offset)
                        {
                            Buffer.BlockCopy(buffer, offset, buffer, 0, size - offset);
                            offset = size - offset;
                        }
                        else
                        {
                            offset = 0;
                        }
                    }
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    _plugin.Log("error:" + e.Message);
                }
                catch (Exception e)
                {
                    _plugin.Log("error:" + e.Message);
                }
                finally
                {
                    _plugin.Log("close connect");
                    if (stream != null)
                    {
                        stream.Close();
                    }
                    if (_client != null)
                    {
                        _client.Close();
                    }
                }
                _plugin.Log("wait...(5s)");
                for (var i = 0; i < 10; i++)
                {
                    if (_cancen.IsCancellationRequested)
                    {
                        break;
                    }
                    await Task.Delay(500);
                }
            }
            _cancen = null;
            Active  = false;
            OnClose();
        }
Ejemplo n.º 2
0
        public async void Start()
        {
            if (_active)
            {
                return;
            }
            _active = true;

            //キューにログを詰め込む
            ActGlobals.oFormActMain.BeforeLogLineRead += new LogLineEventDelegate(BeforeLogLineRead);
            //詰め込んだログを配信する
            queueCheckAsync();

            int       port      = _plugin.Controller.Port;
            IPAddress localAddr = _plugin.Controller.GetIP();

            try {
                _server = new TcpListener(localAddr, port);
                _server.Start();
                _plugin.Log("Starting... " + localAddr.ToString() + ":" + port.ToString());
                while (true)
                {
                    var client = await _server.AcceptTcpClientAsync();

                    client.NoDelay = true;

                    //dynamic newjson = new DynamicJson();
                    //newjson.Name = ActGlobals.oFormActMain.

                    lock (_clients)
                    {
                        _clients.Add(new TcpInfo {
                            client = client,
                            stream = client.GetStream()
                        });
                        _plugin.Log("Accepted client: " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
                    }
                }
            }
            catch (System.ObjectDisposedException)
            {
                _plugin.Log("close");
            }
            catch (Exception e) {
                _plugin.Log(e.ToString());
            }
            finally
            {
                if (_server != null)
                {
                    _server.Stop();
                }
                _server = null;
            }
            lock (_clients)
            {
                foreach (var c in _clients)
                {
                    c.stream.Close();
                    c.client.Close();
                }
                _clients.Clear();
            }
            ActGlobals.oFormActMain.BeforeLogLineRead -= new LogLineEventDelegate(BeforeLogLineRead);
            _active = false;
            OnClose();
        }