Skip to content

Commit

Permalink
Better Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
jianmingyong committed Dec 14, 2015
1 parent 03ffcda commit 83a8cd5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Pokemon.3D.Server.Core/Pokemon.3D.Server.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ private void HandlePrivateMessage(Package p)
Core.Pokemon3DPlayer.SentToPlayer(new Package(Package.PackageTypes.PrivateMessage, Player.ID, p.DataItems, p.Client));

Core.Logger.Log(Player.isGameJoltPlayer ?
Core.Setting.Token("SERVER_GAMEJOLT", Player.Name, Player.GameJoltID.ToString(), "have sent a private message to " + p.DataItems[0]) :
Core.Setting.Token("SERVER_NOGAMEJOLT", Player.Name, "have sent a private message to " + p.DataItems[0]), Logger.LogTypes.PM, p.Client);
Core.Setting.Token("SERVER_GAMEJOLT", Player.Name, Player.GameJoltID.ToString(), "have sent a private message to " + p.DataItems[0] + " with the following message: " + p.DataItems[1]) :
Core.Setting.Token("SERVER_NOGAMEJOLT", Player.Name, "have sent a private message to " + p.DataItems[0] + " with the following message: " + p.DataItems[1]), Logger.LogTypes.PM, p.Client);
}
}

Expand Down
67 changes: 33 additions & 34 deletions Pokemon.3D.Server.Core/Server_Client_Listener/Servers/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ public class Listener : IDisposable
{
private TcpListener TcpListener { get; set; }

private TcpClient Client { get; set; }
private StreamReader Reader { get; set; }

private ThreadCollection ThreadCollection { get; set; } = new ThreadCollection();
private IWorkItemsGroup ThreadPool = new SmartThreadPool().CreateWorkItemsGroup(1);
private IWorkItemsGroup ThreadPool = new SmartThreadPool().CreateWorkItemsGroup(Environment.ProcessorCount);

private bool IsActive { get; set; } = false;

Expand Down Expand Up @@ -76,8 +73,6 @@ public void Dispose()
IsActive = false;

if (TcpListener != null) TcpListener.Stop();
if (Client != null) Client.Close();
if (Reader != null) Reader.Dispose();

ThreadCollection.Dispose();

Expand Down Expand Up @@ -115,19 +110,41 @@ private void ThreadStartListening()
{
try
{
Client = TcpListener.AcceptTcpClient();

if (Client != null)
{
Reader = new StreamReader(Client.GetStream());
ThreadPool.QueueWorkItem(new WorkItemCallback(ThreadHandlePackage), Reader.ReadLine());
}
ThreadPool.QueueWorkItem(new WorkItemCallback(ThreadAcceptTcpClient), TcpListener.AcceptTcpClient());
}
catch (ThreadAbortException) { return; }
catch (Exception) { }
} while (IsActive);
}

private object ThreadAcceptTcpClient(object obj)
{
try
{
TcpClient Client = (TcpClient)obj;

if (Client != null)
{
StreamReader Reader = new StreamReader(Client.GetStream());
string ReturnMessage = Reader.ReadLine();

if (!string.IsNullOrWhiteSpace(ReturnMessage))
{
Package Package = new Package(ReturnMessage, Client);
Core.Logger.Log($"Receive: {ReturnMessage}", Logger.LogTypes.Debug, Client);

if (Package.IsValid)
{
Package.Handle();
}
}
}
}
catch (Exception) { }

return null;
}

private void ThreadPortCheck()
{
Stopwatch sw = new Stopwatch();
Expand All @@ -150,29 +167,11 @@ private void ThreadPortCheck()
ClientEvent.Invoke(ClientEvent.Types.Restart);
}
}
} while (IsActive);
}

private object ThreadHandlePackage(object obj)
{
try
{
string ReturnMessage = (string)obj;

if (!string.IsNullOrWhiteSpace(ReturnMessage))
else
{
Package Package = new Package(ReturnMessage, Client);
Core.Logger.Log($"Receive: {ReturnMessage}", Logger.LogTypes.Debug, Client);

if (Package.IsValid)
{
Package.Handle();
}
Thread.Sleep(1);
}
}
catch (Exception) { }

return null;
} while (IsActive);
}

private void ThreadAutoRestart()
Expand Down

0 comments on commit 83a8cd5

Please sign in to comment.