コード例 #1
0
ファイル: JsonApiV2.cs プロジェクト: josephworks/BukkitGUI2
        public void ReadConsoleStream()
        {
            JsonApiConnector.HandleOutput(LogPrefix + "Connecting to the external server...");
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                sock.Connect(_host, _port + 1);
            }
            catch (Exception connectException)
            {
                JsonApiConnector.HandleOutput(ErrPrefix + "Couldn't connect to JsonApi stream:" +
                                              connectException.Message);
            }

            if (sock.Connected)
            {
                NetworkStream stream  = new NetworkStream(sock);
                StreamWriter  sw      = new StreamWriter(stream);
                string        request = "/api/2/subscribe?json=" + CreateJsonPayload("console", null, true);
                sw.WriteLine(request);
                sw.Flush();
                StreamReader sr = new StreamReader(stream);
                JsonApiConnector.HandleOutput(LogPrefix + "Connected!");

                while (_listening && sock.Connected && stream.CanRead)
                {
                    string l = "";
                    try
                    {
                        while (_listening && sock.Connected && stream.CanRead && sr.EndOfStream)
                        {
                            Thread.Sleep(100);
                        }
                        if (_listening && sock.Connected)
                        {
                            l = sr.ReadLine();
                        }
                    }
                    catch (Exception readex)
                    {
                        Debug.WriteLine("exception at run_connection_receive, while reading from networkstream " +
                                        readex.Message);
                        //don't flag as critical error
                    }

                    if (!string.IsNullOrEmpty(l) && l.Contains("{") & l.Contains(":") & l.Contains("}"))
                    {
                        l = new JsonApiStreamResult(l).Line;
                        if (!_filter ||
                            !Regex.IsMatch(l,
                                           "^[^\\]]*\\](:|\\s){0,2}\\[JSONAPI\\]\\s?\\[(api|stream) (call|request)\\]",
                                           RegexOptions.IgnoreCase))
                        {
                            JsonApiConnector.HandleOutput(l.TrimEnd(Environment.NewLine.ToCharArray()));
                        }
                    }

                    Thread.Sleep(10);
                }
            }
            JsonApiConnector.HandleOutput(LogPrefix + "Disconnected");
            if (sock.Connected)
            {
                sock.Close();
            }
            _listening = false;
        }
コード例 #2
0
ファイル: JsonApiV2.cs プロジェクト: CaptainTF/bukkitgui2
        public void ReadConsoleStream()
        {
            JsonApiConnector.HandleOutput(LogPrefix + "Connecting to the external server...");
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                sock.Connect(_host, _port + 1);
            }
            catch (Exception connectException)
            {
                JsonApiConnector.HandleOutput(ErrPrefix + "Couldn't connect to JsonApi stream:" +
                                              connectException.Message);
            }

            if (sock.Connected)
            {
                NetworkStream stream = new NetworkStream(sock);
                StreamWriter sw = new StreamWriter(stream);
                string request = "/api/2/subscribe?json=" + CreateJsonPayload("console", null, true);
                sw.WriteLine(request);
                sw.Flush();
                StreamReader sr = new StreamReader(stream);
                JsonApiConnector.HandleOutput(LogPrefix + "Connected!");

                while (_listening && sock.Connected && stream.CanRead)
                {
                    string l = "";
                    try
                    {
                        while (_listening && sock.Connected && stream.CanRead && sr.EndOfStream)
                        {
                            Thread.Sleep(100);
                        }
                        if (_listening && sock.Connected) l = sr.ReadLine();
                    }
                    catch (Exception readex)
                    {
                        Debug.WriteLine("exception at run_connection_receive, while reading from networkstream " +
                                        readex.Message);
                        //don't flag as critical error
                    }

                    if (!string.IsNullOrEmpty(l) && l.Contains("{") & l.Contains(":") & l.Contains("}"))
                    {
                        l = new JsonApiStreamResult(l).Line;
                        if (!_filter ||
                            !Regex.IsMatch(l,
                                "^[^\\]]*\\](:|\\s){0,2}\\[JSONAPI\\]\\s?\\[(api|stream) (call|request)\\]",
                                RegexOptions.IgnoreCase))
                            JsonApiConnector.HandleOutput(l.TrimEnd(Environment.NewLine.ToCharArray()));
                    }

                    Thread.Sleep(10);
                }
            }
            JsonApiConnector.HandleOutput(LogPrefix + "Disconnected");
            if (sock.Connected) sock.Close();
            _listening = false;
        }