Add PeerListener and make Menu more visible

This commit is contained in:
Sofia 2017-11-25 18:45:48 +02:00
parent 545a3cc5c5
commit 634b260e18
9 changed files with 128 additions and 28 deletions

View File

@ -62,11 +62,13 @@
<Compile Include="scripts\net\packethandling\PrimitiveSerializer.cs" />
<Compile Include="scripts\net\packethandling\Serializable.cs" />
<Compile Include="scripts\net\Peer.cs" />
<Compile Include="scripts\net\PeerListener.cs" />
<Compile Include="scripts\net\Server.cs" />
<Compile Include="scripts\net\syncing\PacketDistributor.cs" />
<Compile Include="scripts\net\syncing\ConnectionHandler.cs" />
<Compile Include="scripts\util\DisconnectReason.cs" />
<Compile Include="scripts\util\Optional.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -139,6 +139,27 @@ lines_skipped = 0
max_lines_visible = -1
_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"]
anchor_left = 0.5

View File

@ -1,24 +1,31 @@
using Godot;
using System;
using Network;
using Util;
public class MainMenu : Panel {
public class MainMenu : Panel, PeerListener {
private Net Net;
private LineEdit IPAddressEdit;
private LineEdit PortEdit;
private Label StatusText;
public override void _Ready() {
Net = (Net) GetNode("/root/Net");
var IPAddressNode = GetNode("IPAddress");
var PortNode = GetNode("Port");
var StatusNode = GetNode("StatusText");
if (IPAddressNode is LineEdit) {
IPAddressEdit = (LineEdit) IPAddressNode;
}
if (PortNode is LineEdit) {
PortEdit = (LineEdit) PortNode;
}
if (StatusNode is Label) {
StatusText = (Label) StatusNode;
}
}
private void OnLaunchServer() {
@ -35,7 +42,11 @@ public class MainMenu : Panel {
string address;
int port;
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) {
@ -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();
}
}

View File

@ -8,9 +8,7 @@ namespace Network {
public class Client : Peer {
private PacketPeerUDP PacketPeer;
private Connection ServerConn;
private float Timer = 0;
public Client(PacketPeerUDP packetPeer) : base(packetPeer, false) {
@ -22,6 +20,7 @@ namespace Network {
ServerConn = new Connection(address, port);
ServerConn.Name = "Server";
ConnectionList.AddConnection(ServerConn);
SendBuffer(new PacketBuffer(), ServerConn);
StartListening(port, "*");
}
@ -33,22 +32,27 @@ namespace Network {
}
public override void Process(float delta) {
Timer += delta;
if (Timer > 10) {
Timer = 0;
TextMessagePkt Pkt = new TextMessagePkt();
Pkt.Text = "Hello, Server!";
PacketDistributor.AddReliableForAll(Pkt);
GD.print("Hello'd the server!");
if (Initialized) {
Timer += delta;
if (Timer > 10) {
Timer = 0;
TextMessagePkt Pkt = new TextMessagePkt();
Pkt.Text = "Hello, Server!";
PacketDistributor.AddReliableForAll(Pkt);
GD.print("Hello'd the server!");
}
}
}
public override void Connected(Connection conn) {
GD.print("Connected to " + conn.Name + " at " + conn.Address + ":" + conn.Port);
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.
}
}

View File

@ -25,16 +25,18 @@ namespace Network {
if (IsClient) { Client.Free(); }
}
public void StartClient(string address, int port) {
if (IsClient || IsServer) { return; }
public Client StartClient(string address, int port) {
if (IsServer) { return null; }
Client = new Client(PacketPeer);
Client.Start(address, port);
return Client;
}
public void StartServer(string address, int port) {
if (IsClient || IsServer) { return; }
public Server StartServer(string address, int port) {
if (IsClient) { return null; }
Server = new Server(PacketPeer);
Server.Start(address, port);
return Server;
}
}
}

View File

@ -1,6 +1,7 @@
using Godot;
using Network.PacketHandling;
using Network.Syncing;
using System.Collections.Generic;
using System.Threading;
using Util;
using Thread = System.Threading.Thread;
@ -13,7 +14,7 @@ namespace Network {
private static PacketPeerUDP PacketPeer;
private static Peer Singleton;
public readonly bool IsServer;
private bool Initialized = false;
public bool Initialized { get; private set; } = false;
public Protocol Protocol;
@ -26,6 +27,8 @@ namespace Network {
private bool ShouldListen = false;
public List<PeerListener> PeerListeners = new List<PeerListener>();
public Peer(PacketPeerUDP packetPeer, bool isServer) {
PacketPeer = packetPeer;
IsServer = isServer;
@ -40,28 +43,36 @@ namespace Network {
}
public void Start(string address, int port) {
if (Initialized) { return; }
Singleton = this;
Initialize(address, port);
Initialized = true;
foreach (PeerListener listener in PeerListeners) {
listener.OnStart();
}
}
public void Stop() {
if (!Initialized) { return; }
Uninitialize();
Initialized = false;
foreach (PeerListener listener in PeerListeners) {
listener.OnStop();
}
}
public abstract void Initialize(string address, int port);
public abstract void Uninitialize();
public abstract void Process(float delta);
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) {
PacketPeer.SetDestAddress(to.Address, to.Port);
LastConnectionSended = to.ID;
}
PacketPeer.PutPacket(packetBuffer.ByteBuffer);
return PacketPeer.PutPacket(packetBuffer.ByteBuffer);
}
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() {
GD.print("Started Listener Thread.");
@ -108,12 +132,7 @@ namespace Network {
}
if (Confirmed) {
Optional<Connection> Conn = Singleton.ConnectionList.GetOriginal(Address, Port);
if (Conn.isEmpty) {
Conn = new Connection(Address, Port);
Singleton.ConnectionList.AddConnection(Conn);
Singleton.PacketDistributor.AddHandler(Conn);
}
Connection Conn = Singleton.GetOrAddConnection(Address, Port);
Singleton.PacketDistributor.HandleRawPacket(PB, Singleton.ConnectionList.GetOriginal(Conn));
}
}
@ -139,6 +158,18 @@ namespace Network {
ConnectionList.RemoveConnection(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);
}
}
}

View 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);
}
}

View File

@ -39,7 +39,7 @@ namespace Network {
GD.print("Someone connected at " + conn.Address + ":" + conn.Port);
}
public override void Disconnected(Connection conn) {
public override void Disconnected(Connection conn, DisconnectReason reason) {
}
}
}

View File

@ -1,6 +1,6 @@

namespace Util {
public enum DisconnectReason {
TIMEOUT, MANUAL_DISCONNECT
TIMEOUT, MANUAL_DISCONNECT, INVALID_ADDRESS
}
}