本文整理汇总了C++中NetadrToSockadr函数的典型用法代码示例。如果您正苦于以下问题:C++ NetadrToSockadr函数的具体用法?C++ NetadrToSockadr怎么用?C++ NetadrToSockadr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NetadrToSockadr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: NET_SendPacket
void
NET_SendPacket (int length, const void *data, netadr_t to)
{
int ret;
AF_address_t addr;
NetadrToSockadr (&to, &addr);
ret = sendto (net_socket, data, length, 0, &addr.sa, SA_LEN (&addr.sa));
if (ret == -1) {
#ifdef _WIN32
int err = WSAGetLastError ();
if (err == WSAEADDRNOTAVAIL)
Sys_Printf ("NET_SendPacket Warning: %i\n", err);
#else /* _WIN32 */
int err = errno;
if (err == ECONNREFUSED)
return;
#endif /* _WIN32 */
if (err == EWOULDBLOCK)
return;
Sys_Printf ("NET_SendPacket: %s\n", strerror (err));
}
}
示例2: NET_SendPacket
//=============================================================================
void NET_SendPacket (int length, void *data, netadr_t to)
{
int ret;
int outlen;
struct sockaddr_in addr;
char string[120];
NetadrToSockadr (&to, &addr);
HuffEncode((unsigned char *)data,huffbuff,length,&outlen);
#ifdef _DEBUG
sprintf(string,"in: %d out: %d ratio: %f\n",HuffIn, HuffOut, 1-(float)HuffOut/(float)HuffIn);
OutputDebugString(string);
CalcFreq((unsigned char *)data, length);
#endif
ret = sendto (net_socket, (char *) huffbuff, outlen, 0, (struct sockaddr *)&addr, sizeof(addr) );
if (ret == -1)
{
int errno = WSAGetLastError();
Con_Printf ("NET_SendPacket ERROR: %i", errno);
}
}
示例3: NET_SendPacket
void NET_SendPacket(int length, byte *data, netadr_t to)
{
int ret;
struct sockaddr_in addr;
NetadrToSockadr(&to, &addr);
ret = sendto(net_socket, (const char*)data, length, 0, (struct sockaddr *)&addr, sizeof(addr) );
if (ret == -1)
{
#ifdef _WIN32
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK)
return;
#else
if (errno == EWOULDBLOCK)
return;
if (errno == ECONNREFUSED)
return;
printf("NET_SendPacket: %s\n", strerror(errno));
#endif
}
}
示例4: Com_sprintf
const char *NET_AdrToString(netadr_t a)
{
static char s[NET_ADDRSTRMAXLEN];
if (a.type == NA_LOOPBACK)
{
Com_sprintf(s, sizeof(s), "loopback");
}
else if (a.type == NA_BOT)
{
Com_sprintf(s, sizeof(s), "bot");
}
else if (a.type == NA_IP)
{
// Port has to be returned along with ip address because of compatibility
Com_sprintf(s, sizeof(s), "%i.%i.%i.%i:%hu",
a.ip[0], a.ip[1], a.ip[2], a.ip[3], BigShort(a.port));
}
#ifdef FEATURE_IPV6
else if (a.type == NA_IP6)
{
// FIXME: add port for compatibility
// (joining a server through the server browser)
struct sockaddr_storage sadr;
memset(&sadr, 0, sizeof(sadr));
NetadrToSockadr(&a, (struct sockaddr *) &sadr);
Sys_SockaddrToString(s, sizeof(s), (struct sockaddr *) &sadr);
}
#endif
return s;
}
示例5: NET_SendPacket
void NET_SendPacket (netsrc_t sock, int length, void *data, netadr_t to) {
int ret;
struct sockaddr_in addr;
int net_socket;
if (to.type == NA_LOOPBACK) {
NET_SendLoopPacket (sock, length, data, to);
return;
}
net_socket = ip_sockets[sock];
if (net_socket == -1)
return;
NetadrToSockadr (&to, &addr);
ret = sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, sizeof(addr) );
if (ret == -1) {
if (errno == EWOULDBLOCK)
return;
if (errno == ECONNREFUSED)
return;
Sys_Printf ("NET_SendPacket: %s\n", strerror(errno));
}
}
示例6: NET_IsClientLegal
// Returns true if we can't bind the address locally--in other words,
// the IP is NOT one of our interfaces.
qboolean NET_IsClientLegal(netadr_t *adr)
{
struct sockaddr_in sadr;
int newsocket;
#if 0
if (adr->ip[0] == 127)
return false; // no local connections period
NetadrToSockadr (adr, &sadr);
if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
Sys_Error ("NET_IsClientLegal: socket:", strerror(errno));
sadr.sin_port = 0;
if( bind (newsocket, (void *)&sadr, sizeof(sadr)) == -1)
{
// It is not a local address
close(newsocket);
return true;
}
close(newsocket);
return false;
#else
return true;
#endif
}
示例7: Sys_SendPacket
/*
==================
Sys_SendPacket
==================
*/
void Sys_SendPacket( int length, const void* data, netadr_t to )
{
int ret = SOCKET_ERROR;
struct sockaddr_storage addr;
if ( to.type != NA_BROADCAST && to.type != NA_IP && to.type != NA_IP6 && to.type != NA_MULTICAST6 )
{
Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" );
return;
}
if ( ( ip_socket == INVALID_SOCKET && to.type == NA_IP ) ||
( ip_socket == INVALID_SOCKET && to.type == NA_BROADCAST ) ||
( ip6_socket == INVALID_SOCKET && to.type == NA_IP6 ) ||
( ip6_socket == INVALID_SOCKET && to.type == NA_MULTICAST6 ) )
return;
if ( to.type == NA_MULTICAST6 && ( net_enabled->integer & NET_DISABLEMCAST ) )
return;
memset( &addr, 0, sizeof( addr ) );
NetadrToSockadr( &to, ( struct sockaddr* ) &addr );
if ( usingSocks && to.type == NA_IP )
{
socksBuf[0] = 0; // reserved
socksBuf[1] = 0;
socksBuf[2] = 0; // fragment (not fragmented)
socksBuf[3] = 1; // address type: IPV4
*( int* )&socksBuf[4] = ( ( struct sockaddr_in* )&addr )->sin_addr.s_addr;
*( short* )&socksBuf[8] = ( ( struct sockaddr_in* )&addr )->sin_port;
memcpy( &socksBuf[10], data, length );
ret = sendto( ip_socket, socksBuf, length + 10, 0, &socksRelayAddr, sizeof( socksRelayAddr ) );
}
else
{
if ( addr.ss_family == AF_INET )
ret = sendto( ip_socket, ( const char* )data, length, 0, ( struct sockaddr* ) &addr, sizeof( struct sockaddr_in ) );
else if ( addr.ss_family == AF_INET6 )
ret = sendto( ip6_socket, ( const char* )data, length, 0, ( struct sockaddr* ) &addr, sizeof( struct sockaddr_in6 ) );
}
if ( ret == SOCKET_ERROR )
{
int err = socketError;
// wouldblock is silent
if ( err == EAGAIN )
{
return;
}
// some PPP links do not allow broadcasts and return an error
if ( ( err == EADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) ) )
{
return;
}
Com_Printf( "Sys_SendPacket: %s\n", NET_ErrorString() );
}
}
示例8: SB_PingTree_SendQueryThread
DWORD WINAPI SB_PingTree_SendQueryThread(void *thread_arg)
{
proxy_request_queue *queue = (proxy_request_queue *) thread_arg;
int i, ret;
double interval_ms = (1.0 / sb_proxinfopersec.value) * 1000.0;
timerresolution_session_t timersession = {0, 0};
Sys_TimerResolution_InitSession(&timersession);
Sys_TimerResolution_RequestMinimum(&timersession);
for (i = 0; i < queue->items; i++) {
if (!queue->data[i].done) {
struct sockaddr_storage addr_to;
netadr_t netadr = SB_NodeNetadr_Get(queue->data[i].nodeid);
NetadrToSockadr(&netadr, &addr_to);
ret = sendto(queue->data[i].sock,
PROXY_PINGLIST_QUERY, PROXY_PINGLIST_QUERY_LEN, 0,
(struct sockaddr *) &addr_to, sizeof (struct sockaddr));
if (ret < 0) {
Com_DPrintf("SB_PingTree_SendQueryThread sendto returned %d\n", ret);
}
Sys_MSleep(interval_ms);
}
if (queue->allrecved) break;
}
Sys_TimerResolution_Clear(&timersession);
queue->sending_done = true;
return 0;
}
示例9: NET_SendPacket
void NET_SendPacket (buf_t &buf, netadr_t &to)
{
int ret;
struct sockaddr_in addr;
NetadrToSockadr (&to, &addr);
ret = sendto (net_socket, (const char *)buf.ptr(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr));
buf.clear();
if (ret == -1)
{
#ifdef _WIN32
int err = WSAGetLastError();
// wouldblock is silent
if (err == WSAEWOULDBLOCK)
return;
#else
if (errno == EWOULDBLOCK)
return;
if (errno == ECONNREFUSED)
return;
Printf (PRINT_HIGH, "NET_SendPacket: %s\n", strerror(errno));
#endif
}
}
示例10: AddressToString
std::string AddressToString( const netadr_t& address, bool with_port )
{
if ( address.type == netadrtype_t::NA_LOOPBACK )
{
return "loopback";
}
else if ( address.type == netadrtype_t::NA_BOT )
{
return "bot";
}
else if ( address.type == netadrtype_t::NA_IP ||
address.type == netadrtype_t::NA_IP6 ||
address.type == netadrtype_t::NA_IP_DUAL )
{
char s[ NET_ADDR_STR_MAX_LEN ];
sockaddr_storage sadr = {0};
NetadrToSockadr( &address, reinterpret_cast<sockaddr*>(&sadr) );
Sys_SockaddrToString( s, sizeof(s), reinterpret_cast<sockaddr*>(&sadr) );
std::string result = s;
if ( with_port )
{
if ( NET_IS_IPv6( address.type ) )
{
result = '[' + result + ']';
}
result += ':' + std::to_string(ntohs(GetPort(address)));
}
return result;
}
return "";
}
示例11: Sys_SendPacket
/*
==================
Sys_SendPacket
==================
*/
void Sys_SendPacket( int length, const void *data, netadr_t to ) {
int ret;
struct sockaddr addr;
SOCKET net_socket;
if( to.type == NA_BROADCAST ) {
net_socket = ip_socket;
}
else if( to.type == NA_IP ) {
net_socket = ip_socket;
}
else if( to.type == NA_IPX ) {
net_socket = ipx_socket;
}
else if( to.type == NA_BROADCAST_IPX ) {
net_socket = ipx_socket;
}
else {
Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" );
return;
}
if( !net_socket ) {
return;
}
NetadrToSockadr( &to, &addr );
if( usingSocks && to.type == NA_IP ) {
socksBuf[0] = 0; // reserved
socksBuf[1] = 0;
socksBuf[2] = 0; // fragment (not fragmented)
socksBuf[3] = 1; // address type: IPV4
*(int *)&socksBuf[4] = ((struct sockaddr_in *)&addr)->sin_addr.s_addr;
*(short *)&socksBuf[8] = ((struct sockaddr_in *)&addr)->sin_port;
memcpy( &socksBuf[10], data, length );
ret = sendto( net_socket, socksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) );
}
else {
ret = sendto( net_socket, (const char *)data, length, 0, &addr, sizeof(addr) );
}
if( ret == SOCKET_ERROR ) {
int err = WSAGetLastError();
// wouldblock is silent
if( err == WSAEWOULDBLOCK ) {
return;
}
// some PPP links do not allow broadcasts and return an error
if( ( err == WSAEADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) || ( to.type == NA_BROADCAST_IPX ) ) ) {
return;
}
Com_Printf( "NET_SendPacket: %s\n", NET_ErrorString() );
}
}
示例12: NetadrToSockadr
/*
==================
idTCP::Init
==================
*/
bool idTCP::Init( const char *host, short port )
{
struct sockaddr_in sadr;
if ( !Sys_StringToNetAdr( host, &address, true ) )
{
common->Printf( "Couldn't resolve server name \"%s\"\n", host );
return false;
}
address.type = NA_IP;
if (!address.port)
{
address.port = port;
}
common->Printf( "\"%s\" resolved to %i.%i.%i.%i:%i\n", host,
address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port );
NetadrToSockadr(&address, &sadr);
if (fd)
{
common->Warning("idTCP::Init: already initialized?\n");
}
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
fd = 0;
common->Printf("ERROR: idTCP::Init: socket: %s\n", strerror(errno));
return false;
}
if ( connect( fd, (const sockaddr *)&sadr, sizeof( sadr ) ) == -1 )
{
common->Printf( "ERROR: idTCP::Init: connect: %s\n", strerror( errno ) );
close( fd );
fd = 0;
return false;
}
int status;
if ((status = fcntl(fd, F_GETFL, 0)) != -1)
{
status |= O_NONBLOCK; /* POSIX */
status = fcntl(fd, F_SETFL, status);
}
if (status == -1)
{
common->Printf("ERROR: idTCP::Init: fcntl / O_NONBLOCK: %s\n", strerror(errno));
close(fd);
fd = 0;
return false;
}
common->DPrintf("Opened TCP connection\n");
return true;
}
示例13: NET_SendPacket
void NET_SendPacket (netsrc_t sock, int length, void *data, netadr_t to)
{
int ret;
struct sockaddr_in addr;
int net_socket;
if ( to.type == NA_LOOPBACK )
{
NET_SendLoopPacket (sock, length, data, to);
return;
}
if (to.type == NA_BROADCAST)
{
net_socket = ip_sockets[sock];
if (!net_socket)
return;
}
else if (to.type == NA_IP)
{
net_socket = ip_sockets[sock];
if (!net_socket)
return;
}
else if (to.type == NA_IPX)
{
net_socket = ipx_sockets[sock];
if (!net_socket)
return;
}
else if (to.type == NA_BROADCAST_IPX)
{
net_socket = ipx_sockets[sock];
if (!net_socket)
return;
}
else
Com_Error (ERR_FATAL, "NET_SendPacket: bad address type");
NetadrToSockadr (&to, &addr);
// >>> FIX: For Nintendo Wii using devkitPPC / libogc
// Switching to the equivalent function in the platform:
//ret = sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, sizeof(addr) );
//if (ret == -1)
ret = net_sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, 8);
if (ret < 0)
// <<< FIX
{
Com_Printf ("NET_SendPacket ERROR: %s to %s\n", NET_ErrorString(),
NET_AdrToString (to));
}
}
示例14: UDP_Write
int
UDP_Write(int socket, const void *buf, int len, const netadr_t *addr)
{
struct sockaddr_in saddr;
int ret;
NetadrToSockadr(addr, &saddr);
ret = sendto(socket, buf, len, 0, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret == -1 && errno == EWOULDBLOCK)
return 0;
return ret;
}
示例15: Sys_SendVoicePacket
/*
==================
Sys_SendVoicePacket
==================
*/
void Sys_SendVoicePacket( int length, const void *data, netadr_t to ) {
int ret;
struct sockaddr addr;
// check for valid packet intentions (direct send or broadcast)
//
if( to.type != NA_IP && to.type != NA_BROADCAST )
{
Com_Error( ERR_FATAL, "Sys_SendVoicePacket: bad address type" );
return;
}
// check we have our voice socket set up
//
if( v_socket == INVALID_SOCKET) {
return;
}
NetadrToSockadr( &to, &addr );
#ifdef SOF2_METRICS
gXBL_NumberVoicePacketsSent++;
gXBL_SizeVoicePacketsSent += length;
#endif
/*if( usingSocks && to.type == NA_IP ) {
vsocksBuf[0] = 0; // reserved
vsocksBuf[1] = 0;
vsocksBuf[2] = 0; // fragment (not fragmented)
vsocksBuf[3] = 1; // address type: IPV4
*(int *)&vsocksBuf[4] = ((struct sockaddr_in *)&addr)->sin_addr.s_addr;
*(short *)&vsocksBuf[8] = ((struct sockaddr_in *)&addr)->sin_port;
memcpy( &vsocksBuf[10], data, length );
ret = sendto( v_socket, vsocksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) );
}
else {*/
ret = sendto( v_socket, (const char *)data, length, 0, &addr, sizeof(addr) );
//}
if( ret == SOCKET_ERROR ) {
int err = WSAGetLastError();
// wouldblock is silent
if( err == WSAEWOULDBLOCK ) {
return;
}
// some PPP links do not allow broadcasts and return an error
if( ( err == WSAEADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) || ( to.type == NA_BROADCAST_IPX ) ) ) {
return;
}
Com_DPrintf( "NET_SendVoicePacket: %s\n", NET_ErrorString() );
}
}