Building a Lightweight RunLoop, Part 2

Preface

Continuing from the previous article, Building a Lightweight RunLoop, Part 1, let’s keep analyzing.

LWURLConnection

LWURLConnection is one of the more interesting parts of this framework, and also one of the more complex ones. After reading it, it should help a lot with understanding AFNetworking. Here is the code directly:

1
2
3
4
5
6
7
8
9
10
- (void)performURLConnectionOnRunLoopThread
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.11:8080/v1/list/post"]];
request.HTTPMethod = @"POST";
NSString *content = @"name=john&address=beijing&mobile=140005";
request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
LWURLConnection *conn = [[LWURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn scheduleInRunLoop:_lwRunLoopThread.looper];
[conn start];
}

LWURLConnection Initialization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (instancetype _Nonnull)initWithRequest:(NSMutableURLRequest * _Nullable)request
delegate:(nullable id)delegate
startImmediately:(BOOL)startImmediately
{
if (self = [super init]) {
_internal = [[LWConnectionInternal alloc] initWithRequest:request];
_internal.delegate = self;
_startImmediately = startImmediately;
_delegate = delegate;
}
return self;
}

- (void)start
{
[_internal start];
}

LWURLConnection holds a LWConnectionInternal internally and sets itself as the delegate of LWConnectionInternal.

LWConnectionInternal Initialization

1
2
3
4
5
6
7
- (instancetype)init
{
if (self = [super init]) {
_helper = new LWConnHelper();
}
return self;
}
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
- (void)start
{
// Restart on a child thread.
[NSThread detachNewThreadSelector:@selector(startInternal) toTarget:self withObject:nil];
}

- (void)startInternal
{
// Set context.
LWConnHelperContext context = {(__bridge void *)self,TimeOutCallBackRoutine, ReceiveCallBackRoutine, FinshCallBackRoutine, FailureCallBackRoutine};
_helper->setLWConnHelperContext(&context);

// Establish connection.
if ([self establishConnection])
{
// Prepare request headers.
[self prepareHttpRequest];
if ((strcasecmp("post", [request.HTTPMethod UTF8String]) == 0) && request.HTTPBody.length > 0) {
// Prepare request body.
[self prepareHTTPBody];
}
if (request.timeoutInterval <= 0) {
request.timeoutInterval = 60;
}
_helper->createHttpRequest(request.timeoutInterval);
}
else
{
_helper->closeConn();
[self failure];
}
}

The start method starts a child thread and executes startInternal. Inside that method, it establishes the connection, prepares request parameters, and creates the HTTP request. Let’s break it down step by step.

Establishing the Socket Connection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (BOOL)establishConnection
{
// Get IP and port.
NSURL *targetURL = request.URL;
NSString *host = targetURL.host;
NSInteger port = [targetURL.port intValue];
char *ip = _helper->resolveHostName([host UTF8String]);
if (ip == NULL) {
NSLog(@"resolve host name failure");
return NO;
}

// Use LWConnHelper to establish connection.
return _helper->establishSocket(ip, (int)port);
}
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
bool LWConnHelper::establishSocket(const char *ip, const int port)
{
// Convert IP and port.
struct sockaddr_in serverAddr;
serverAddr.sin_len = sizeof(struct sockaddr_in);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
if (inet_aton(ip, &serverAddr.sin_addr) == 0) {
printf("address error\n");
return false;
}
inet_aton(ip, &serverAddr.sin_addr);

// Create a socket to open a network connection. If successful,
// it returns a network file descriptor.
this->mSockFd = socket(AF_INET, SOCK_STREAM, 0);

// Set the socket descriptor to non-blocking IO.
int flag = fcntl(this->mSockFd, F_GETFL, NULL);
flag |= O_NONBLOCK;
fcntl(this->mSockFd, F_SETFL, flag);

// Connect to the server and communicate.
int ret = connect(this->mSockFd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));

if (ret < 0) {
return false;
}

return true;
}

Preparing Request Headers and Body

This part is responsible for request-parameter setup.

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
- (void)prepareHttpRequest
{
NSURL *targetURL = request.URL;
NSString *httpMethod = request.HTTPMethod;
NSString *path = targetURL.path;
NSString *host = targetURL.host;

NSMutableString *httpRequestLineAndHeader = [[NSMutableString alloc] init];

NSString *requestLine = [NSString stringWithFormat:@"%@ %@ HTTP/1.1 \r\n",httpMethod, path];
NSString *hostHeader = [NSString stringWithFormat:@"HOST: %@ \r\n", host];
[httpRequestLineAndHeader appendString:requestLine];
[httpRequestLineAndHeader appendString:hostHeader];

NSMutableDictionary *allHTTPHeaderFields = [[NSMutableDictionary alloc] init];
[allHTTPHeaderFields setValue:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];
[allHTTPHeaderFields setValue:@(request.HTTPBody.length) forKey:@"Content-Length"];
[allHTTPHeaderFields setValue:@"wuyunfeng@LWURLConnection" forKey:@"Accept"];
[allHTTPHeaderFields setValue:@"gzip, deflate" forKey:@"Accept-Encoding"];
[allHTTPHeaderFields setValue:@"utf-8" forKey:@"Accept-Charset"];
[allHTTPHeaderFields setValue:@"LWRunLoopAgent" forKey:@"User-Agent"];
[allHTTPHeaderFields setValue:@"no-cache" forKey:@"Cache-Control"];
[allHTTPHeaderFields setValue:@"close" forKey:@"Connection"];
[allHTTPHeaderFields addEntriesFromDictionary:request.allHTTPHeaderFields];
NSString *httpHeaderAndValues = LWHeaderStringFromHTTPHeaderFieldsDictironary(allHTTPHeaderFields);
[httpRequestLineAndHeader appendString:httpHeaderAndValues];

// Send the request line and headers.
_helper->sendMsg([httpRequestLineAndHeader UTF8String], (int)httpRequestLineAndHeader.length);
}
1
2
3
4
5
- (void)prepareHTTPBody
{
NSData *data = request.HTTPBody;
_helper->sendMsg((const char *)([data bytes]), (int)[data length]);
}
1
2
3
4
5
6
7
8
9
10
void LWConnHelper::sendMsg(const char *content, int length)
{
if (content == NULL) {
return;
}
ssize_t mWrite;
do {
mWrite = write(this->mSockFd, content, length);
} while (mWrite == -1 && errno == EINTR);
}

Creating the HTTP Request

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
void LWConnHelper::createHttpRequest(int timeoutMills)
{
fd_set readfds;

// Convert timeout.
struct timeval timeout;
timeout.tv_sec = timeoutMills / 1000;
timeout.tv_usec = timeoutMills % 1000 * 1000;

// Set readset descriptor.
int maxfd = -1;
FD_ZERO(&readfds);
maxfd = this->mSockFd + 1;

int ret;
do
{
FD_SET(this->mSockFd, &readfds);

// Check whether the network has responded.
ret = select(maxfd, &readfds, NULL, NULL, &timeout);

// Timeout.
if (0 == ret) {
if (this->mContext->LWConnectionTimeOutCallBack != NULL) {
this->mContext->LWConnectionTimeOutCallBack(this->mContext->info);
}
}

// Check whether mSockFd is in the returned readfds set.
if (FD_ISSET(this->mSockFd, &readfds)) {
char buffer[4 * 1024];
ssize_t nRead;
do {
// On success, returns bytes read. On error, returns -1 and sets errno.
// If EOF was reached before read is called, read returns 0.
nRead = read(this->mSockFd, buffer, sizeof(buffer));

// Callback for each chunk of data.
if (this->mContext->LWConnectionReceiveCallBack != NULL) {
this->mContext->LWConnectionReceiveCallBack(this->mContext->info, buffer, (int)nRead);
}
} while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));

// Finish callback.
if (this->mContext->LWConnectionFinishCallBack) {
this->mContext->LWConnectionFinishCallBack(this->mContext->info);
}
}
} while (-1 == ret && errno == EINTR);

// Failure.
if (-1 == ret)
{
if (this->mContext->LWConnectionFailureCallBack != NULL) {
this->mContext->LWConnectionFailureCallBack(this->mContext->info, -1);
}
}
// Close connection.
closeConn();
}

Actually, the name createHttpRequest is not quite accurate. Inside this method, it is already waiting for network data through IO multiplexing with select. The request itself has already been sent in prepareHttpRequest.

Network Callback Flow

1
2
3
4
5
6
7
8
9
10
@class LWConnectionInternal;
@protocol LWConnectionInternalDelegate <NSObject>

- (void)internal_connection:(LWConnectionInternal * _Nonnull)connection didReceiveData:(NSData * _Nullable)data;

- (void)internal_connection:(LWConnectionInternal * _Nonnull)connection didFailWithError:(NSError * _Nullable)error;

- (void)internal_connectionDidFinishLoading:(LWConnectionInternal * _Nonnull)connection;

@end

LWConnectionInternal has a LWConnectionInternalDelegate internally, and it delegates the network result to LWURLConnection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)internal_connectionDidFinishLoading:(LWConnectionInternal * _Nonnull)connection
{
if ([self.delegate respondsToSelector:@selector(lw_connectionDidFinishLoading:)]) {
id target = self.delegate;
NSMethodSignature *sig = [target methodSignatureForSelector:@selector(lw_connectionDidFinishLoading:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
invocation.target = self.delegate;
invocation.selector = @selector(lw_connectionDidFinishLoading:);
id argument = self;
[invocation setArgument:&argument atIndex:2];
[invocation retainArguments];
LWMessage *msg = [[LWMessage alloc] initWithTarget:invocation aSel:@selector(invoke) withArgument:nil at:0];
[_runloop postMessage:msg];
}
}

Using internal_connectionDidFinishLoading as an example, it wraps the delegate method of LWURLConnection, which is the outer business layer, into an LWMessage, then calls the familiar [_runloop postMessage:msg], sending it into the RunLoop message queue.

So LWURLConnection itself performs network requests based on socket and select. Once the network result comes back, it interacts with the RunLoop again: the delegate method is wrapped as a message and added to the RunLoop message queue. What happens next is handled by the RunLoop itself.

LWPort

The most basic concept in Mach is the message. Messages are passed between two ports. By design, any two ports can pass messages to each other, whether they are on the same machine or on a remote host. The source code discussed here uses ports on the same machine.

Sending a message from one port to another is essentially placing the message into a queue until the receiver can process it.

For example, CFRunLoop has two kinds of CFRunLoopSource:

  • source0: handles events such as UIEvent and CFSocket.
  • source1: Mach-port-driven, such as CFMachPort and CFMessagePort.

From the name alone, source1 is clearly related to ports.

Leader

First create a thread, obtain the corresponding runloop, define a LWSocketPort object named _leaderPort, set its port to 8082, and add _leaderPort to the runloop.

1
2
3
4
5
6
7
8
@autoreleasepool
{
LWRunLoop *looper = [LWRunLoop currentLWRunLoop];
_leaderPort = [[LWSocketPort alloc] initWithTCPPort:8082];
_leaderPort.delegate = self;
[looper addPort:_leaderPort forMode:LWDefaultRunLoop];
[looper runMode:LWDefaultRunLoop];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)addPort:(LWPort *)aPort forMode:(NSString *)mode
{
if (_allPorts) {
_allPorts = [[NSMutableArray alloc] init];
}
[_allPorts addObject:aPort];
if ([aPort isKindOfClass:[LWSocketPort class]]) {
LWSocketPort *socketTypePort = (LWSocketPort *)aPort;
int fd = socketTypePort.socket;
LWSocketPortRoleType roleType = LWSocketPortRoleTypeLeader;
LWPortContext context = socketTypePort.context;
[_queue.nativeRunLoop addFd:fd type:LWNativeRunLoopFdSocketServerType filter:LWNativeRunLoopEventFilterRead callback:context.LWPortReceiveDataCallBack data:context.info];
}
}

Follower

At the same time, another thread is created with detachNewThreadSelector, and a WorkerClass object is defined.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
_worker = [[WorkerClass alloc] init];
[NSThread detachNewThreadSelector:@selector(launchThreadWithPort:) toTarget:_worker withObject:_leaderPort];

- (void)launchThreadWithPort:(LWPort *)port
{
@autoreleasepool {
[self prepare:port];
}
}

- (void)prepare:(LWPort *)port
{
_workPortRunLoopThread = [NSThread currentThread];
[NSThread currentThread].name = @"workerPortLoopThread";
_distantPort = (LWSocketPort *)port;
_localPort = [[LWSocketPort alloc] initWithTCPPort:8082];
_localPort.delegate = self;
[_localPort setType:LWSocketPortRoleTypeFollower];
LWRunLoop *_currentRunLoop = [LWRunLoop currentLWRunLoop];
[_currentRunLoop addPort:_localPort forMode:LWDefaultRunLoop];
[_currentRunLoop runMode:LWDefaultRunLoop];
}

When the follower port is added, the runloop registers it as a socket-client file descriptor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)addPort:(LWPort *)aPort forMode:(NSString *)mode
{
if (_allPorts) {
_allPorts = [[NSMutableArray alloc] init];
}
[_allPorts addObject:aPort];
if ([aPort isKindOfClass:[LWSocketPort class]]) {
LWSocketPort *socketTypePort = (LWSocketPort *)aPort;
int fd = socketTypePort.socket;
LWSocketPortRoleType roleType = LWSocketPortRoleTypeFollower;
LWPortContext context = socketTypePort.context;
[_queue.nativeRunLoop addFd:fd type:LWNativeRunLoopFdSocketClientType filter:LWNativeRunLoopEventFilterRead callback:context.LWPortReceiveDataCallBack data:context.info];
}
}

LWSocket

Now look at the initialization of LWSocket:

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
- (BOOL)initInternalWithTCPPort:(unsigned short)port
{
if ((_sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
return NO;
}
_context.info = (__bridge void *)(self);
_context.LWPortReceiveDataCallBack = PortBasedReceiveDataRoutine;
struct sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAddr.sin_port = htons(port);
_roleType = LWSocketPortRoleTypeLeader;//leader
int option = 1;
setsockopt(_sockFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));

// if bind fails, _sockFd becomes follower; otherwise leader.
if (-1 == bind(_sockFd, (struct sockaddr *)&sockAddr, sizeof(sockAddr))) {
_roleType = LWSocketPortRoleTypeFollower;//follower
}

if (_roleType == LWSocketPortRoleTypeLeader) {
if (listen(_sockFd, 5) == -1) {
return NO;
}
[self setValid:YES];
_port = port;
} else {
// We can ignore the connect delay for local TCP connect.
int flag = connect(_sockFd, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
if (-1 == flag) {
return NO;
}
struct sockaddr_in name;
socklen_t namelen = sizeof(name);
getsockname(_sockFd, (struct sockaddr *)&name, &namelen);
_port = name.sin_port;
[self setValid:YES];
}
return YES;
}
  • For the leader, the main job is to define a socket, open a network communication port, bind a fixed network address such as 127.0.0.1 and port 8082, then call listen to listen for requests.
  • For the follower, the main job is to define a socket with an automatically assigned port, then call connect to connect to the server. connect and bind have similar parameter forms. The difference is that bind receives the local address, while connect receives the peer address.
  • This is very typical socket programming.

Message Sending

At this point, both FollowPort and LeaderPort have their own threads and corresponding runloops.

Take the follower sending a message to the leader as an example. First package the message as LWPortMessage, then call sendBeforeDate, which enters internalSendBeforDate.

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
- (void)actualSendContent:(id)content
{
int length = (int)[content length];
NSMutableData *data = [[NSMutableData alloc] init];
[data appendBytes:&length length:sizeof(int)];
[data appendData:[content dataUsingEncoding:NSUTF8StringEncoding]];
LWPortMessage *message = [[LWPortMessage alloc] initWithSendPort:_localPort receivePort:_distantPort components:data];
[message sendBeforeDate:0];
}

- (BOOL)sendBeforeDate:(NSInteger)delay
{
[self internalSendBeforDate:delay];
return YES;
}

- (void)internalSendBeforDate:(NSInteger)delay
{
LWSocketPort *_sendSocketPort = (LWSocketPort *)_sendPort;
LWSocketPort *_receiveSocketPort = (LWSocketPort *)_receivePort;
LWRunLoop *runloop = [LWRunLoop currentLWRunLoop];
// send data from leader to follower
if (_sendSocketPort.roleType == LWSocketPortRoleTypeLeader) {
short port = _receiveSocketPort.port;
[runloop send:_components toPort:port];
} else {// send data from follower to leader
int fd = _sendSocketPort.socket;
[runloop send:_components toFd:fd];
}
}

In internalSendBeforDate, it first obtains the destination port, then calls the runloop’s send method:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)send:(NSData *)data toFd:(int)fd
{
[_queue.nativeRunLoop send:data toFd:fd];
}

- (void)send:(NSData *)data toFd:(int)fd
{
ssize_t nWrite;
do {
nWrite = write(fd, [data bytes], [data length]);
} while (nWrite == -1 && errno == EINTR);
}

Message Receiving

In the leader’s runloop, kevent detects a change on the socket file descriptor and wakes up the thread. It then calls handleFollowerToLeader, reads out the message sent earlier, and one complete message send/receive flow ends.

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
- (BOOL)handleFollowerToLeader:(int)event fd:(int)fd
{
if (event & EVFILT_READ) {
int length = 0;
ssize_t nRead;
do {
nRead = read(fd, &length, sizeof(int));
} while (nRead == -1 && EINTR == errno);
if (nRead == -1 && EAGAIN == errno) {
// The file was marked for non-blocking I/O, and no data were ready to be read.
return false;
}
// Buffer sent from follower LWPort to leader LWPort.
char *buffer = malloc(length);
do {
nRead = read(fd, buffer, length);
} while (nRead == -1 && EINTR == errno);
NSValue *data = [_requests objectForKey:@(_leader)];
PortWrapper request;
[data getValue:&request];
// Notify leader.
if (request.callback) {
request.callback(fd, request.info, buffer, length);
}
// Remember to release malloc memory.
free(buffer);
buffer = NULL;
struct sockaddr_in sockaddr;
socklen_t len;
int ret = getpeername(fd, (struct sockaddr *)&sockaddr, &len);
if (ret < 0) {
return false;
}
LWPortClientInfo *info = [_portClients valueForKey:[NSString stringWithFormat:@"%d", sockaddr.sin_port]];
if (info.cacheSend && info.cacheSend.length > 0) {
// Write cached data on next event.
[self kevent:fd filter:EVFILT_WRITE action:EV_ADD];
}
} else if (event & EVFILT_WRITE) {
struct sockaddr_in sockaddr;
socklen_t len;
int ret = getpeername(fd, (struct sockaddr *)&sockaddr, &len);
if (ret < 0) {
return false;
}
LWPortClientInfo *info = [_portClients valueForKey:[NSString stringWithFormat:@"%d", sockaddr.sin_port]];
if (info.cacheSend && info.cacheSend.length > 0) {
ssize_t nWrite;
do {
nWrite = write(fd, [info.cacheSend bytes], info.cacheSend.length);
} while (nWrite == -1 && errno == EINTR);

if (nWrite != 1 && errno != EAGAIN) {
return false;
}
// Clean the sending cache.
info.cacheSend = nil;
} else {
return false;
}
}
return true;
}

Summary

After analyzing this source code, go back and think carefully about RunLoop again. If possible, dig deeper. If this article can improve your understanding even a little compared with before, then it has served its purpose.

Finally, I will borrow another diagram from the author’s GitHub. Thanks again to wuyunfeng.

Building a Lightweight RunLoop, Part 2: Summary (figure 1)
-------------本文结束 感谢您的阅读-------------
If this article helped you, your support encourages me to keep writing.

Title:Building a Lightweight RunLoop, Part 2

Author:lingyun

Published:2018-06-15 21:12

Updated:2026-09-23 00:37

Original link:https://tsuijunxi.github.io/en/2018/06/15/building-a-lightweight-runloop-part-2/

License: Attribution-NonCommercial-NoDerivatives 4.0 International Please keep the original link and author when sharing.