Prevent copies

This commit is contained in:
def 2018-12-17 22:15:41 +01:00
parent bfa25bac4a
commit 4b92e72763
4 changed files with 19 additions and 19 deletions

View file

@ -1346,7 +1346,6 @@ NETSOCKET net_udp_create(NETADDR bindaddr)
NETSOCKET sock = invalid_socket;
NETADDR tmpbindaddr = bindaddr;
int broadcast = 1;
int recvsize = 65536;
if(bindaddr.type&NETTYPE_IPV4)
{
@ -1365,9 +1364,6 @@ NETSOCKET net_udp_create(NETADDR bindaddr)
/* set broadcast */
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast));
/* set receive buffer size */
setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char*)&recvsize, sizeof(recvsize));
{
/* set DSCP/TOS */
int iptos = 0x10 /* IPTOS_LOWDELAY */;
@ -1413,9 +1409,6 @@ NETSOCKET net_udp_create(NETADDR bindaddr)
/* set broadcast */
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast));
/* set receive buffer size */
setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char*)&recvsize, sizeof(recvsize));
{
/* set DSCP/TOS */
int iptos = 0x10 /* IPTOS_LOWDELAY */;
@ -1543,7 +1536,7 @@ void net_init_mmsgs(MMSGS* m)
#endif
}
int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize, MMSGS* m)
int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *buffer, int maxsize, MMSGS* m, unsigned char **data)
{
#ifndef FUZZING
char sockaddrbuf[128];
@ -1575,7 +1568,7 @@ int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize, MMSGS*
//network_stats.recv_bytes += bytes;
//network_stats.recv_packets++;
bytes = m->msgs[m->pos].msg_len;
mem_copy(data, m->bufs[m->pos], bytes);
*data = (unsigned char*)m->bufs[m->pos];
m->pos++;
return bytes;
}
@ -1583,13 +1576,15 @@ int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize, MMSGS*
if(bytes == 0 && sock.ipv4sock >= 0)
{
socklen_t fromlen = sizeof(struct sockaddr_in);
bytes = recvfrom(sock.ipv4sock, (char*)data, maxsize, 0, (struct sockaddr *)&sockaddrbuf, &fromlen);
bytes = recvfrom(sock.ipv4sock, (char*)buffer, maxsize, 0, (struct sockaddr *)&sockaddrbuf, &fromlen);
*data = buffer;
}
if(bytes <= 0 && sock.ipv6sock >= 0)
{
socklen_t fromlen = sizeof(struct sockaddr_in6);
bytes = recvfrom(sock.ipv6sock, (char*)data, maxsize, 0, (struct sockaddr *)&sockaddrbuf, &fromlen);
bytes = recvfrom(sock.ipv6sock, (char*)buffer, maxsize, 0, (struct sockaddr *)&sockaddrbuf, &fromlen);
*data = buffer;
}
#endif
@ -1597,7 +1592,8 @@ int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize, MMSGS*
if(bytes <= 0 && sock.web_ipv4sock >= 0)
{
socklen_t fromlen = sizeof(struct sockaddr);
bytes = websocket_recv(sock.web_ipv4sock, data, maxsize, (struct sockaddr_in *)&sockaddrbuf, fromlen);
bytes = websocket_recv(sock.web_ipv4sock, buffer, maxsize, (struct sockaddr_in *)&sockaddrbuf, fromlen);
*data = buffer;
((struct sockaddr_in *)&sockaddrbuf)->sin_family = AF_WEBSOCKET_INET;
}
#endif
@ -1629,7 +1625,8 @@ int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize, MMSGS*
break;
}
((unsigned char*)data)[CurrentData] = gs_NetData[gs_NetPosition];
((unsigned char*)buffer)[CurrentData] = gs_NetData[gs_NetPosition];
*data = buffer;
CurrentData++;
gs_NetPosition++;
}

View file

@ -782,14 +782,15 @@ void net_init_mmsgs(MMSGS* m);
Parameters:
sock - Socket to use.
addr - Pointer to an NETADDR that will receive the address.
data - Pointer to a buffer that will receive the data.
buffer - Pointer to a buffer that can be used to receive the data.
maxsize - Maximum size to receive.
data - Will get set to the actual data, might be the passed buffer or an internal one
Returns:
On success it returns the number of bytes received. Returns -1
on error.
*/
int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize, MMSGS* m);
int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *buffer, int maxsize, MMSGS* m, unsigned char **data);
/*
Function: net_udp_close

View file

@ -66,13 +66,14 @@ int CNetClient::Recv(CNetChunk *pChunk)
// TODO: empty the recvinfo
NETADDR Addr;
int Bytes = net_udp_recv(m_Socket, &Addr, m_RecvUnpacker.m_aBuffer, NET_MAX_PACKETSIZE, &m_MMSGS);
unsigned char *pData;
int Bytes = net_udp_recv(m_Socket, &Addr, m_RecvUnpacker.m_aBuffer, NET_MAX_PACKETSIZE, &m_MMSGS, &pData);
// no more packets for now
if(Bytes <= 0)
break;
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data, /* Decompress =*/ true) == 0)
if(CNetBase::UnpackPacket(pData, Bytes, &m_RecvUnpacker.m_Data, /* Decompress =*/ true) == 0)
{
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)
{

View file

@ -600,7 +600,8 @@ int CNetServer::Recv(CNetChunk *pChunk)
return 1;
// TODO: empty the recvinfo
int Bytes = net_udp_recv(m_Socket, &Addr, m_RecvUnpacker.m_aBuffer, NET_MAX_PACKETSIZE, &m_MMSGS);
unsigned char *pData;
int Bytes = net_udp_recv(m_Socket, &Addr, m_RecvUnpacker.m_aBuffer, NET_MAX_PACKETSIZE, &m_MMSGS, &pData);
// no more packets for now
if(Bytes <= 0)
@ -619,7 +620,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
int Slot = GetClientSlot(Addr);
bool Decompress = Slot != -1;
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data, Decompress) == 0)
if(CNetBase::UnpackPacket(pData, Bytes, &m_RecvUnpacker.m_Data, Decompress) == 0)
{
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)
{