ObjFW  Check-in [28ea4a8a67]

Overview
Comment:Rename OFSocket to OFStreamSocket.

This is to reflect that it is a connection-based, sequenced, reliable,
two-way byte stream.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 28ea4a8a67a92e61fdfa874f193bc7938b8d8d9da146edb81ef0bb2ca886930e
User & Date: js on 2010-07-07 20:57:04
Other Links: manifest | tags
Context
2010-07-07
21:06
Move most of the code for -[close] to OFStreamSocket. check-in: d81629fead user: js tags: trunk
20:57
Rename OFSocket to OFStreamSocket. check-in: 28ea4a8a67 user: js tags: trunk
20:48
Rename OFSocketObserver to OFStreamObserver and make it more general. check-in: 52dcb22b8c user: js tags: trunk
Changes

Modified src/Makefile from [1900e0a870] to [a6f4f62219].

20
21
22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
       OFMutableDictionary.m	\
       OFMutableString.m	\
       OFNumber.m		\
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSeekableStream.m	\
       OFSHA1Hash.m		\
       OFSocket.m		\
       OFStream.m		\
       OFStreamObserver.m	\

       OFString.m		\
       OFString+Hashing.m	\
       OFString+URLEncoding.m	\
       OFString+XMLEscaping.m	\
       OFString+XMLUnescaping.m	\
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\







<


>







20
21
22
23
24
25
26

27
28
29
30
31
32
33
34
35
36
       OFMutableDictionary.m	\
       OFMutableString.m	\
       OFNumber.m		\
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSeekableStream.m	\
       OFSHA1Hash.m		\

       OFStream.m		\
       OFStreamObserver.m	\
       OFStreamSocket.m		\
       OFString.m		\
       OFString+Hashing.m	\
       OFString+URLEncoding.m	\
       OFString+XMLEscaping.m	\
       OFString+XMLUnescaping.m	\
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\

Modified src/OFExceptions.m from [a38cc28a3c] to [c2ce5ebc7f].

446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
- initWithClass: (Class)class_
	   size: (size_t)size
{
	self = [super initWithClass: class_];

	requestedSize = size;

	if ([class_ isSubclassOfClass: [OFSocket class]])
		errNo = GET_SOCK_ERRNO;
	else
		errNo = GET_ERRNO;

	return self;
}








|







446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
- initWithClass: (Class)class_
	   size: (size_t)size
{
	self = [super initWithClass: class_];

	requestedSize = size;

	if ([class_ isSubclassOfClass: [OFStreamSocket class]])
		errNo = GET_SOCK_ERRNO;
	else
		errNo = GET_ERRNO;

	return self;
}

Deleted src/OFSocket.h version [1c92be9a99].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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 "OFStream.h"

#ifdef _WIN32
# define _WIN32_WINNT 0x0501
# include <winsock2.h>
#endif

/**
 * \brief A class which provides functions to create and use sockets.
 */
@interface OFSocket: OFStream
{
@public
#ifndef _WIN32
	int    sock;
#else
	SOCKET sock;
#endif
	BOOL   listening;
@protected
	BOOL   eos;
}

/**
 * \return A new autoreleased OFTCPSocket
 */
+ socket;

/**
 * Enables/disables non-blocking I/O.
 */
- (void)setBlocking: (BOOL)enable;
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
























































































Deleted src/OFSocket.m version [26b969d2ec].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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.
 */

#include "config.h"

#include <string.h>
#include <fcntl.h>
#include <errno.h>

#ifndef _WIN32
# include <sys/types.h>
# include <sys/socket.h>
#endif

#import "OFSocket.h"
#import "OFExceptions.h"

#ifndef INVALID_SOCKET
# define INVALID_SOCKET -1
#endif

@implementation OFSocket
#ifdef _WIN32
+ (void)initialize
{
	WSADATA wsa;

	if (self != [OFSocket class])
		return;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		@throw [OFInitializationFailedException newWithClass: self];
}
#endif

+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_atEndOfStream
{
	return eos;
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

#ifndef _WIN32
	/* FIXME: We want a sane error message on Win32 as well */
	if (eos)
		errno = ENOTCONN;
#endif

	if (eos || (ret = recv(sock, buf, size, 0)) < 0)
		@throw [OFReadFailedException newWithClass: isa
						      size: size];

	if (ret == 0)
		eos = YES;

	return ret;
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

#ifndef _WIN32
	/* FIXME: We want a sane error message on Win32 as well */
	if (eos)
		errno = ENOTCONN;
#endif

	if (eos || (ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];

	/* This is safe, as we already checked for -1 */
	return ret;
}

- (void)setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) == -1)
		@throw [OFSetOptionFailedException newWithClass: isa];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) == -1)
		@throw [OFSetOptionFailedException newWithClass: isa];
#else
	u_long v = enable;

	if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException newWithClass: isa];
#endif
}

- (int)fileDescriptor
{
	return sock;
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






























































































































































































































































Added src/OFStreamSocket.h version [5b8ef38675].

























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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 "OFStream.h"

#ifdef _WIN32
# define _WIN32_WINNT 0x0501
# include <winsock2.h>
#endif

/**
 * \brief A class which provides functions to create and use stream sockets.
 */
@interface OFStreamSocket: OFStream
{
@public
#ifndef _WIN32
	int    sock;
#else
	SOCKET sock;
#endif
	BOOL   listening;
@protected
	BOOL   eos;
}

/**
 * \return A new autoreleased OFTCPSocket
 */
+ socket;

/**
 * Enables/disables non-blocking I/O.
 */
- (void)setBlocking: (BOOL)enable;
@end

Added src/OFStreamSocket.m version [1c776793df].































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. 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.
 */

#include "config.h"

#include <string.h>
#include <fcntl.h>
#include <errno.h>

#ifndef _WIN32
# include <sys/types.h>
# include <sys/socket.h>
#endif

#import "OFStreamSocket.h"
#import "OFExceptions.h"

#ifndef INVALID_SOCKET
# define INVALID_SOCKET -1
#endif

@implementation OFStreamSocket
#ifdef _WIN32
+ (void)initialize
{
	WSADATA wsa;

	if (self != [OFStreamSocket class])
		return;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		@throw [OFInitializationFailedException newWithClass: self];
}
#endif

+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_atEndOfStream
{
	return eos;
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

#ifndef _WIN32
	/* FIXME: We want a sane error message on Win32 as well */
	if (eos)
		errno = ENOTCONN;
#endif

	if (eos || (ret = recv(sock, buf, size, 0)) < 0)
		@throw [OFReadFailedException newWithClass: isa
						      size: size];

	if (ret == 0)
		eos = YES;

	return ret;
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

#ifndef _WIN32
	/* FIXME: We want a sane error message on Win32 as well */
	if (eos)
		errno = ENOTCONN;
#endif

	if (eos || (ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];

	/* This is safe, as we already checked for -1 */
	return ret;
}

- (void)setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) == -1)
		@throw [OFSetOptionFailedException newWithClass: isa];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) == -1)
		@throw [OFSetOptionFailedException newWithClass: isa];
#else
	u_long v = enable;

	if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException newWithClass: isa];
#endif
}

- (int)fileDescriptor
{
	return sock;
}
@end

Modified src/OFTCPSocket.h from [19e54da62b] to [b07d89367b].

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#ifndef _WIN32
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
#endif

#import "OFSocket.h"

#ifdef _WIN32
# include <ws2tcpip.h>
#endif

@class OFString;

/**
 * \brief A class which provides functions to create and use TCP sockets.
 */
@interface OFTCPSocket: OFSocket
{
	struct sockaddr	*sockAddr;
	socklen_t	sockAddrLen;
}

/**
 * Connect the OFTCPSocket to the specified destination.







|










|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#ifndef _WIN32
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
#endif

#import "OFStreamSocket.h"

#ifdef _WIN32
# include <ws2tcpip.h>
#endif

@class OFString;

/**
 * \brief A class which provides functions to create and use TCP sockets.
 */
@interface OFTCPSocket: OFStreamSocket
{
	struct sockaddr	*sockAddr;
	socklen_t	sockAddrLen;
}

/**
 * Connect the OFTCPSocket to the specified destination.

Modified src/ObjFW.h from [152202e873] to [816eccd8cc].

24
25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40
41
42
43

#import "OFDictionary.h"
#import "OFEnumerator.h"

#import "OFNumber.h"

#import "OFStream.h"


#import "OFFile.h"

#import "OFSocket.h"
#import "OFTCPSocket.h"
#import "OFSocketObserver.h"

#import "OFHash.h"
#import "OFMD5Hash.h"
#import "OFSHA1Hash.h"

#import "OFXMLAttribute.h"
#import "OFXMLElement.h"







>



|

<







24
25
26
27
28
29
30
31
32
33
34
35
36

37
38
39
40
41
42
43

#import "OFDictionary.h"
#import "OFEnumerator.h"

#import "OFNumber.h"

#import "OFStream.h"
#import "OFStreamObserver.h"

#import "OFFile.h"

#import "OFStreamSocket.h"
#import "OFTCPSocket.h"


#import "OFHash.h"
#import "OFMD5Hash.h"
#import "OFSHA1Hash.h"

#import "OFXMLAttribute.h"
#import "OFXMLElement.h"