Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions LuaApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@

## C#层增加的接口(底层接口)

### 通用通道一览

`apiSend` / `apiSetCb` 里能用的通道名称,每个通道对应软件里的一个选项卡,完整用法见软件自带的 `channel-demo.lua`。

|channel|对应功能|回调收到的数据|发送用法|
|-|-|-|-|
|`uart`|串口|string,收到的原始字节|`apiSend("uart",data)`|
|`mqtt`|MQTT 选项卡|table:`topic` / `payload` / `qos`|`apiSend("mqtt",nil,{topic=…,payload=…,qos=…})`|
|`tcp-server`|本机TCP服务端 选项卡|table:`from` / `data`;`from` 为 `"connected"` / `"disconnected"` 时表示客户端连上或断开|`apiSend("tcp-server",data)`,广播给所有客户端|
|`socket-client`|socket客户端 选项卡|string,收到的字节|`apiSend("socket-client",data)`|
|`netlab`|socket公共服务端 选项卡|table:`client` / `data`;`client` 为 `"connected"` / `"disconnected"` 时表示客户端连上或断开|`apiSend("netlab",nil,{client=…,data=…})`|
|`winusb`|winusb 选项卡|string,收到的字节|`apiSend("winusb",data)`|
|`udp-server`|本机UDP服务端 选项卡|table:`from`(`"ip:port"`)/ `data`|`apiSend("udp-server",data)` 回给最近一次发数据的地址;也可 `apiSend("udp-server",nil,{from="ip:port",data=…})` 指定目标|

### apiSend(channel,data[,table])

发送数据到某个通道,具体用法请见软件自带的`channel-demo.lua`例子
Expand Down
16 changes: 16 additions & 0 deletions llcom/DefaultFiles/user_script_run/channel-demo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ apiSetCb("winusb",function (data)
log.info("winusb received", data)
end)
local sendResult = apiSend("winusb",string.char(0,4))

-- udp-server,对应 本机UDP服务端 选项卡
apiSetCb("udp-server",function (data)
log.info(
"udp-server received",
data.from,
data.data)
end)
-- 不带table发送时,数据会回给最近一次发数据过来的那个地址
local sendResult = apiSend("udp-server","echo from lua!")
-- 也可以带table指定目标地址,此时from填"ip:port"
-- local sendResult = apiSend("udp-server",nil,
-- {
-- from = "127.0.0.1:8080",
-- data = "test data~"
-- })
62 changes: 62 additions & 0 deletions llcom/Pages/UdpLocalPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Windows.Navigation;
using System.Windows.Shapes;
using static llcom.Pages.SocketClientPage;
using llcom.LuaEnv;
using System.Net.NetworkInformation;

namespace llcom.Pages
Expand Down Expand Up @@ -43,6 +44,25 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
//绑定
MainGrid.DataContext = this;
IpPortTextBox.DataContext = Tools.Global.setting;

//适配一下通用通道:收到的数据转给lua;lua发过来的数据回给最近一次发数据的地址
LuaApis.SendChannelsRegister("udp-server", (data, table) =>
{
if (Server == null)
return false;
var payload = data ?? table?.Get<byte[]>("data");
if (payload == null)
return false;
var target = ParseEndPoint(table?.Get<string>("from")) ?? lastRemote;
if (target == null)
return false;
try
{
Server.Send(payload, payload.Length, target);
return true;
}
catch { return false; }
});
}

/// <summary>
Expand Down Expand Up @@ -81,7 +101,40 @@ private void ShowData(string title, byte[] data = null, bool send = false)
}


/// <summary>
/// 解析 "ip:port" / "[ipv6]:port" 形式的地址,失败返回null
/// </summary>
private static IPEndPoint ParseEndPoint(string addr)
{
if (string.IsNullOrWhiteSpace(addr))
return null;
addr = addr.Trim();
string host;
string port;
if (addr.StartsWith("["))
{
var end = addr.IndexOf(']');
if (end <= 0 || end + 1 >= addr.Length || addr[end + 1] != ':')
return null;
host = addr.Substring(1, end - 1);
port = addr.Substring(end + 2);
}
else
{
var idx = addr.LastIndexOf(':');
if (idx <= 0)
return null;
host = addr.Substring(0, idx);
port = addr.Substring(idx + 1);
}
if (!IPAddress.TryParse(host, out var ip) || !ushort.TryParse(port, out var p))
return null;
return new IPEndPoint(ip, p);
}

private UdpClient Server = null;
//最近一次收到数据的来源,通用通道发数据时就用它
private IPEndPoint lastRemote = null;

/// <summary>
/// 开始监听服务器
Expand Down Expand Up @@ -111,6 +164,14 @@ private bool StartServer(string ip, int port)
byte[] receiveBytes = u.EndReceive(ar, ref e);
var isV6 = e.Address.ToString().Contains(":");
ShowData($"{(isV6 ? "[" : "")}{e.Address}{(isV6 ? "]" : "")}:{e.Port}", receiveBytes);
lastRemote = new IPEndPoint(e.Address, e.Port);
//通用通道收到消息
LuaApis.SendChannelsReceived("udp-server",
new
{
from = $"{(isV6 ? "[" : "")}{e.Address}{(isV6 ? "]" : "")}:{e.Port}",
data = receiveBytes
});
Server.BeginReceive(newConnectionCb, ar.AsyncState);
}
catch { }
Expand Down Expand Up @@ -139,6 +200,7 @@ private void StopServer()
Server?.Close();
Server?.Dispose();
Server = null;
lastRemote = null;
}

private void RefreshIpButton_Click(object sender, RoutedEventArgs e)
Expand Down
Loading