src/network/core/udp.c
changeset 5772 fbb42af982b4
parent 5770 15abf566c92e
child 5778 c1454e0c03bf
equal deleted inserted replaced
5771:7288d4e4bfba 5772:fbb42af982b4
    15 #include "udp.h"
    15 #include "udp.h"
    16 
    16 
    17 /**
    17 /**
    18  * @file udp.c Basic functions to receive and send UDP packets.
    18  * @file udp.c Basic functions to receive and send UDP packets.
    19  */
    19  */
    20 
       
    21 /**
       
    22  * Send a packet over UDP
       
    23  * @param udp  the socket to send over
       
    24  * @param p    the packet to send
       
    25  * @param recv the receiver (target) of the packet
       
    26  */
       
    27 void NetworkSendUDP_Packet(SOCKET udp, Packet *p, struct sockaddr_in *recv)
       
    28 {
       
    29 	int res;
       
    30 
       
    31 	NetworkSend_FillPacketSize(p);
       
    32 
       
    33 	/* Send the buffer */
       
    34 	res = sendto(udp, p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
       
    35 
       
    36 	/* Check for any errors, but ignore it otherwise */
       
    37 	if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
       
    38 }
       
    39 
    20 
    40 /**
    21 /**
    41  * Start listening on the given host and port.
    22  * Start listening on the given host and port.
    42  * @param udp       the place where the (references to the) UDP are stored
    23  * @param udp       the place where the (references to the) UDP are stored
    43  * @param host      the host (ip) to listen on
    24  * @param host      the host (ip) to listen on
    87 	}
    68 	}
    88 
    69 
    89 	DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port);
    70 	DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port);
    90 
    71 
    91 	return true;
    72 	return true;
       
    73 }
       
    74 
       
    75 /**
       
    76  * Close the given UDP socket
       
    77  * @param udp the socket to close
       
    78  */
       
    79 void NetworkUDPClose(SOCKET *udp)
       
    80 {
       
    81 	if (*udp == INVALID_SOCKET) return;
       
    82 
       
    83 	closesocket(*udp);
       
    84 	*udp = INVALID_SOCKET;
       
    85 }
       
    86 
       
    87 
       
    88 /**
       
    89  * Send a packet over UDP
       
    90  * @param udp  the socket to send over
       
    91  * @param p    the packet to send
       
    92  * @param recv the receiver (target) of the packet
       
    93  */
       
    94 void NetworkSendUDP_Packet(SOCKET udp, Packet *p, struct sockaddr_in *recv)
       
    95 {
       
    96 	int res;
       
    97 
       
    98 	NetworkSend_FillPacketSize(p);
       
    99 
       
   100 	/* Send the buffer */
       
   101 	res = sendto(udp, p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
       
   102 
       
   103 	/* Check for any errors, but ignore it otherwise */
       
   104 	if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
    92 }
   105 }
    93 
   106 
    94 /**
   107 /**
    95  * Receive a packet at UDP level
   108  * Receive a packet at UDP level
    96  * @param udp the socket to receive the packet on
   109  * @param udp the socket to receive the packet on