ObjFW  Diff

Differences From Artifact [8ed3399f44]:

To Artifact [43c1e24000]:

  • File src/OFStreamSocket.m — part of check-in [4e59d2692f] at 2014-04-26 00:40:17 on branch trunk — Fix a few issues on LLP64 and Win64

    LLP64 was mostly fast enumeration using an unsigned long for the state,
    which can't store a pointer or a size_t on LLP64. This is now solved by
    either throwing an OFOutOfRangeException if the value of the size_t is
    bigger than ULONG_MAX or storing the pointer in the extra field (copied
    using memcpy, as it's an array of unsigned long, which again would be
    too small to store a pointer).

    Win64 was mostly Microsoft not being able to decide whether a length is
    a size_t, a DWORD, an int or an unsigned int (thus the different types
    in places that seem to be almost the same). But since that would not be
    confusing enough, a file descriptor is an int if it's for a file, but a
    long long if it is for a socket. But of course, for ReadFile and friends
    it's a DWORD instead of an int then. (user: js, size: 3691) [annotate] [blame] [check-ins using]


22
23
24
25
26
27
28

29
30
31
32
33
34
35

#include <errno.h>

#import "OFStreamSocket.h"

#import "OFInitializationFailedException.h"
#import "OFNotConnectedException.h"

#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"

#import "socket_helpers.h"

@implementation OFStreamSocket







>







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

#include <errno.h>

#import "OFStreamSocket.h"

#import "OFInitializationFailedException.h"
#import "OFNotConnectedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"

#import "socket_helpers.h"

@implementation OFStreamSocket
66
67
68
69
70
71
72

73
74
75








76
77
78
79
80
81
82

		e = [OFReadFailedException exceptionWithObject: self
					       requestedLength: length];
		e->_errNo = ENOTCONN;
		@throw e;
	}


	if ((ret = recv(_socket, buffer, length, 0)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];









	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}








>



>
>
>
>
>
>
>
>







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

		e = [OFReadFailedException exceptionWithObject: self
					       requestedLength: length];
		e->_errNo = ENOTCONN;
		@throw e;
	}

#ifndef _WIN32
	if ((ret = recv(_socket, buffer, length, 0)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
#else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = recv(_socket, buffer, (unsigned int)length, 0)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

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
128

		e = [OFWriteFailedException exceptionWithObject: self
						requestedLength: length];
		e->_errNo = ENOTCONN;
		@throw e;
	}


	if (send(_socket, buffer, length, 0) < length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];








}

#ifdef _WIN32
- (void)setBlocking: (bool)enable
{
	u_long v = enable;
	_blocking = enable;

	if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}
#endif

- (int)fileDescriptorForReading
{

	return _socket;






}

- (int)fileDescriptorForWriting
{

	return _socket;






}

- (void)close
{
	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];








>



>
>
>
>
>
>
>
>















>

>
>
>
>
>
>




>

>
>
>
>
>
>







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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

		e = [OFWriteFailedException exceptionWithObject: self
						requestedLength: length];
		e->_errNo = ENOTCONN;
		@throw e;
	}

#ifndef _WIN32
	if (send(_socket, buffer, length, 0) < length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
#else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if (send(_socket, buffer, (unsigned int)length, 0) < length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
#endif
}

#ifdef _WIN32
- (void)setBlocking: (bool)enable
{
	u_long v = enable;
	_blocking = enable;

	if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}
#endif

- (int)fileDescriptorForReading
{
#ifndef _WIN32
	return _socket;
#else
	if (_socket > INT_MAX)
		@throw [OFOutOfRangeException exception];

	return (int)_socket;
#endif
}

- (int)fileDescriptorForWriting
{
#ifndef _WIN32
	return _socket;
#else
	if (_socket > INT_MAX)
		@throw [OFOutOfRangeException exception];

	return (int)_socket;
#endif
}

- (void)close
{
	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];