MB 5 years ago
parent
commit
86ef441910
4 changed files with 41 additions and 35 deletions
  1. +12
    -4
      Assets/Scipts/Multiplayer/Client/ClientManager.cs
  2. +8
    -9
      Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs
  3. +20
    -1
      Assets/Scipts/Multiplayer/Server/LoginManager.cs
  4. +1
    -21
      Assets/Scipts/Multiplayer/Server/ServerManager.cs

+ 12
- 4
Assets/Scipts/Multiplayer/Client/ClientManager.cs View File

@ -71,6 +71,7 @@ namespace Multiplayer
{ {
client.RegisterHandler(MsgType.Connect, OnConnected); client.RegisterHandler(MsgType.Connect, OnConnected);
client.RegisterHandler(MsgType.Disconnect, OnDisconnected); client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
client.RegisterHandler(LoginMsgID.QueryName, OnRecieveID);
} }
@ -78,10 +79,7 @@ namespace Multiplayer
{ {
IsConnected = true; IsConnected = true;
ID = (byte)msg.conn.connectionId; ID = (byte)msg.conn.connectionId;
Debug.Log("Successfully connected to server");
Debug.Log("Connection ID: " + msg.conn.connectionId);
SendMessage(LoginMsgID.Name, new StringMsg(ID, Name));
Debug.Log("Successfully connected to server");
} }
private void OnDisconnected(NetworkMessage msg) private void OnDisconnected(NetworkMessage msg)
@ -90,7 +88,17 @@ namespace Multiplayer
Debug.Log("Disconnected from Server"); Debug.Log("Disconnected from Server");
} }
private void OnRecieveID(NetworkMessage msg)
{
PlayerMsg playerMsg;
if (!Utility.ReadMessage<PlayerMsg>(msg, out playerMsg))
return;
Debug.Log("Recieved ID from server: " + playerMsg.ID);
ID = playerMsg.ID;
SendMessage(LoginMsgID.Name, new StringMsg(ID, Name));
}
/// <summary> /// <summary>

+ 8
- 9
Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs View File

@ -16,23 +16,23 @@ namespace Multiplayer
public class PlayerMsg : MessageBase public class PlayerMsg : MessageBase
{ {
public ushort playerID;
public byte ID;
public PlayerMsg() { } public PlayerMsg() { }
public PlayerMsg(ushort playerID)
public PlayerMsg(byte playerID)
{ {
this.playerID = playerID;
this.ID = playerID;
} }
public override void Serialize(NetworkWriter writer) public override void Serialize(NetworkWriter writer)
{ {
writer.Write(playerID);
writer.Write(ID);
} }
public override void Deserialize(NetworkReader reader) public override void Deserialize(NetworkReader reader)
{ {
playerID = reader.ReadUInt16();
ID = reader.ReadByte();
} }
} }
@ -43,7 +43,7 @@ namespace Multiplayer
public Vector3 vector; public Vector3 vector;
public VectorMsg() { } public VectorMsg() { }
public VectorMsg(ushort PlayerID, Vector3 Vector) : base(PlayerID)
public VectorMsg(byte PlayerID, Vector3 Vector) : base(PlayerID)
{ {
this.vector = Vector; this.vector = Vector;
} }
@ -67,7 +67,7 @@ namespace Multiplayer
public Quaternion quaternion; public Quaternion quaternion;
public QuaternionMsg() { } public QuaternionMsg() { }
public QuaternionMsg(ushort PlayerID, Quaternion quaternion) : base(PlayerID)
public QuaternionMsg(byte PlayerID, Quaternion quaternion) : base(PlayerID)
{ {
this.quaternion = quaternion; this.quaternion = quaternion;
} }
@ -95,7 +95,7 @@ namespace Multiplayer
public StringMsg() { } public StringMsg() { }
public StringMsg(ushort PlayerID, string String) : base(PlayerID)
public StringMsg(byte PlayerID, string String) : base(PlayerID)
{ {
this.String = String; this.String = String;
} }
@ -104,7 +104,6 @@ namespace Multiplayer
public override void Serialize(NetworkWriter writer) public override void Serialize(NetworkWriter writer)
{ {
base.Serialize(writer); base.Serialize(writer);
writer.Write(String); writer.Write(String);
} }

+ 20
- 1
Assets/Scipts/Multiplayer/Server/LoginManager.cs View File

@ -21,6 +21,8 @@ namespace Multiplayer
public void RegisterHandles() public void RegisterHandles()
{ {
ServerManager.Server.RegisterHandler(LoginMsgID.Name, OnPlayerLogin); ServerManager.Server.RegisterHandler(LoginMsgID.Name, OnPlayerLogin);
ServerManager.Server.RegisterHandler(MsgType.Connect, OnConnect);
ServerManager.Server.RegisterHandler(MsgType.Disconnect, OnDisconnect);
} }
@ -33,7 +35,7 @@ namespace Multiplayer
Debug.Log("Player login: " + stringMsg.String); Debug.Log("Player login: " + stringMsg.String);
if (ServerManager.AllPlayers.ContainsKey(stringMsg.playerID))
if (ServerManager.AllPlayers.ContainsKey(stringMsg.ID))
{ {
Debug.Log(stringMsg.String + " already logged in"); Debug.Log(stringMsg.String + " already logged in");
return; return;
@ -43,6 +45,23 @@ namespace Multiplayer
ServerManager.AllPlayers.Add(newPlayer.ID, newPlayer); ServerManager.AllPlayers.Add(newPlayer.ID, newPlayer);
} }
private void OnConnect(NetworkMessage msg)
{
Debug.Log("New Connection: \n Address: " + msg.conn.address + "\n ID: " + msg.conn.connectionId);
msg.conn.Send(LoginMsgID.QueryName, new PlayerMsg((byte)msg.conn.connectionId));
}
private void OnDisconnect(NetworkMessage msg)
{
if (ServerManager.AllPlayers.ContainsKey(msg.conn.connectionId))
{
Debug.Log(ServerManager.AllPlayers[msg.conn.connectionId].Name + "has disconnected.");
ServerManager.AllPlayers.Remove(msg.conn.connectionId);
}
else
Debug.Log("Unkown player has disconnected.");
}
} }
} }

+ 1
- 21
Assets/Scipts/Multiplayer/Server/ServerManager.cs View File

@ -41,12 +41,7 @@ namespace Multiplayer
this.Port = Port; this.Port = Port;
AllPlayers = new Dictionary<int, Player>(); AllPlayers = new Dictionary<int, Player>();
Server = new NetworkServerSimple(); Server = new NetworkServerSimple();
Server.RegisterHandler(MsgType.Connect, OnConnect);
Server.RegisterHandler(MsgType.Disconnect, OnDisconnect);
Server.Configure(ChannelConfig.DefaultTopology()); Server.Configure(ChannelConfig.DefaultTopology());
Debug.Log("Starting Server on " + Port); Debug.Log("Starting Server on " + Port);
@ -62,22 +57,7 @@ namespace Multiplayer
} }
private void OnConnect(NetworkMessage msg)
{
Debug.Log("New Connection: \n Address: " + msg.conn.address + "\n ID: " + msg.conn.connectionId);
//msg.conn.Send(LoginMsgID.QueryName, new PlayerMsg((byte)msg.conn.connectionId));
}
private void OnDisconnect(NetworkMessage msg)
{
if (AllPlayers.ContainsKey(msg.conn.connectionId))
{
Debug.Log(AllPlayers[msg.conn.connectionId].Name + "has disconnected.");
AllPlayers.Remove(msg.conn.connectionId);
}
else
Debug.Log("Unkown player has disconnected.");
}

Loading…
Cancel
Save