wip
Dependents: EthernetInterface_vz
Fork of Socket by
Diff: UDPSocket.cpp
- Revision:
- 5:300e7ad2dc1d
- Parent:
- 3:e6474399e057
- Child:
- 6:cd2e5559786d
--- a/UDPSocket.cpp Thu Jul 26 10:07:43 2012 +0000 +++ b/UDPSocket.cpp Thu Jul 26 15:07:32 2012 +0000 @@ -16,7 +16,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "UDPSocket.h" +#include "Socket/UDPSocket.h" #include <cstring> @@ -25,9 +25,13 @@ UDPSocket::UDPSocket() { } +int UDPSocket::init(void) { + return init_socket(SOCK_DGRAM); +} + // Server initialization int UDPSocket::bind(int port) { - if (init(SOCK_DGRAM) < 0) + if (init_socket(SOCK_DGRAM) < 0) return -1; struct sockaddr_in localHost; @@ -46,72 +50,33 @@ } // -1 if unsuccessful, else number of bytes written -int UDPSocket::sendTo(char* data, int length, char* host, int port, int timeout) { +int UDPSocket::sendTo(UDPPacket& packet, int timeout) { if (_sock_fd < 0) return -1; - struct sockaddr_in remoteHost; - - //Populate m_remoteHost - std::memset(&remoteHost, 0, sizeof(struct sockaddr_in)); - - //Resolve DNS address or populate hard-coded IP address - struct hostent *server = ::gethostbyname(host); - if (server == NULL) { - return -1; //Could not resolve address - } - std::memcpy((char*) &remoteHost.sin_addr.s_addr, - (char*) server->h_addr_list[0], server->h_length); - - remoteHost.sin_family = AF_INET; - remoteHost.sin_port = htons(port); - - size_t writtenLen = 0; - set_timeout(timeout); - while (writtenLen < length) { - //Wait for socket to be writeable - //Creating FS set + if (timeout != 0) { + set_timeout(timeout); if (wait_writable() != 0) - return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ? - - int ret = lwip_sendto(_sock_fd, data + writtenLen, length - writtenLen, 0, - (const struct sockaddr *) &remoteHost, sizeof(remoteHost)); - if (ret > 0) { - writtenLen += ret; - } else if (ret == 0) { - return writtenLen; //Connection was closed by server -- FIXME how do we signal that the connection was closed ? - } else { - return -1; //Connnection error - } + return -1; } - return writtenLen; + return lwip_sendto(_sock_fd, packet._buffer, packet._length, 0, (const struct sockaddr *) &packet._remoteHost, sizeof(packet._remoteHost)); } // -1 if unsuccessful, else number of bytes received -int UDPSocket::receiveFrom(char* data, int length, char** host, int* port, int timeout) { +int UDPSocket::receiveFrom(UDPPacket& packet, int timeout) { if (_sock_fd < 0) return -1; - set_timeout(timeout); - if (wait_readable() != 0) - return -1; - - struct sockaddr_in remoteHost; - std::memset(&remoteHost, 0, sizeof(struct sockaddr_in)); + if (timeout != 0) { + set_timeout(timeout); + if (wait_readable() != 0) + return -1; + } - socklen_t remoteHostLen = sizeof(remoteHost); - int n = lwip_recvfrom(_sock_fd, data, length, 0, (struct sockaddr*) &remoteHost, &remoteHostLen); - if (n < 0) - return -1; - - static char hostBuf[16]; - inet_ntoa_r(remoteHost.sin_addr, hostBuf, sizeof(hostBuf)); - - *host = hostBuf; - *port = ntohs(remoteHost.sin_port); - - return n; + packet.reset_address(); + socklen_t remoteHostLen = sizeof(packet._remoteHost); + return lwip_recvfrom(_sock_fd, packet._buffer, packet._length, 0, (struct sockaddr*) &packet._remoteHost, &remoteHostLen); } UDPSocket::~UDPSocket() {