forked from johndbritton/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPMessage.m
189 lines (144 loc) · 4.03 KB
/
TPMessage.m
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// TPMessage.m
// Teleport
//
// Created by JuL on Wed Dec 03 2003.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPMessage.h"
#import "TPRemoteHost.h"
#import "TPLocalHost.h"
#import "TPUtils.h"
const unsigned TPMessageHeaderLength = sizeof(TPMsgType)+sizeof(TPDataLength);
@interface TPMessage (Private)
- (instancetype) initWithType:(TPMsgType)type;
- (instancetype) initWithType:(TPMsgType)type andData:(NSData*)data;
- (instancetype) initWithType:(TPMsgType)type andString:(NSString*)string;
- (instancetype) initWithType:(TPMsgType)type andInfoDict:(NSDictionary*)infoDict;
@end
@implementation TPMessage
- (instancetype) init
{
self = [super init];
_additionalData = nil;
return self;
}
- (instancetype) initWithRawData:(NSData*)rawData
{
self = [self init];
int pos = 0;
@try {
/* Read message type */
[rawData _readBytes:&_msgType withSize:sizeof(TPMsgType) atPos:&pos];
/* Read data length */
[rawData _readBytes:&_dataLength withSize:sizeof(TPDataLength) atPos:&pos];
if([self dataLength] > 0) {
TPDataLength dataLength = [self dataLength];
if(dataLength > [rawData length]-pos) { // not enough data!
return nil;
}
else
_additionalData = [[rawData subdataWithRange:NSMakeRange(pos, dataLength)] copy];
}
else
_additionalData = nil;
}
@catch(NSException * exception) {
return nil;
}
return self;
}
- (NSData*)rawData
{
NSMutableData * rawData = [[NSMutableData alloc] init];
/* Write message type */
[rawData appendData:[NSData dataWithBytes:&_msgType length:sizeof(TPMsgType)]];
/* Optionally write data */
if(_additionalData != nil) {
_dataLength = NSSwapHostLongLongToBig([_additionalData length]);
[rawData appendData:[NSData dataWithBytes:&_dataLength length:sizeof(TPDataLength)]];
[rawData appendData:_additionalData];
}
else {
_dataLength = NSSwapHostLongLongToBig(0);
[rawData appendData:[NSData dataWithBytes:&_dataLength length:sizeof(TPDataLength)]];
}
return rawData;
}
#pragma mark -
#pragma mark Protocol messages
- (instancetype) initWithType:(TPMsgType)type
{
self = [self init];
_msgType = NSSwapHostIntToBig(type);
return self;
}
- (instancetype) initWithType:(TPMsgType)type andData:(NSData*)data
{
self = [self initWithType:type];
_additionalData = data;
return self;
}
- (instancetype) initWithType:(TPMsgType)type andString:(NSString*)string
{
self = [self initWithType:type];
_additionalData = [string dataUsingEncoding:NSUTF8StringEncoding];
return self;
}
- (instancetype) initWithType:(TPMsgType)type andInfoDict:(NSDictionary*)infoDict
{
self = [self initWithType:type];
_additionalData = [NSArchiver archivedDataWithRootObject:infoDict];
return self;
}
+ (instancetype) messageWithType:(TPMsgType)type
{
TPMessage * message = [[TPMessage alloc] initWithType:type];
return message;
}
+ (instancetype) messageWithType:(TPMsgType)type andData:(NSData*)data
{
TPMessage * message = [[TPMessage alloc] initWithType:type andData:data];
return message;
}
+ (instancetype) messageWithType:(TPMsgType)type andString:(NSString*)string
{
TPMessage * message = [[TPMessage alloc] initWithType:type andString:string];
return message;
}
+ (instancetype) messageWithType:(TPMsgType)type andInfoDict:(NSDictionary*)infoDict
{
TPMessage * message = [[TPMessage alloc] initWithType:type andInfoDict:infoDict];
return message;
}
#pragma mark -
#pragma mark Accessors
- (TPMsgType)msgType
{
return NSSwapBigIntToHost(_msgType);
}
- (TPDataLength)msgLength
{
return [self dataLength] + TPMessageHeaderLength;
}
- (TPDataLength)dataLength
{
return NSSwapBigLongLongToHost(_dataLength);
}
- (NSData*)data
{
return _additionalData;
}
- (NSString*)string
{
return [[NSString alloc] initWithData:_additionalData encoding:NSUTF8StringEncoding];
}
- (NSDictionary*)infoDict
{
return (NSDictionary*)[NSUnarchiver unarchiveObjectWithData:_additionalData];
}
- (NSString*)description
{
return [NSString stringWithFormat:@"%@ type=%ld data length=%lu", [super description], _msgType, (unsigned long)[_additionalData length]];
}
@end