Browse Source

fixed to complitable and bind udp port to send data

master
jkpete 2 months ago
parent
commit
ea6b0023a7
  1. 1
      Font/SourceHanSansCN-Regular.otf.import
  2. 27
      addons/EGFramework/Module/ProtocolTools/EGUDP.cs
  3. 1
      project.godot

1
Font/SourceHanSansCN-Regular.otf.import

@ -15,6 +15,7 @@ dest_files=["res://.godot/imported/SourceHanSansCN-Regular.otf-a74baadd242fcbfb4
Rendering=null Rendering=null
antialiasing=1 antialiasing=1
generate_mipmaps=false generate_mipmaps=false
disable_embedded_bitmaps=true
multichannel_signed_distance_field=false multichannel_signed_distance_field=false
msdf_pixel_range=8 msdf_pixel_range=8
msdf_size=48 msdf_size=48

27
addons/EGFramework/Module/ProtocolTools/EGUDP.cs

@ -1,10 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading.Tasks; using Godot;
namespace EGFramework{ namespace EGFramework{
public class EGUDP : IEGFramework, IModule, IProtocolSend, IProtocolReceived public class EGUDP : IEGFramework, IModule, IProtocolSend, IProtocolReceived
{ {
@ -34,6 +33,7 @@ namespace EGFramework{
{ {
UdpClient udpDevice = new UdpClient(localPort); UdpClient udpDevice = new UdpClient(localPort);
UDPDevices.Add(localPort,udpDevice); UDPDevices.Add(localPort,udpDevice);
//udpDevice.EnableBroadcast = true;
HandleUDPListenAsync(udpDevice); HandleUDPListenAsync(udpDevice);
//StartListening(localPort); //StartListening(localPort);
} }
@ -54,6 +54,7 @@ namespace EGFramework{
{ {
try try
{ {
//GD.Print("UDP listened in "+((IPEndPoint)client.Client.LocalEndPoint).Port);
while (true) while (true)
{ {
UdpReceiveResult data = await client.ReceiveAsync(); UdpReceiveResult data = await client.ReceiveAsync();
@ -63,21 +64,30 @@ namespace EGFramework{
ResponseMsgs.Enqueue(receivedMsgs); ResponseMsgs.Enqueue(receivedMsgs);
} }
} }
catch (Exception) catch (Exception e)
{ {
GD.Print("Listen false by:"+e);
} }
} }
public void SendByteData(string host,int port,byte[] data){ public void SendByteData(string host,int port,byte[] data){
UdpClient udpClient = new UdpClient(); UdpClient udpClient;
if(UDPDevices.Count>0){
udpClient = UDPDevices.First().Value;
}else{
udpClient = new UdpClient();
}
try{ try{
GD.Print(udpClient.EnableBroadcast);
udpClient.Send(data, data.Length, host, port); udpClient.Send(data, data.Length, host, port);
} }
catch ( Exception e ){ catch ( Exception e ){
Godot.GD.Print(e.ToString()); Godot.GD.Print(e.ToString());
} }
udpClient.Close(); if(UDPDevices.Count<=0){
udpClient.Dispose(); udpClient.Close();
udpClient.Dispose();
}
} }
public void SendByteData(string destination,byte[] data){ public void SendByteData(string destination,byte[] data){
SendByteData(destination.GetHostByIp(),destination.GetPortByIp(),data); SendByteData(destination.GetHostByIp(),destination.GetPortByIp(),data);
@ -118,6 +128,9 @@ namespace EGFramework{
public static void EGUDPListen(this IEGFramework self ,int port){ public static void EGUDPListen(this IEGFramework self ,int port){
self.GetModule<EGUDP>().ListenUDP(port); self.GetModule<EGUDP>().ListenUDP(port);
} }
public static void EGUDPEndListen(this IEGFramework self ,int port){
self.GetModule<EGUDP>().EndListenUDP(port);
}
} }
} }

1
project.godot

@ -88,4 +88,5 @@ jump={
[rendering] [rendering]
renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility" renderer/rendering_method.mobile="gl_compatibility"

Loading…
Cancel
Save