Add PeerListener and make Menu more visible
This commit is contained in:
parent
545a3cc5c5
commit
634b260e18
@ -62,11 +62,13 @@
|
|||||||
<Compile Include="scripts\net\packethandling\PrimitiveSerializer.cs" />
|
<Compile Include="scripts\net\packethandling\PrimitiveSerializer.cs" />
|
||||||
<Compile Include="scripts\net\packethandling\Serializable.cs" />
|
<Compile Include="scripts\net\packethandling\Serializable.cs" />
|
||||||
<Compile Include="scripts\net\Peer.cs" />
|
<Compile Include="scripts\net\Peer.cs" />
|
||||||
|
<Compile Include="scripts\net\PeerListener.cs" />
|
||||||
<Compile Include="scripts\net\Server.cs" />
|
<Compile Include="scripts\net\Server.cs" />
|
||||||
<Compile Include="scripts\net\syncing\PacketDistributor.cs" />
|
<Compile Include="scripts\net\syncing\PacketDistributor.cs" />
|
||||||
<Compile Include="scripts\net\syncing\ConnectionHandler.cs" />
|
<Compile Include="scripts\net\syncing\ConnectionHandler.cs" />
|
||||||
<Compile Include="scripts\util\DisconnectReason.cs" />
|
<Compile Include="scripts\util\DisconnectReason.cs" />
|
||||||
<Compile Include="scripts\util\Optional.cs" />
|
<Compile Include="scripts\util\Optional.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
@ -139,6 +139,27 @@ lines_skipped = 0
|
|||||||
max_lines_visible = -1
|
max_lines_visible = -1
|
||||||
_sections_unfolded = [ "Margin", "custom_fonts" ]
|
_sections_unfolded = [ "Margin", "custom_fonts" ]
|
||||||
|
|
||||||
|
[node name="StatusText" type="Label" parent="Panel"]
|
||||||
|
|
||||||
|
anchor_left = 0.0
|
||||||
|
anchor_top = 1.0
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
margin_top = -34.0
|
||||||
|
margin_bottom = -15.0
|
||||||
|
rect_pivot_offset = Vector2( 0, 0 )
|
||||||
|
rect_clip_content = false
|
||||||
|
mouse_filter = 2
|
||||||
|
size_flags_horizontal = 1
|
||||||
|
size_flags_vertical = 4
|
||||||
|
custom_fonts/font = ExtResource( 3 )
|
||||||
|
text = "Status: Ok!"
|
||||||
|
align = 1
|
||||||
|
percent_visible = 1.0
|
||||||
|
lines_skipped = 0
|
||||||
|
max_lines_visible = -1
|
||||||
|
_sections_unfolded = [ "Anchor", "Margin", "custom_fonts" ]
|
||||||
|
|
||||||
[node name="IPAddress" type="LineEdit" parent="Panel"]
|
[node name="IPAddress" type="LineEdit" parent="Panel"]
|
||||||
|
|
||||||
anchor_left = 0.5
|
anchor_left = 0.5
|
||||||
|
@ -1,24 +1,31 @@
|
|||||||
using Godot;
|
using Godot;
|
||||||
using System;
|
using System;
|
||||||
using Network;
|
using Network;
|
||||||
|
using Util;
|
||||||
|
|
||||||
public class MainMenu : Panel {
|
public class MainMenu : Panel, PeerListener {
|
||||||
|
|
||||||
private Net Net;
|
private Net Net;
|
||||||
|
|
||||||
private LineEdit IPAddressEdit;
|
private LineEdit IPAddressEdit;
|
||||||
private LineEdit PortEdit;
|
private LineEdit PortEdit;
|
||||||
|
|
||||||
|
private Label StatusText;
|
||||||
|
|
||||||
public override void _Ready() {
|
public override void _Ready() {
|
||||||
Net = (Net) GetNode("/root/Net");
|
Net = (Net) GetNode("/root/Net");
|
||||||
var IPAddressNode = GetNode("IPAddress");
|
var IPAddressNode = GetNode("IPAddress");
|
||||||
var PortNode = GetNode("Port");
|
var PortNode = GetNode("Port");
|
||||||
|
var StatusNode = GetNode("StatusText");
|
||||||
if (IPAddressNode is LineEdit) {
|
if (IPAddressNode is LineEdit) {
|
||||||
IPAddressEdit = (LineEdit) IPAddressNode;
|
IPAddressEdit = (LineEdit) IPAddressNode;
|
||||||
}
|
}
|
||||||
if (PortNode is LineEdit) {
|
if (PortNode is LineEdit) {
|
||||||
PortEdit = (LineEdit) PortNode;
|
PortEdit = (LineEdit) PortNode;
|
||||||
}
|
}
|
||||||
|
if (StatusNode is Label) {
|
||||||
|
StatusText = (Label) StatusNode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnLaunchServer() {
|
private void OnLaunchServer() {
|
||||||
@ -35,7 +42,11 @@ public class MainMenu : Panel {
|
|||||||
string address;
|
string address;
|
||||||
int port;
|
int port;
|
||||||
SetAddressAndPort(out address, out port, "localhost", 7070);
|
SetAddressAndPort(out address, out port, "localhost", 7070);
|
||||||
Net.StartClient(address, port);
|
var Client = Net.StartClient(address, port);
|
||||||
|
if (Client != null) {
|
||||||
|
Client.AddPeerListener(this);
|
||||||
|
}
|
||||||
|
StatusText.Text = "Connecting..";
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetAddressAndPort(out string address, out int port, string defaultAddress, int defaultPort) {
|
private void SetAddressAndPort(out string address, out int port, string defaultAddress, int defaultPort) {
|
||||||
@ -53,4 +64,18 @@ public class MainMenu : Panel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnStart() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnStop() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnConnect(Connection conn) {
|
||||||
|
StatusText.Text = "Connecting..";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnDisconnect(Connection conn, DisconnectReason reason) {
|
||||||
|
StatusText.Text = "Disconnected, reason: " + reason.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,7 @@ namespace Network {
|
|||||||
public class Client : Peer {
|
public class Client : Peer {
|
||||||
|
|
||||||
private PacketPeerUDP PacketPeer;
|
private PacketPeerUDP PacketPeer;
|
||||||
|
|
||||||
private Connection ServerConn;
|
private Connection ServerConn;
|
||||||
|
|
||||||
private float Timer = 0;
|
private float Timer = 0;
|
||||||
|
|
||||||
public Client(PacketPeerUDP packetPeer) : base(packetPeer, false) {
|
public Client(PacketPeerUDP packetPeer) : base(packetPeer, false) {
|
||||||
@ -22,6 +20,7 @@ namespace Network {
|
|||||||
ServerConn = new Connection(address, port);
|
ServerConn = new Connection(address, port);
|
||||||
ServerConn.Name = "Server";
|
ServerConn.Name = "Server";
|
||||||
ConnectionList.AddConnection(ServerConn);
|
ConnectionList.AddConnection(ServerConn);
|
||||||
|
SendBuffer(new PacketBuffer(), ServerConn);
|
||||||
StartListening(port, "*");
|
StartListening(port, "*");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +32,7 @@ namespace Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override void Process(float delta) {
|
public override void Process(float delta) {
|
||||||
|
if (Initialized) {
|
||||||
Timer += delta;
|
Timer += delta;
|
||||||
if (Timer > 10) {
|
if (Timer > 10) {
|
||||||
Timer = 0;
|
Timer = 0;
|
||||||
@ -42,13 +42,17 @@ namespace Network {
|
|||||||
GD.print("Hello'd the server!");
|
GD.print("Hello'd the server!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override void Connected(Connection conn) {
|
public override void Connected(Connection conn) {
|
||||||
GD.print("Connected to " + conn.Name + " at " + conn.Address + ":" + conn.Port);
|
GD.print("Connected to " + conn.Name + " at " + conn.Address + ":" + conn.Port);
|
||||||
PacketDistributor.AddHandler(ServerConn);
|
PacketDistributor.AddHandler(ServerConn);
|
||||||
|
foreach (PeerListener listener in PeerListeners) {
|
||||||
|
listener.OnConnect(conn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Disconnected(Connection conn) {
|
public override void Disconnected(Connection conn, DisconnectReason reason) {
|
||||||
Stop(); // Stop the client if it disconnects.
|
Stop(); // Stop the client if it disconnects.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,16 +25,18 @@ namespace Network {
|
|||||||
if (IsClient) { Client.Free(); }
|
if (IsClient) { Client.Free(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartClient(string address, int port) {
|
public Client StartClient(string address, int port) {
|
||||||
if (IsClient || IsServer) { return; }
|
if (IsServer) { return null; }
|
||||||
Client = new Client(PacketPeer);
|
Client = new Client(PacketPeer);
|
||||||
Client.Start(address, port);
|
Client.Start(address, port);
|
||||||
|
return Client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartServer(string address, int port) {
|
public Server StartServer(string address, int port) {
|
||||||
if (IsClient || IsServer) { return; }
|
if (IsClient) { return null; }
|
||||||
Server = new Server(PacketPeer);
|
Server = new Server(PacketPeer);
|
||||||
Server.Start(address, port);
|
Server.Start(address, port);
|
||||||
|
return Server;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using Godot;
|
using Godot;
|
||||||
using Network.PacketHandling;
|
using Network.PacketHandling;
|
||||||
using Network.Syncing;
|
using Network.Syncing;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Util;
|
using Util;
|
||||||
using Thread = System.Threading.Thread;
|
using Thread = System.Threading.Thread;
|
||||||
@ -13,7 +14,7 @@ namespace Network {
|
|||||||
private static PacketPeerUDP PacketPeer;
|
private static PacketPeerUDP PacketPeer;
|
||||||
private static Peer Singleton;
|
private static Peer Singleton;
|
||||||
public readonly bool IsServer;
|
public readonly bool IsServer;
|
||||||
private bool Initialized = false;
|
public bool Initialized { get; private set; } = false;
|
||||||
|
|
||||||
public Protocol Protocol;
|
public Protocol Protocol;
|
||||||
|
|
||||||
@ -26,6 +27,8 @@ namespace Network {
|
|||||||
|
|
||||||
private bool ShouldListen = false;
|
private bool ShouldListen = false;
|
||||||
|
|
||||||
|
public List<PeerListener> PeerListeners = new List<PeerListener>();
|
||||||
|
|
||||||
public Peer(PacketPeerUDP packetPeer, bool isServer) {
|
public Peer(PacketPeerUDP packetPeer, bool isServer) {
|
||||||
PacketPeer = packetPeer;
|
PacketPeer = packetPeer;
|
||||||
IsServer = isServer;
|
IsServer = isServer;
|
||||||
@ -40,28 +43,36 @@ namespace Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Start(string address, int port) {
|
public void Start(string address, int port) {
|
||||||
|
if (Initialized) { return; }
|
||||||
Singleton = this;
|
Singleton = this;
|
||||||
Initialize(address, port);
|
Initialize(address, port);
|
||||||
Initialized = true;
|
Initialized = true;
|
||||||
|
foreach (PeerListener listener in PeerListeners) {
|
||||||
|
listener.OnStart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop() {
|
public void Stop() {
|
||||||
|
if (!Initialized) { return; }
|
||||||
Uninitialize();
|
Uninitialize();
|
||||||
Initialized = false;
|
Initialized = false;
|
||||||
|
foreach (PeerListener listener in PeerListeners) {
|
||||||
|
listener.OnStop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void Initialize(string address, int port);
|
public abstract void Initialize(string address, int port);
|
||||||
public abstract void Uninitialize();
|
public abstract void Uninitialize();
|
||||||
public abstract void Process(float delta);
|
public abstract void Process(float delta);
|
||||||
public abstract void Connected(Connection conn);
|
public abstract void Connected(Connection conn);
|
||||||
public abstract void Disconnected(Connection conn);
|
public abstract void Disconnected(Connection conn, DisconnectReason reason);
|
||||||
|
|
||||||
public void SendBuffer(PacketBuffer packetBuffer, Connection to) {
|
public int SendBuffer(PacketBuffer packetBuffer, Connection to) {
|
||||||
if (LastConnectionSended != to.ID) {
|
if (LastConnectionSended != to.ID) {
|
||||||
PacketPeer.SetDestAddress(to.Address, to.Port);
|
PacketPeer.SetDestAddress(to.Address, to.Port);
|
||||||
LastConnectionSended = to.ID;
|
LastConnectionSended = to.ID;
|
||||||
}
|
}
|
||||||
PacketPeer.PutPacket(packetBuffer.ByteBuffer);
|
return PacketPeer.PutPacket(packetBuffer.ByteBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool StartListening(int port, string address = "*") {
|
public bool StartListening(int port, string address = "*") {
|
||||||
@ -85,6 +96,19 @@ namespace Network {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Connection GetOrAddConnection(string address, int port) {
|
||||||
|
Optional<Connection> Conn = ConnectionList.GetOriginal(address, port);
|
||||||
|
if (Conn.isEmpty) {
|
||||||
|
Conn = new Connection(address, port);
|
||||||
|
ConnectionList.AddConnection(Conn);
|
||||||
|
PacketDistributor.AddHandler(Conn);
|
||||||
|
foreach (PeerListener Listener in PeerListeners) {
|
||||||
|
Listener.OnConnect(Conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Conn;
|
||||||
|
}
|
||||||
|
|
||||||
private static void ListenerThreadMethod() {
|
private static void ListenerThreadMethod() {
|
||||||
GD.print("Started Listener Thread.");
|
GD.print("Started Listener Thread.");
|
||||||
|
|
||||||
@ -108,12 +132,7 @@ namespace Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Confirmed) {
|
if (Confirmed) {
|
||||||
Optional<Connection> Conn = Singleton.ConnectionList.GetOriginal(Address, Port);
|
Connection Conn = Singleton.GetOrAddConnection(Address, Port);
|
||||||
if (Conn.isEmpty) {
|
|
||||||
Conn = new Connection(Address, Port);
|
|
||||||
Singleton.ConnectionList.AddConnection(Conn);
|
|
||||||
Singleton.PacketDistributor.AddHandler(Conn);
|
|
||||||
}
|
|
||||||
Singleton.PacketDistributor.HandleRawPacket(PB, Singleton.ConnectionList.GetOriginal(Conn));
|
Singleton.PacketDistributor.HandleRawPacket(PB, Singleton.ConnectionList.GetOriginal(Conn));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,6 +158,18 @@ namespace Network {
|
|||||||
|
|
||||||
ConnectionList.RemoveConnection(conn);
|
ConnectionList.RemoveConnection(conn);
|
||||||
PacketDistributor.RemoveHandler(conn);
|
PacketDistributor.RemoveHandler(conn);
|
||||||
|
Disconnected(conn, reason);
|
||||||
|
foreach (PeerListener Listener in PeerListeners) {
|
||||||
|
Listener.OnDisconnect(conn, reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddPeerListener(PeerListener listener) {
|
||||||
|
PeerListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemovePeerListener(PeerListener listener) {
|
||||||
|
PeerListeners.Remove(listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
scripts/net/PeerListener.cs
Normal file
15
scripts/net/PeerListener.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Util;
|
||||||
|
|
||||||
|
namespace Network {
|
||||||
|
public interface PeerListener {
|
||||||
|
|
||||||
|
void OnStart();
|
||||||
|
void OnStop();
|
||||||
|
|
||||||
|
void OnConnect(Connection conn);
|
||||||
|
void OnDisconnect(Connection conn, DisconnectReason reason);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -39,7 +39,7 @@ namespace Network {
|
|||||||
GD.print("Someone connected at " + conn.Address + ":" + conn.Port);
|
GD.print("Someone connected at " + conn.Address + ":" + conn.Port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Disconnected(Connection conn) {
|
public override void Disconnected(Connection conn, DisconnectReason reason) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
namespace Util {
|
namespace Util {
|
||||||
public enum DisconnectReason {
|
public enum DisconnectReason {
|
||||||
TIMEOUT, MANUAL_DISCONNECT
|
TIMEOUT, MANUAL_DISCONNECT, INVALID_ADDRESS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user