Skip to content

Commit

Permalink
network clientlib refacto for loginserver implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Jun 24, 2024
1 parent af38e36 commit d7f5087
Show file tree
Hide file tree
Showing 112 changed files with 443 additions and 168 deletions.
4 changes: 2 additions & 2 deletions l2-unity/Assets/Scripts/Combat/TargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void SetTarget(ObjectData target) {

PlayerEntity.Instance.TargetId = _target.Identity.Id;
PlayerEntity.Instance.Target = _target.Data.ObjectTransform;
ClientPacketHandler.Instance.SendRequestSetTarget(_target.Identity.Id);
GameClientPacketHandler.Instance.SendRequestSetTarget(_target.Identity.Id);
}

public void SetAttackTarget() {
Expand All @@ -63,7 +63,7 @@ public bool HasAttackTarget() {
public void ClearTarget() {
if (HasTarget()) {
if(PlayerEntity.Instance.TargetId != -1) {
ClientPacketHandler.Instance.SendRequestSetTarget(-1);
GameClientPacketHandler.Instance.SendRequestSetTarget(-1);
PlayerEntity.Instance.TargetId = -1;
PlayerEntity.Instance.Target = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void OnReachingTarget() {
// _networkCharacterControllerShare.ShareMoveDirection(Vector3.zero);
}

ClientPacketHandler.Instance.SendRequestAutoAttack();
GameClientPacketHandler.Instance.SendRequestAutoAttack();
}
}

Expand Down
2 changes: 1 addition & 1 deletion l2-unity/Assets/Scripts/Game/Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected virtual void EquipAllWeapons() {

/* Notify server that entity got attacked */
public void InflictAttack(AttackType attackType) {
ClientPacketHandler.Instance.InflictAttack(_identity.Id, attackType);
GameClientPacketHandler.Instance.InflictAttack(_identity.Id, attackType);
}

protected virtual void OnDeath() {
Expand Down
2 changes: 1 addition & 1 deletion l2-unity/Assets/Scripts/Game/Manager/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void LogOut() {

public void OnWorldSceneLoaded() {
GameObject.Destroy(L2LoginUI.Instance.gameObject);
ClientPacketHandler.Instance.SendLoadWorld();
GameClientPacketHandler.Instance.SendLoadWorld();
}

public void OnPlayerInfoReceived() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ public class AsynchronousClient {
private string _username;
private int _port;
private bool _connected;
private ClientPacketHandler _clientPacketHandler;
private ServerPacketHandler _serverPacketHandler;

public int Ping { get; set; }

public AsynchronousClient(string ip, int port) {
public AsynchronousClient(string ip, int port, ClientPacketHandler clientPacketHandler, ServerPacketHandler serverPacketHandler) {
_ipAddress = ip;
_port = port;
_clientPacketHandler = clientPacketHandler;
_serverPacketHandler = serverPacketHandler;
}

public bool Connect() {
IPHostEntry ipHostInfo = Dns.GetHostEntry(_ipAddress);
IPAddress ipAddress = ipHostInfo.AddressList[0];
Expand Down Expand Up @@ -46,7 +52,7 @@ public bool Connect() {

public void Disconnect() {
try {
ServerPacketHandler.Instance.CancelTokens();
_serverPacketHandler.CancelTokens();
_connected = false;
_client.Close();
_client.Dispose();
Expand All @@ -58,12 +64,6 @@ public void Disconnect() {
}

public void SendPacket(ClientPacket packet) {
if(DefaultClient.Instance.LogSentPackets) {
ClientPacketType packetType = (ClientPacketType)packet.GetPacketType();
if(packetType != ClientPacketType.Ping && packetType != ClientPacketType.RequestRotate) {
Debug.Log("[" + Thread.CurrentThread.ManagedThreadId + "] Sending packet:" + packetType);
}
}
try {
using (NetworkStream stream = new NetworkStream(_client)) {
stream.Write(packet.GetData(), 0, (int)packet.GetLength());
Expand Down Expand Up @@ -101,8 +101,11 @@ public void StartReceiving() {
received += readCount;
}

Task.Run(() => ServerPacketHandler.Instance.HandlePacketAsync(packet));
Task.Run(() => GameServerPacketHandler.Instance.HandlePacketAsync(packet));
}
}
}

protected void HandlePacket(Packet packet) {
}
}

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text;

public class AuthRequestPacket : ClientPacket {
public AuthRequestPacket(string username) : base((byte)ClientPacketType.AuthRequest) {
public AuthRequestPacket(string username) : base((byte)GameClientPacketType.AuthRequest) {
WriteS(username);
BuildPacket();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class LoadWorldPacket : ClientPacket {
public LoadWorldPacket() : base((byte)GameClientPacketType.LoadWorld) {
SetData(new byte[] { (byte)GameClientPacketType.LoadWorld, 0x02});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class PingPacket : ClientPacket {
public PingPacket() : base((byte)GameClientPacketType.Ping) {
SetData(new byte[] { (byte)GameClientPacketType.Ping, 0x02});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class RequestAnimPacket : ClientPacket {

public RequestAnimPacket(byte anim, float value) : base((byte)ClientPacketType.RequestAnim) {
public RequestAnimPacket(byte anim, float value) : base((byte)GameClientPacketType.RequestAnim) {
WriteB(anim);
WriteF(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


public class RequestAttackPacket : ClientPacket {
public RequestAttackPacket(int targetId, AttackType type) : base((byte)ClientPacketType.RequestAttack) {
public RequestAttackPacket(int targetId, AttackType type) : base((byte)GameClientPacketType.RequestAttack) {
WriteI(targetId);
WriteB((byte)type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;

public class RequestAutoAttackPacket : ClientPacket {
public RequestAutoAttackPacket() : base((byte)ClientPacketType.RequestAutoAttack) {
public RequestAutoAttackPacket() : base((byte)GameClientPacketType.RequestAutoAttack) {
BuildPacket();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class RequestMoveDirectionPacket : ClientPacket {

public RequestMoveDirectionPacket(Vector3 pos) : base((byte)ClientPacketType.RequestMoveDirection) {
public RequestMoveDirectionPacket(Vector3 pos) : base((byte)GameClientPacketType.RequestMoveDirection) {
WriteF(pos.x);
WriteF(pos.y);
WriteF(pos.z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class RequestMovePacket : ClientPacket {

public RequestMovePacket(Vector3 pos) : base((byte)ClientPacketType.RequestMove) {
public RequestMovePacket(Vector3 pos) : base((byte)GameClientPacketType.RequestMove) {
WriteF(pos.x);
WriteF(pos.y);
WriteF(pos.z);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;

public class RequestRotatePacket : ClientPacket {
public RequestRotatePacket(float angle) : base((byte)ClientPacketType.RequestRotate) {
public RequestRotatePacket(float angle) : base((byte)GameClientPacketType.RequestRotate) {
WriteF(angle);
BuildPacket();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class RequestSetTargetPacket : ClientPacket {
public RequestSetTargetPacket(int targetId) : base((byte)ClientPacketType.RequestSetTarget) {
public RequestSetTargetPacket(int targetId) : base((byte)GameClientPacketType.RequestSetTarget) {
WriteI(targetId);
BuildPacket();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text;

public class SendMessagePacket : ClientPacket {
public SendMessagePacket(string text) : base((byte)ClientPacketType.SendMessage) {
public SendMessagePacket(string text) : base((byte)GameClientPacketType.SendMessage) {
WriteS(text);
BuildPacket();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum ClientPacketType : byte
public enum GameClientPacketType : byte
{
Ping = 0x00,
AuthRequest = 0x01,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum ServerPacketType : byte
public enum GameServerPacketType : byte
{
Ping = 0,
AuthResponse = 1,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum LoginClientPacketType : byte {
Ping = 0x00,
AuthRequest = 0x01
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum LoginServerPacketType : byte
{
Ping = 0,
AuthResponse = 1,
MessagePacket = 2,
SystemMessage = 3,
PlayerInfo = 4,
ObjectPosition = 5,
RemoveObject = 6,
ObjectRotation = 7,
ObjectAnimation = 8,
ApplyDamage = 9,
NpcInfo = 0x0A,
ObjectMoveTo = 0x0B,
UserInfo = 0x0C,
ObjectMoveDirection = 0x0D,
GameTime = 0x0E,
EntitySetTarget = 0x0F,
AutoAttackStart = 0x10,
AutoAttackStop = 0x11,
ActionFailed = 0x12
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public abstract class ClientPacketHandler
{
protected AsynchronousClient _client;

public void SetClient(AsynchronousClient client) {
_client = client;
}

public abstract void SendPacket(ClientPacket packet);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d7f5087

Please sign in to comment.