Artifact 9c27c4838b1ff28dfcff824f05a6709f4cb015ed0cf61299ff34e4ade81c5d4c:
- File
src/OFSocket.m
— part of check-in
[70874cf3c5]
at
2008-12-07 03:06:33
on branch trunk
— Remove writeWideCString as that's a bad idea.
Endianess, size etc. may differ from system to system. While this might
be ok when writing files that are only read local again, this is
definitely not ok with sockets. So it's better to remove it from the
OFStream protocol. (user: js, size: 2951) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import "config.h" #import <stdio.h> #import <stdlib.h> #import <string.h> #import <unistd.h> #import "OFSocket.h" #import "OFExceptions.h" @implementation OFSocketAddress + newWithHost: (const char*)host andPort: (uint16_t)port andFamily: (int)family andType: (int)type andProtocol: (int)protocol { return [[OFSocketAddress alloc] initWithHost: host andPort: port andFamily: family andType: type andProtocol: protocol]; } - initWithHost: (const char*)host andPort: (uint16_t)port andFamily: (int)family andType: (int)type andProtocol: (int)protocol { if ((self = [super init])) { if (port == 0) { /* FIXME: Throw exception */ [self free]; return nil; } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; hints.ai_socktype = type; hints.ai_protocol = protocol; hoststr = strdup(host); snprintf(portstr, 6, "%d", port); res = NULL; } return self; } - (struct addrinfo*)getAddressInfo { if (res != NULL) return res; if (getaddrinfo(hoststr, portstr, &hints, &res)) { /* FIXME: Throw exception */ return NULL; } return res; } - free { free(hoststr); if (res != NULL) freeaddrinfo(res); return [super free]; } @end @implementation OFSocket - free { if (sock >= 0) close(sock); return [super free]; } - connect: (OFSocketAddress*)addr { struct addrinfo *ai, *iter; ai = [addr getAddressInfo]; for (iter = ai; iter != NULL; iter = iter->ai_next) { if ((sock = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol)) < 0) continue; if (connect(sock, iter->ai_addr, iter->ai_addrlen) < 0) { close(sock); sock = -1; continue; } break; } if (sock < 0) { /* FIXME: Throw exception */ return nil; } return self; } - (size_t)readNBytes: (size_t)size intoBuffer: (uint8_t*)buf { ssize_t ret; if ((ret = recv(sock, buf, size, 0)) < 0) { /* FIXME: Throw exception */ return 0; } /* This is safe, as we already checked < 0 */ return ret; } - (uint8_t*)readNBytes: (size_t)size { uint8_t *ret; ret = [self getMemWithSize: size]; @try { [self readNBytes: size intoBuffer: ret]; } @catch (id exception) { [self freeMem: ret]; @throw exception; } return ret; } - (size_t)writeNBytes: (size_t)size fromBuffer: (const uint8_t*)buf { ssize_t ret; if ((ret = send(sock, buf, size, 0)) < 0) { /* FIXME: Throw exception */ return 0; } /* This is safe, as we already checked < 0 */ return ret; } - (size_t)writeCString: (const char*)str { return [self writeNBytes: strlen(str) fromBuffer: (const uint8_t*)str]; } @end