SmartOS/Drivers/Esp8266/EspUdp.cpp

36 lines
838 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "EspUdp.h"
/******************************** Udp ********************************/
EspUdp::EspUdp(Esp8266& host, byte idx)
: EspSocket(host, NetType::Udp, idx)
{
}
bool EspUdp::SendTo(const Buffer& bs, const IPEndPoint& remote)
{
//!!! ESP8266有BUG收到数据后远程地址还是乱了所以这里的远程地址跟实际可能不一致
if(remote == Remote) return Send(bs);
if(!Open()) return false;
String cmd = "AT+CIPSEND=";
cmd = cmd + _Index + ',' + bs.Length();
// 加上远程IP和端口
cmd = cmd + ",\"" + remote.Address + "\"";
cmd = cmd + ',' + remote.Port;
cmd += "\r\n";
return SendData(cmd, bs);
}
bool EspUdp::OnWriteEx(const Buffer& bs, const void* opt)
{
auto ep = (IPEndPoint*)opt;
if(!ep) return OnWrite(bs);
return SendTo(bs, *ep);
}