Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from eesast/dev
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
cititude authored Apr 10, 2020
2 parents 3b4828e + 15d6a17 commit e69a481
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 28 deletions.
13 changes: 9 additions & 4 deletions communication/Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ private static int MainInternal(string ep, ushort port, string token, int debugL
};
server.OnReceive += delegate (Message message)
{
var now = Environment.TickCount;
lock (LastSpam)
if (!(((message.Content as Message)?.Content as Message)?.Content is PingPacket))
{
if (now <= (int)LastSpam + Constants.TimeLimit) return;
LastSpam = now;
var now = Environment.TickCount;
lock (LastSpam)
{
if (now <= (int)LastSpam + Constants.TimeLimit) return;
LastSpam = now;
}
}
else
Constants.Debug("Ignoring PingPacket");

client.Send(message.Content as Message);
};
Expand Down
13 changes: 11 additions & 2 deletions communication/Proto/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ public static class Constants
public static readonly IPEndPoint Server = new IPEndPoint(IPAddress.Loopback, ServerPort);
public static readonly IPEndPoint Agent = new IPEndPoint(IPAddress.Loopback, AgentPort);
public static readonly int HeartbeatInternal = 1000;
public static readonly int TokenExpire = 3600;

public static readonly int TokenExpire = 3600;

public static DebugFunc Debug = delegate (string DebugMessage)
{
var stack = new StackTrace();
var method = stack.GetFrame(1).GetMethod();
Console.WriteLine($"[{method.DeclaringType.Name}/{method.Name}] {DebugMessage}");
};

public static DebugFunc Error = delegate (string DebugMessage) //这里应该另外加一个debug level
{
var stack = new StackTrace();
var method = stack.GetFrame(1).GetMethod();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{method.DeclaringType.Name}/{method.Name}] {DebugMessage}");
Console.ResetColor();
};
}
}
34 changes: 22 additions & 12 deletions communication/Proto/Message.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Google.Protobuf;
using Google.Protobuf.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand All @@ -9,30 +10,39 @@ namespace Communication.Proto
{
internal class Message : IMessage //Communication�ڲ�ʹ�õ�Message
{
public int Address; //������/�����ߣ��򻷾�����
public IMessage Content; //������
public int Address;
public IMessage Content;

public MessageDescriptor Descriptor => null;

public int CalculateSize() //Ŀǰû�����Բ�ʵ��
public int CalculateSize()
{
return Content.CalculateSize() + 4 + Content.GetType().FullName.Length;
}

public void MergeFrom(Stream stream)
{
MergeFrom(new CodedInputStream(stream));
try
{
MergeFrom(new CodedInputStream(stream));
}
catch (Exception e)
{
Constants.Error($"Unhandled exception while trying to deserialize packet: {e}");
//stream should be instance of MemoryStream only.
Constants.Error($"Packet: {string.Concat((stream as MemoryStream).ToArray().Select((b) => $"{b:X2} "))}");
}
}

public void MergeFrom(CodedInputStream input)
{
Address = input.ReadInt32();
string PacketType = input.ReadString();

Content = Activator.CreateInstance(Type.GetType(PacketType)) as IMessage;

Content.MergeFrom(input);
Constants.Debug($"{PacketType} received ({Content.CalculateSize()} bytes)");
{
Address = input.ReadInt32();
string PacketType = input.ReadString();

Content = Activator.CreateInstance(Type.GetType(PacketType)) as IMessage;

Content.MergeFrom(input);
Constants.Debug($"{PacketType} received ({Content.CalculateSize()} bytes)");
}

public void WriteTo(Stream stream)
Expand Down
17 changes: 11 additions & 6 deletions communication/Server/CommunicationImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ public string Token
}
set
{
var json = JObject.Parse(decoder.Decode(value));
Constants.Debug($"Parsing roomID from {json}");
roomID = (string)json["roomID"];
Constants.Debug($"roomID = {roomID}");
if (string.IsNullOrEmpty(value))
{
Constants.Debug("Enable offline mode.");
}
else
{
var json = JObject.Parse(decoder.Decode(value));
Constants.Debug($"Parsing roomID from {json}");
roomID = (string)json["roomID"];
Constants.Debug($"roomID = {roomID}");
}
token = value;
}
}
Expand Down Expand Up @@ -71,7 +78,6 @@ private async Task HttpAsync(string uri, string token, string method, JObject da

private async Task NoticeServer(string token, DockerGameStatus status)
{
if (IsOffline) return;
if (token == null)
{
await HttpAsync($"https://api.eesast.com/v1/rooms/{roomID}", this.token, "PUT", new JObject
Expand Down Expand Up @@ -100,7 +106,6 @@ public ushort ServerPort
get => server.Port;
set => server.Port = value;
}
public bool IsOffline { get; set; }

public event MessageHandler MsgProcess;

Expand Down
1 change: 0 additions & 1 deletion communication/Server/ICommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public interface ICommunication : IDisposable
{
int PlayerCount { get;}

bool IsOffline { get; set; }
/// <summary>
/// Token for the client api
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions communication/ServerChatTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void Main(string[] args)
//Console.WriteLine(args[0]);
//comm.EndPoint = new IPEndPoint(IPAddress.Parse(t[0]), ushort.Parse(t[1]));
//comm.ID = args[1];
Constants.ServerPort = 20000;
comm.IsOffline = false;
comm.ServerPort = Constants.ServerPort;
comm.Token = new JwtEncoder(new HMACSHA256Algorithm(), new JsonNetSerializer(), new JwtBase64UrlEncoder())
Expand Down
2 changes: 1 addition & 1 deletion communication/runAgent.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@dotnet Agent/bin/Debug/netcoreapp3.0/Communication.Agent.dll --server 127.0.0.1:10086 --port 30000 --token token --playercount 1
@dotnet Agent/bin/Release/netcoreapp3.0/Communication.Agent.dll --server 127.0.0.1:10086 --port 30000 --token token --playercount 1
2 changes: 1 addition & 1 deletion communication/runClient.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@dotnet ClientChatTest\bin\Debug\netcoreapp3.0\Communication.ClientChatTest.dll 127.0.0.1:30000
@dotnet ClientChatTest\bin\Release\netcoreapp3.0\Communication.ClientChatTest.dll 127.0.0.1:30000
2 changes: 1 addition & 1 deletion communication/runServer.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@dotnet exec ServerChatTest/bin/Debug/netcoreapp3.0/Communication.ServerChatTest.dll
@dotnet exec ServerChatTest/bin/Release/netcoreapp3.0/Communication.ServerChatTest.dll

0 comments on commit e69a481

Please sign in to comment.