This commit is contained in:
azw 2023-04-24 11:42:23 +00:00
parent 2444f1b8e2
commit 4fb98b218b
3 changed files with 18 additions and 19 deletions

View File

@ -18,14 +18,6 @@
namespace a8
{
long long UdpPacket::GetRemoteKey()
{
unsigned long addr = remote_addr.sin_addr.s_addr;
int port = remote_addr.sin_port;
long long key = a8::MakeInt64(addr, port);
return key;
}
struct UdpListenerImpl
{
a8::UdpListener* master = nullptr;
@ -69,7 +61,12 @@ namespace a8
// TIME_WAIT - argh
int on = 1;
if (::setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) == -1 ){
//return false;
if (master->on_error){
master->on_error(errno);
}
::close(listen_socket);
listen_socket = a8::INVALID_SOCKET;
return false;
}
sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
@ -131,13 +128,12 @@ namespace a8
UdpListener::UdpListener()
{
impl_ = new a8::UdpListenerImpl();
impl_ = std::make_shared<a8::UdpListenerImpl>();
impl_->master = this;
}
UdpListener::~UdpListener()
{
delete impl_;
impl_ = nullptr;
}
@ -162,7 +158,6 @@ namespace a8
void UdpListener::SendUdpPacket(UdpPacket* pkt)
{
//a8::XPrintf("SendUdpPacket buflen:%d\n", {pkt->buf_len});
::sendto(impl_->listen_socket,
pkt->buf,
pkt->buf_len,

View File

@ -7,16 +7,22 @@
namespace a8
{
enum UDPLISTENER_E
{
UE_CREATE_ERR,
UE_SETSOCKOPT_ERR,
UE_BIND_ERR,
UE_LISTEN_ERR,
};
struct UdpPacket
{
const char* buf = nullptr;
int buf_len = 0;
sockaddr_in remote_addr;
long long GetRemoteKey();
sockaddr_in remote_addr = {};
};
struct UdpListenerImpl;
class UdpListener
{
public:
@ -35,7 +41,7 @@ namespace a8
virtual void SendUdpPacket(UdpPacket* pkt);
private:
a8::UdpListenerImpl* impl_ = nullptr;
std::shared_ptr<struct UdpListenerImpl> impl_;
};
}

View File

@ -9,7 +9,6 @@ namespace a8
{
struct UdpPacket;
class UdpListener;
class UdpSession
{
@ -28,7 +27,6 @@ namespace a8
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) = 0;
private:
a8::UdpListener* listener_ = nullptr;
long long remote_key_ = 0;
int socket_handle_ = 0;
sockaddr_in remote_addr_ = {};