Commit 8d586161 authored by Edoardo Lolletti's avatar Edoardo Lolletti Committed by DailyShana

Clean up broadcasting code (#2218)

parent dbb12f03
...@@ -3809,45 +3809,24 @@ void DuelClient::BeginRefreshHost() { ...@@ -3809,45 +3809,24 @@ void DuelClient::BeginRefreshHost() {
if(!host) if(!host)
return; return;
SOCKET reply = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); SOCKET reply = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in reply_addr; BOOL opt = TRUE;
memset(&reply_addr, 0, sizeof(reply_addr)); setsockopt(reply, SOL_SOCKET, SO_BROADCAST, (const char*)&opt, sizeof(BOOL));
reply_addr.sin_family = AF_INET;
reply_addr.sin_port = htons(7921);
reply_addr.sin_addr.s_addr = 0;
if(bind(reply, (sockaddr*)&reply_addr, sizeof(reply_addr)) == SOCKET_ERROR) {
closesocket(reply);
return;
}
timeval timeout = {3, 0}; timeval timeout = {3, 0};
resp_event = event_new(broadev, reply, EV_TIMEOUT | EV_READ | EV_PERSIST, BroadcastReply, broadev); resp_event = event_new(broadev, reply, EV_TIMEOUT | EV_READ | EV_PERSIST, BroadcastReply, broadev);
event_add(resp_event, &timeout); event_add(resp_event, &timeout);
Thread::NewThread(RefreshThread, broadev); Thread::NewThread(RefreshThread, broadev);
//send request //send request
SOCKADDR_IN local; SOCKADDR_IN sockTo = {};
local.sin_family = AF_INET;
local.sin_port = htons(7922);
SOCKADDR_IN sockTo;
sockTo.sin_addr.s_addr = htonl(INADDR_BROADCAST);
sockTo.sin_family = AF_INET; sockTo.sin_family = AF_INET;
sockTo.sin_port = htons(7920); sockTo.sin_port = htons(7911);
HostRequest hReq; HostRequest hReq;
hReq.identifier = NETWORK_CLIENT_ID; hReq.identifier = NETWORK_CLIENT_ID;
for(int i = 0; i < 8; ++i) { for(int i = 0; i < 8; ++i) {
if(host->h_addr_list[i] == 0) if(host->h_addr_list[i] == 0)
break; break;
unsigned int local_addr = *(unsigned int*)host->h_addr_list[i]; unsigned int local_addr = *(unsigned int*)host->h_addr_list[i];
local.sin_addr.s_addr = local_addr; sockTo.sin_addr.s_addr = local_addr;
SOCKET sSend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sendto(reply, (const char*)&hReq, sizeof(HostRequest), 0, (sockaddr*)&sockTo, sizeof(sockaddr));
if(sSend == INVALID_SOCKET)
break;
BOOL opt = TRUE;
setsockopt(sSend, SOL_SOCKET, SO_BROADCAST, (const char*)&opt, sizeof(BOOL));
if(bind(sSend, (sockaddr*)&local, sizeof(sockaddr)) == SOCKET_ERROR) {
closesocket(sSend);
break;
}
sendto(sSend, (const char*)&hReq, sizeof(HostRequest), 0, (sockaddr*)&sockTo, sizeof(sockaddr));
closesocket(sSend);
} }
} }
int DuelClient::RefreshThread(void * arg) { int DuelClient::RefreshThread(void * arg) {
......
...@@ -40,13 +40,11 @@ bool NetServer::StartBroadcast() { ...@@ -40,13 +40,11 @@ bool NetServer::StartBroadcast() {
if(!net_evbase) if(!net_evbase)
return false; return false;
SOCKET udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); SOCKET udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
BOOL opt = TRUE;
setsockopt(udp, SOL_SOCKET, SO_BROADCAST, (const char*)&opt, sizeof(BOOL));
sockaddr_in addr; sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(7920); addr.sin_port = htons(7911);
addr.sin_addr.s_addr = 0; addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(udp, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { if(bind(udp, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
closesocket(udp); closesocket(udp);
return false; return false;
...@@ -85,17 +83,13 @@ void NetServer::BroadcastEvent(evutil_socket_t fd, short events, void* arg) { ...@@ -85,17 +83,13 @@ void NetServer::BroadcastEvent(evutil_socket_t fd, short events, void* arg) {
return; return;
HostRequest* pHR = (HostRequest*)buf; HostRequest* pHR = (HostRequest*)buf;
if(pHR->identifier == NETWORK_CLIENT_ID) { if(pHR->identifier == NETWORK_CLIENT_ID) {
SOCKADDR_IN sockTo;
sockTo.sin_addr.s_addr = bc_addr.sin_addr.s_addr;
sockTo.sin_family = AF_INET;
sockTo.sin_port = htons(7921);
HostPacket hp; HostPacket hp;
hp.identifier = NETWORK_SERVER_ID; hp.identifier = NETWORK_SERVER_ID;
hp.port = server_port; hp.port = server_port;
hp.version = PRO_VERSION; hp.version = PRO_VERSION;
hp.host = duel_mode->host_info; hp.host = duel_mode->host_info;
BufferIO::CopyWStr(duel_mode->name, hp.name, 20); BufferIO::CopyWStr(duel_mode->name, hp.name, 20);
sendto(fd, (const char*)&hp, sizeof(HostPacket), 0, (sockaddr*)&sockTo, sizeof(sockTo)); sendto(fd, (const char*)&hp, sizeof(HostPacket), 0, (sockaddr*)&bc_addr, sz);
} }
} }
void NetServer::ServerAccept(evconnlistener* listener, evutil_socket_t fd, sockaddr* address, int socklen, void* ctx) { void NetServer::ServerAccept(evconnlistener* listener, evutil_socket_t fd, sockaddr* address, int socklen, void* ctx) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment