| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include "Assert.h"
- #include "Types.h"
- #include "OS.h"
- #include "UDPSocket.h"
- #include "NetAddress.h"
- namespace crown
- {
- namespace os
- {
- //-----------------------------------------------------------------------------
- UDPSocket::UDPSocket() :
- m_socket(0)
- {
- }
- //-----------------------------------------------------------------------------
- UDPSocket::~UDPSocket()
- {
- close();
- }
- //-----------------------------------------------------------------------------
- bool UDPSocket::open(uint16_t port)
- {
- CE_ASSERT(!is_open(), "Socket is already open");
- m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (m_socket <= 0)
- {
- os::printf("Failed to create socket.\n");
- m_socket = 0;
- return false;
- }
- // Bind to port
- sockaddr_in address;
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(port);
- if (bind(m_socket, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
- {
- os::printf("Failed to bind socket\n");
- close();
- return false;
- }
- int non_blocking = 1;
- if (fcntl(m_socket, F_SETFL, O_NONBLOCK, non_blocking) == -1)
- {
- os::printf("Failed to set non-blocking socket\n");
- close();
- return false;
- }
-
- return true;
- }
- //-----------------------------------------------------------------------------
- bool UDPSocket::send(const NetAddress &receiver, const void* data, size_t size)
- {
- CE_ASSERT(data != NULL, "Data must be != NULL");
- sockaddr_in address;
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = htonl(receiver.address());
- address.sin_port = htons(receiver.port());
- ssize_t sent_bytes = sendto(m_socket, (const char*) data, size, 0, (sockaddr*) &address, sizeof(sockaddr_in));
- if (sent_bytes < 0)
- {
- return false;
- }
- return (size_t) sent_bytes == size;
- }
- //-----------------------------------------------------------------------------
- size_t UDPSocket::receive(NetAddress& sender, void* data, size_t size)
- {
- CE_ASSERT(data != NULL, "Data must be != NULL");
- sockaddr_in from;
- socklen_t from_length = sizeof(from);
- ssize_t received_bytes = recvfrom(m_socket, (char*)data, size, 0, (sockaddr*)&from, &from_length);
- if (received_bytes <= 0)
- {
- return 0;
- }
- uint32_t address = ntohl(from.sin_addr.s_addr);
- uint16_t port = ntohs(from.sin_port);
- sender.set(address, port);
- return (size_t) received_bytes;
- }
- //-----------------------------------------------------------------------------
- void UDPSocket::close()
- {
- if (m_socket != 0)
- {
- ::close(m_socket);
- m_socket = 0;
- }
- }
- //-----------------------------------------------------------------------------
- bool UDPSocket::is_open()
- {
- return m_socket != 0;
- }
- } // namespace os
- } // namespace crown
|