-
Notifications
You must be signed in to change notification settings - Fork 13
/
MySocket.h
139 lines (110 loc) · 4.15 KB
/
MySocket.h
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
128
129
130
131
132
133
134
135
136
137
138
139
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
/*
* MySocket is my 24 hour hackjob. There are other classes which would have
* done a better job, but I found them to be too slow. This class wraps
* around BSD sockets directly. It uses two threads for both asynchronous
* reads and writes. Performance still isn't spectacular... I measure about
* 58 MB/s average when transfering to localhost.
* The way the old The Cheat did networking was by spawning a new thread for
* each connection (for both serving and receiving). I thought that this way
* would be more efficient, but the old way is noticeably faster. I may go
* back to that way eventually, after all it isn't that many threads.
*/
/*
* June 21 2005 - Chaz
* I have written a much more robust socket wrapper for another project that
* could be re-emplemented into The Cheat. It is cleaner code and has very
* nice statistics tracking features. I'm going on my mission tomorrow. This
* will have to wait two years or so. ;)
*/
#import <Cocoa/Cocoa.h>
#import "ChazLog.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <string.h>
/*
* This class has a few limitations which may not be obvious. First, it will
* only send back delegate methods on the Main thread, not necessarily the
* same thread used to create the socket. There is no way to time out a read
* or write action. There is no way to just read "all" data possible; you
* must specify how much data to receive.
*/
@interface MySocket : NSObject
{
int _sockfd;
NSMutableArray *_readQueue;
NSMutableArray *_writeQueue;
NSMutableData *_unclaimedData;
NSRecursiveLock *_readLock;
NSRecursiveLock *_writeLock;
unsigned _flags;
unsigned _bytesRead;
unsigned _bytesWritten;
NSTimeInterval _startTime;
unsigned _lastBytesRead;
unsigned _lastBytesWritten;
NSTimeInterval _lastBytesReadTime;
NSTimeInterval _lastBytesWrittenTime;
id _delegate;
}
/* INITIALIZATION */
- (id)initWithDelegate:(id)delegate;
/* CONNECTING/ACCEPTING */
- (BOOL)connectToHost:(NSString *)host port:(int)port;
- (BOOL)connectToAddress:(const struct sockaddr *)addr length:(unsigned)addrLen;
- (BOOL)connectToAddressWithData:(NSData *)addr;
- (BOOL)listenOnPort:(int)port;
// returns YES on success
/* OBVIOUS */
- (void)disconnect;
// the disconnect delegate method will NOT be called.
// it is only called when the socket is disconnect by remote or by error.
// absolutely NO delegate methods are sent after this (until reconnecting).
/* READING & WRITING */
- (void)readDataToLength:(unsigned)len tag:(int)tag;
- (void)writeData:(NSData *)data tag:(int)tag;
- (void)writeBytes:(void const *)bytes length:(unsigned)len tag:(int)tag;
/* Nope, there is no way to time out a request. */
/* STATISTICS */
- (unsigned)bytesRead;
- (unsigned)bytesWritten;
/* the above accessors can be accessed after the socket is disconnected to get
the grand total amount of traffic passed through the socket. */
- (NSTimeInterval)timeConnected;
- (double)readSpeed; // bytes/sec
- (double)writeSpeed; // bytes/sec
/* These speeds are averaged out using the last time these methods were called.
The more often you call these methods the more accurate they will be. */
- (NSString *)localHost;
- (int)localPort;
- (NSString *)remoteHost;
- (int)remotePort;
- (BOOL)isConnected;
- (BOOL)isListener;
/* CONVENIENCE */
+ (NSData *)addressWithHost:(NSString *)host port:(int)port;
/* DELEGATION */
- (id)delegate;
- (void)setDelegate:(id)delegate;
@end
@interface NSObject ( MySocketDelegate )
/* DELEGATE METHODS */
- (void)socketDidDisconnect:(MySocket *)mySocket;
- (void)socket:(MySocket *)mySocket didAcceptSocket:(MySocket *)newSocket;
- (void)socket:(MySocket *)mySocket didReadData:(NSData *)theData tag:(int)tag;
- (void)socket:(MySocket *)mySocket didWriteDataWithTag:(int)tag;
@end