Made Peer handle Listening

This commit is contained in:
Sofia 2017-11-21 23:23:12 +02:00
parent f8be410575
commit 8bc8b692a8
5 changed files with 88 additions and 30 deletions

View File

@ -179,7 +179,7 @@ size_flags_horizontal = 1
size_flags_vertical = 1
expand_to_len = false
focus_mode = 2
placeholder_text = "8080"
placeholder_text = "7070"
placeholder_alpha = 0.6
caret_blink = false
caret_blink_speed = 0.65

View File

@ -17,6 +17,11 @@ public class Client : Peer {
ServerConn = new Connection(address, port);
}
public override void ReceivePacket(byte[] buffer, string address, int port) {
GD.print("Client received stuff from " + address + " : " + port + " :");
GD.print(buffer[0]);
}
public override void Update(float delta) {
GD.print("Update client.");
SendBuffer(TempBuffer, ServerConn);

View File

@ -6,32 +6,62 @@ public class Net : Node {
private Client Client;
private Server Server;
public bool IsClient { get { return Client != null; } }
public bool IsServer { get { return Server != null; } }
public override void _Ready() {
PacketPeer = new PacketPeerUDP();
}
public override void _Process(float delta) {
if (IsServer()) { Server.Update(delta); }
if (IsClient()) { Client.Update(delta); }
if (IsServer) { Server.Update(delta); }
if (IsClient) { Client.Update(delta); }
}
public override void _ExitTree() {
if (IsServer) { Server.Free(); }
if (IsClient) { Client.Free(); }
}
public void StartClient() {
//if (IsClient() || IsServer()) { return; }
Client = new Client(PacketPeer);
Client.Initialize("localhost", 8080);
Client.Initialize("localhost", 7070);
}
public void StartServer() {
//if (IsClient() || IsServer()) { return; }
Server = new Server(PacketPeer);
Server.Initialize("*", 8080);
Server.Initialize("*", 7070);
}
}
public bool IsClient() {
return Client != null;
}
//extends Node2D
public bool IsServer() {
return Server != null;
}
}
//# Member variables
//var thread = Thread.new()
//# This function runs in a thread!
//# Threads always take one userdata argument
//func _bg_load(path):
// print("THREAD FUNC!")
// # Load the resource
// var tex = ResourceLoader.load(path)
// # Call _bg_load_done on main thread
// call_deferred("_bg_load_done")
// return tex # return it
//func _bg_load_done():
// # Wait for the thread to complete, get the returned value
// var tex = thread.wait_to_finish()
// # Set to the sprite
// get_node("sprite").set_texture(tex)
//func _on_load_pressed():
// if (thread.is_active()):
// # Already working
// return
// print("START THREAD!")
// thread.start(self, "_bg_load", "res://mona.png")

View File

@ -1,10 +1,13 @@
using Godot;
using System.Threading;
using Thread = System.Threading.Thread;
public abstract class Peer : Object {
private PacketPeerUDP PacketPeer;
private static PacketPeerUDP PacketPeer;
private static Peer Singleton;
private int LastConnectionSended = -1;
private Thread ListenerThread;
public Peer(PacketPeerUDP packetPeer) {
PacketPeer = packetPeer;
@ -12,11 +15,36 @@ public abstract class Peer : Object {
public abstract void Initialize(string address, int port);
public abstract void Update(float delta);
public abstract void ReceivePacket(byte[] buffer, string address, int port);
public void SendBuffer(byte[] buffer, Connection to) {
int temp1 = PacketPeer.SetDestAddress(to.Address, to.Port);
int temp2 = PacketPeer.PutPacket(buffer);
GD.print("Putting stuff to " + to.Address + ":" + to.Port);
PacketPeer.SetDestAddress(to.Address, to.Port);
PacketPeer.PutPacket(buffer);
}
public bool StartListening(int port, string address = "*") {
if (PacketPeer.IsListening() || ListenerThread != null) {
GD.printerr("The Peer is already listening or the thread is active!");
return false;
}
Singleton = this;
PacketPeer.Listen(port, address);
ThreadStart ChildRef = new ThreadStart(ListenerThreadMethod);
ListenerThread = new Thread(ChildRef);
ListenerThread.Start();
return true;
}
public static void ListenerThreadMethod() {
GD.print("Started Listener Thread.");
while (true) {
PacketPeer.Wait();
byte[] Buffer = PacketPeer.GetPacket();
string Address = PacketPeer.GetPacketIp();
int Port = PacketPeer.GetPacketPort();
Singleton.ReceivePacket(Buffer, Address, Port);
}
}
}

View File

@ -10,22 +10,17 @@ public class Server : Peer {
}
public override void Initialize(string address, int port) {
PacketPeer.Listen(port, address);
ThreadStart childref = new ThreadStart(ListenerThread);
System.Threading.Thread thread = new System.Threading.Thread(childref);
thread.Start();
StartListening(port, address);
GD.print("Server initialization finished.");
}
public override void ReceivePacket(byte[] buffer, string address, int port) {
GD.print("Server received stuff from " + address + " : " + port + " :");
for (int i = 0; i < buffer.Length; i++) {
GD.print(buffer[i]);
}
}
public override void Update(float delta) {
}
public static void ListenerThread() {
GD.print("Started Listener Thread.");
while (true) {
PacketPeer.Wait();
GD.print("Received stuff from " + PacketPeer.GetPacketIp() + " : " + PacketPeer.GetPacketPort() + " :");
GD.print(PacketPeer.GetPacket()[0]);
}
}
}