Add Server Info Reply. Change handler method signature.

This commit is contained in:
Ray Koopa 2019-01-02 18:20:27 +01:00
parent 13f97d706a
commit f6b37eb96c
5 changed files with 82 additions and 35 deletions

View File

@ -20,34 +20,25 @@ namespace Syroot.Worms.OnlineWorms.Server
/// </summary>
/// <param name="tcpClient">The <see cref="TcpClient"/> representing the connection to the client.</param>
/// <param name="server">The <see cref="Server"/> instance with which this client communicates.</param>
public Client(TcpClient tcpClient, Server server)
internal Client(TcpClient tcpClient, Server server)
: base(tcpClient)
{
_server = server;
}
// ---- METHODS (PROTECTED) ------------------------------------------------------------------------------------
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
protected override void OnPrePacketHandle(Packet packet)
public void HandleConnect(ConnectQueryPacket inPacket)
{
_server.Log.Write(LogCategory.Client, $"{TcpClient.Client.RemoteEndPoint} >> {packet}");
SendPacket(new ConnectReplyPacket
{
Unknown = _server.Name,
Unknown2 = _server.RegionName,
Version = _server.Version
});
}
protected override void OnPrePacketSend(Packet packet)
{
_server.Log.Write(LogCategory.Server, $"{TcpClient.Client.RemoteEndPoint} << {packet}");
}
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
private Packet HandleConnect(ConnectQueryPacket inPacket) => new ConnectReplyPacket
{
Unknown = _server.Name,
Unknown2 = _server.RegionName,
Version = _server.Version
};
private Packet HandleLogin(LoginQueryPacket inPacket)
public void HandleLogin(LoginQueryPacket inPacket)
{
LoginPlayerInfo[] playerInfos = new LoginPlayerInfo[inPacket.Logins.Length];
for (int i = 0; i < inPacket.Logins.Length; i++)
@ -60,16 +51,36 @@ namespace Syroot.Worms.OnlineWorms.Server
};
}
return new LoginReplyPacket
// Send login result.
SendPacket(new LoginReplyPacket
{
Unknown1 = 1,
LoginResult = LoginResult.Success,
PlayerInfos = playerInfos
};
});
// Send info text.
SendPacket(new ServerInfoReplyPacket { Text = @"Welcome to the Online Worms Server.
Encoding tests are following:
Direct Sound Error로
.
%s님을 \t하시겠습니까?
01234567890" });
}
#if DEBUG
private Packet HandleRaw(RawQueryPacket inPacket) => null;
public void HandleRaw(RawQueryPacket inPacket) { }
#endif
// ---- METHODS (PROTECTED) ------------------------------------------------------------------------------------
protected override void OnPrePacketHandle(Packet packet)
{
_server.Log.Write(LogCategory.Client, $"{ TcpClient.Client.RemoteEndPoint } >> { packet }");
}
protected override void OnPrePacketSend(Packet packet)
{
_server.Log.Write(LogCategory.Server, $"{TcpClient.Client.RemoteEndPoint} << {packet}");
}
}
}

View File

@ -102,11 +102,11 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
if (!_connectionClassesCache.TryGetValue(classType, out Dictionary<Type, MethodInfo> handlerMethods))
{
handlerMethods = new Dictionary<Type, MethodInfo>();
// Find all packet handling methods which are methods accepting and returning a specific packet.
foreach (MethodInfo method in classType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
// Find all packet handling methods which are methods accepting a specific packet and returning nothing.
foreach (MethodInfo method in classType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
Type inPacket = method.GetParameters().FirstOrDefault()?.ParameterType;
if (typeof(Packet).IsAssignableFrom(inPacket) && typeof(Packet).IsAssignableFrom(method.ReturnType))
if (method.ReturnType == typeof(void) && typeof(Packet).IsAssignableFrom(inPacket))
handlerMethods.Add(inPacket, method);
}
_connectionClassesCache.Add(classType, handlerMethods);
@ -117,14 +117,7 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
private void HandlePacket(Packet inPacket)
{
OnPrePacketHandle(inPacket);
Type packetType = inPacket.GetType();
// Invoke the handler and send back any packet resulting from it.
if (_handlers[packetType].Invoke(this, new[] { inPacket }) is Packet outPacket)
{
OnPrePacketSend(outPacket);
SendPacket(outPacket);
}
_handlers[inPacket.GetType()].Invoke(this, new[] { inPacket });
}
private Packet ReceivePacket()
@ -148,8 +141,10 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
return packet;
}
private bool SendPacket(Packet packet)
protected bool SendPacket(Packet packet)
{
OnPrePacketSend(packet);
// Serialize the raw packet data.
_sendStream.Position = 0;
packet.Serialize(_sendStream);

View File

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
@ -76,9 +77,13 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
sb.Append(indent);
sb.Append(enumValue.ToString());
break;
case IPAddress ipAddress:
sb.Append(indent);
sb.Append(ipAddress.ToString());
break;
default:
foreach (PropertyInfo property in obj.GetType().GetProperties(
BindingFlags.Instance | BindingFlags.NonPublic))
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
// Ignore indexers.
if (property.GetIndexParameters().Length > 0)

View File

@ -90,6 +90,10 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
}
}
/// <summary>
/// Reads a 2-byte length-prefixed, Windows-949 enoded string.
/// </summary>
/// <returns>The read value.</returns>
internal string ReadString()
{
return _baseStream.ReadString(StringCoding.Int16CharCount, _win949Encoding);
@ -100,6 +104,10 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
return _baseStream.ReadBytes((int)(Length - Position));
}
/// <summary>
/// Writes a 2-byte length-prefixed, Windows-949 encoded string.
/// </summary>
/// <param name="value">The value to write.</param>
internal void WriteString(string value)
{
_baseStream.WriteString(value, StringCoding.Int16CharCount, _win949Encoding);

View File

@ -0,0 +1,28 @@
using System;
namespace Syroot.Worms.OnlineWorms.Server.Net
{
/// <summary>
/// Represents an additional server response to a <see cref="LoginQueryPacket"/>, providing informational server
/// screen text.
/// </summary>
[Packet(0x8033)]
internal class ServerInfoReplyPacket : Packet
{
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
/// <summary>
/// Gets or sets the text to display in the information box. Only the first 10 lines are read by the server.
/// </summary>
internal string Text { get; set; }
// ---- METHODS (INTERNAL) -------------------------------------------------------------------------------------
internal override void Deserialize(PacketStream stream) => throw new NotImplementedException();
internal override void Serialize(PacketStream stream)
{
stream.WriteString(Text.Replace(Environment.NewLine, "\n"));
}
}
}