From 85951e961fceacbbbdf35fc027ac0207748d214a Mon Sep 17 00:00:00 2001 From: icedream Date: Wed, 7 May 2014 22:32:18 +0200 Subject: [PATCH] Lots of debugging. --- src/libnpsharp/NPClient.cs | 18 ++++++++----- src/libnpsharp/RPC/RPCClientStream.cs | 8 ++++++ src/npmotd/Program.cs | 37 ++++++++++++++++++++++----- src/npmotd/npmotd.csproj | 7 +++++ src/npmotd/packages.config | 4 +++ 5 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 src/npmotd/packages.config diff --git a/src/libnpsharp/NPClient.cs b/src/libnpsharp/NPClient.cs index 0ce86ed..d042bfc 100644 --- a/src/libnpsharp/NPClient.cs +++ b/src/libnpsharp/NPClient.cs @@ -18,6 +18,7 @@ namespace NPSharp private readonly RPCClientStream _rpc; private CancellationTokenSource _cancellationTokenSource; private CancellationToken _cancellationToken; + private Task _procTask; private readonly ILog _log; /// @@ -48,25 +49,22 @@ namespace NPSharp /// True if the connection succeeded, otherwise false. public bool Connect() { + _log.Debug("Connect() start"); + _cancellationTokenSource = new CancellationTokenSource(); _cancellationToken = _cancellationTokenSource.Token; if (!_rpc.Open()) return false; - Task.Factory.StartNew(() => + _procTask = Task.Factory.StartNew(() => { _log.Debug("Now receiving RPC messages"); try { while (true) { - var message = _rpc.Read(); - if (message == null) - continue; - - // TODO: log4net - Console.WriteLine("Received packet ID {1} (type {0})", message.GetType().Name, message.MessageId); + _rpc.Read(); } } catch (ProtocolViolationException error) @@ -78,6 +76,7 @@ namespace NPSharp _log.Debug("Now not receiving RPC messages anymore"); }, _cancellationToken); + _log.Debug("Connect() done"); return true; } @@ -86,10 +85,15 @@ namespace NPSharp /// public void Disconnect() { + _log.Debug("Disconnect() start"); + _cancellationTokenSource.Cancel(true); // TODO: Find a cleaner way to cancel _processingTask (focus: _rpc.Read) + _procTask.Wait(_cancellationToken); _rpc.Close(); LoginId = 0; + + _log.Debug("Disconnect() done"); } // TODO: Try to use an exception for failed action instead diff --git a/src/libnpsharp/RPC/RPCClientStream.cs b/src/libnpsharp/RPC/RPCClientStream.cs index c09522c..87d3b8c 100644 --- a/src/libnpsharp/RPC/RPCClientStream.cs +++ b/src/libnpsharp/RPC/RPCClientStream.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net.Sockets; +using log4net; using NPSharp.RPC.Packets; namespace NPSharp.RPC @@ -12,6 +13,7 @@ namespace NPSharp.RPC { private NetworkStream _ns; private uint _id; + private ILog _log; private readonly string _host; private readonly ushort _port; @@ -27,6 +29,7 @@ namespace NPSharp.RPC { _host = host; _port = port; + _log = LogManager.GetLogger("RPC"); } /// @@ -98,6 +101,9 @@ namespace NPSharp.RPC var buffer = message.Serialize(_id); _ns.Write(buffer, 0, buffer.Length); + _ns.Flush(); + + _log.DebugFormat("Sent packet ID {1} (type {0})", message.GetType().Name, _id); return _id++; } @@ -122,6 +128,8 @@ namespace NPSharp.RPC _callbacks[message.MessageId].Invoke(message); _callbacks.Remove(message.MessageId); + _log.DebugFormat("Received packet ID {1} (type {0})", message.GetType().Name, message.MessageId); + return message; } } diff --git a/src/npmotd/Program.cs b/src/npmotd/Program.cs index 8a2e0c2..617c08c 100644 --- a/src/npmotd/Program.cs +++ b/src/npmotd/Program.cs @@ -1,5 +1,10 @@ using System; using System.Text; +using log4net; +using log4net.Appender; +using log4net.Config; +using log4net.Core; +using log4net.Layout; using NPSharp.Authentication; namespace NPSharp.CommandLine.MOTD @@ -8,9 +13,25 @@ namespace NPSharp.CommandLine.MOTD { static void Main(string[] args) { + // log4net setup + var appender = new ColoredConsoleAppender + { + Threshold = Level.Debug, + Layout = new PatternLayout("%level [%thread] %d{HH:mm:ss} - %message%newline"), + }; + appender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Debug, ForeColor = ColoredConsoleAppender.Colors.Cyan | ColoredConsoleAppender.Colors.HighIntensity }); + appender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Info, ForeColor = ColoredConsoleAppender.Colors.Green | ColoredConsoleAppender.Colors.HighIntensity }); + appender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Warn, ForeColor = ColoredConsoleAppender.Colors.Purple | ColoredConsoleAppender.Colors.HighIntensity }); + appender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Error, ForeColor = ColoredConsoleAppender.Colors.Red | ColoredConsoleAppender.Colors.HighIntensity }); + appender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Fatal, ForeColor = ColoredConsoleAppender.Colors.White | ColoredConsoleAppender.Colors.HighIntensity, BackColor = ColoredConsoleAppender.Colors.Red }); + appender.ActivateOptions(); + BasicConfigurator.Configure(new IAppender[]{appender,new DebugAppender{Layout=appender.Layout,Threshold=Level.All}}); + + var log = LogManager.GetLogger("Main"); + if (args.Length < 4) { - Console.Error.WriteLine("Needs 4 arguments: hostname port username password"); + log.ErrorFormat("Needs 4 arguments: hostname port username password"); return; } @@ -20,10 +41,11 @@ namespace NPSharp.CommandLine.MOTD var password = args[3]; // NP connection setup + log.DebugFormat("Connecting to {0}:{1}...", hostname, port); var np = new NPClient(hostname, port); if (!np.Connect()) { - Console.Error.WriteLine("Connection to NP server failed."); + log.Error("Connection to NP server failed."); return; } @@ -35,10 +57,11 @@ namespace NPSharp.CommandLine.MOTD } catch (Exception err) { + np.Disconnect(); #if DEBUG - Console.Error.WriteLine("Could not authenticate: {0}", err); + log.ErrorFormat("Could not authenticate: {0}", err); #else - Console.Error.WriteLine("Could not authenticate: {0}", err.Message); + log.ErrorFormat("Could not authenticate: {0}", err.Message); #endif return; } @@ -51,9 +74,9 @@ namespace NPSharp.CommandLine.MOTD catch (Exception err) { #if DEBUG - Console.Error.WriteLine("Authenticated but session token was invalid. {0}", err); + log.ErrorFormat("Authenticated but session token was invalid. {0}", err); #else - Console.Error.WriteLine("Authenticated but session token was invalid ({0}).", err.Message); + log.ErrorFormat("Authenticated but session token was invalid ({0}).", err.Message); #endif return; } @@ -64,7 +87,7 @@ namespace NPSharp.CommandLine.MOTD } catch { - Console.Error.WriteLine("Could not read MOTD from NP server."); + log.ErrorFormat("Could not read MOTD from NP server."); } diff --git a/src/npmotd/npmotd.csproj b/src/npmotd/npmotd.csproj index dd55d3b..d639486 100644 --- a/src/npmotd/npmotd.csproj +++ b/src/npmotd/npmotd.csproj @@ -11,6 +11,8 @@ npmotd v4.5 512 + ..\..\ + true true @@ -37,6 +39,9 @@ $(SolutionDir)\bin\$(Configuration)\$(Platform)\ + + ..\..\packages\log4net.2.0.3\lib\net40-full\log4net.dll + @@ -47,6 +52,7 @@ + @@ -55,6 +61,7 @@ +