Skip to content

Commit dc99402

Browse files
committed
tcp ping done
1 parent d2e83f1 commit dc99402

File tree

8 files changed

+207
-13
lines changed

8 files changed

+207
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ DerivedData
2323
# you should judge for yourself, the pros and cons are mentioned at:
2424
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
2525
#
26-
#Pods/
26+
Pods/
27+
Podfile.lock

NetDiag.xcodeproj/project.pbxproj

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
A6296E24683C84AC5548DB39 /* libPods-NetDiagTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F50835689CBA65323B9B2AA7 /* libPods-NetDiagTests.a */; };
1011
DF2B76EC1C33702D00643678 /* QNNPing.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2B76EB1C33702D00643678 /* QNNPing.m */; };
1112
DF2B76F11C3B9DA100643678 /* QNNSimplePing.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2B76F01C3B9DA100643678 /* QNNSimplePing.m */; };
1213
DF8DFFC51C60951D00174DA9 /* QNNNslookup.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFC41C60951D00174DA9 /* QNNNslookup.m */; };
1314
DF8DFFD21C61DA1B00174DA9 /* TcpPingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */; };
1415
DF8DFFD41C61DA1B00174DA9 /* libNetDiag.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DFCEC6451C2EC01D0058EE65 /* libNetDiag.a */; };
16+
DF8DFFDC1C61E6B200174DA9 /* QNTestLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFDB1C61E6B200174DA9 /* QNTestLogger.m */; };
1517
DF9D41FA1C57787500486F87 /* QNNTcpPing.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D41F91C57787500486F87 /* QNNTcpPing.m */; };
1618
DF9D41FD1C57789B00486F87 /* QNNUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D41FC1C57789B00486F87 /* QNNUtil.m */; };
1719
DF9D42001C5778B600486F87 /* QNNRtmp.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D41FF1C5778B600486F87 /* QNNRtmp.m */; };
@@ -44,6 +46,7 @@
4446
/* End PBXCopyFilesBuildPhase section */
4547

4648
/* Begin PBXFileReference section */
49+
32AB6E052E20B19230FF87D1 /* Pods-NetDiagTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NetDiagTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests.debug.xcconfig"; sourceTree = "<group>"; };
4750
DF2B76EA1C33702D00643678 /* QNNPing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNPing.h; sourceTree = "<group>"; };
4851
DF2B76EB1C33702D00643678 /* QNNPing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNPing.m; sourceTree = "<group>"; };
4952
DF2B76ED1C33706900643678 /* QNNProtocols.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNProtocols.h; sourceTree = "<group>"; };
@@ -54,6 +57,8 @@
5457
DF8DFFCF1C61DA1B00174DA9 /* NetDiagTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NetDiagTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5558
DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TcpPingTest.m; sourceTree = "<group>"; };
5659
DF8DFFD31C61DA1B00174DA9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
60+
DF8DFFDA1C61E6B200174DA9 /* QNTestLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestLogger.h; sourceTree = "<group>"; };
61+
DF8DFFDB1C61E6B200174DA9 /* QNTestLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNTestLogger.m; sourceTree = "<group>"; };
5762
DF9D41F81C57787500486F87 /* QNNTcpPing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNTcpPing.h; sourceTree = "<group>"; };
5863
DF9D41F91C57787500486F87 /* QNNTcpPing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNTcpPing.m; sourceTree = "<group>"; };
5964
DF9D41FB1C57789B00486F87 /* QNNUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNUtil.h; sourceTree = "<group>"; };
@@ -66,6 +71,8 @@
6671
DF9D42081C57BEFD00486F87 /* QNNExternalIp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNExternalIp.m; sourceTree = "<group>"; };
6772
DFCEC6451C2EC01D0058EE65 /* libNetDiag.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libNetDiag.a; sourceTree = BUILT_PRODUCTS_DIR; };
6873
DFCEC6481C2EC01D0058EE65 /* QNNetDiag.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QNNetDiag.h; sourceTree = "<group>"; };
74+
E7FCF14D691EF09CEBD2808D /* Pods-NetDiagTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NetDiagTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests.release.xcconfig"; sourceTree = "<group>"; };
75+
F50835689CBA65323B9B2AA7 /* libPods-NetDiagTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-NetDiagTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6976
/* End PBXFileReference section */
7077

7178
/* Begin PBXFrameworksBuildPhase section */
@@ -74,6 +81,7 @@
7481
buildActionMask = 2147483647;
7582
files = (
7683
DF8DFFD41C61DA1B00174DA9 /* libNetDiag.a in Frameworks */,
84+
A6296E24683C84AC5548DB39 /* libPods-NetDiagTests.a in Frameworks */,
7785
);
7886
runOnlyForDeploymentPostprocessing = 0;
7987
};
@@ -87,11 +95,30 @@
8795
/* End PBXFrameworksBuildPhase section */
8896

8997
/* Begin PBXGroup section */
98+
0C4135454D851F8DF38E206F /* Pods */ = {
99+
isa = PBXGroup;
100+
children = (
101+
32AB6E052E20B19230FF87D1 /* Pods-NetDiagTests.debug.xcconfig */,
102+
E7FCF14D691EF09CEBD2808D /* Pods-NetDiagTests.release.xcconfig */,
103+
);
104+
name = Pods;
105+
sourceTree = "<group>";
106+
};
107+
7EC51E005E7B857726052BEF /* Frameworks */ = {
108+
isa = PBXGroup;
109+
children = (
110+
F50835689CBA65323B9B2AA7 /* libPods-NetDiagTests.a */,
111+
);
112+
name = Frameworks;
113+
sourceTree = "<group>";
114+
};
90115
DF8DFFD01C61DA1B00174DA9 /* NetDiagTests */ = {
91116
isa = PBXGroup;
92117
children = (
93118
DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */,
94119
DF8DFFD31C61DA1B00174DA9 /* Info.plist */,
120+
DF8DFFDA1C61E6B200174DA9 /* QNTestLogger.h */,
121+
DF8DFFDB1C61E6B200174DA9 /* QNTestLogger.m */,
95122
);
96123
path = NetDiagTests;
97124
sourceTree = "<group>";
@@ -102,6 +129,8 @@
102129
DFCEC6471C2EC01D0058EE65 /* NetDiag */,
103130
DF8DFFD01C61DA1B00174DA9 /* NetDiagTests */,
104131
DFCEC6461C2EC01D0058EE65 /* Products */,
132+
0C4135454D851F8DF38E206F /* Pods */,
133+
7EC51E005E7B857726052BEF /* Frameworks */,
105134
);
106135
sourceTree = "<group>";
107136
};
@@ -146,9 +175,12 @@
146175
isa = PBXNativeTarget;
147176
buildConfigurationList = DF8DFFD71C61DA1B00174DA9 /* Build configuration list for PBXNativeTarget "NetDiagTests" */;
148177
buildPhases = (
178+
8E2B56729DA1DBF78DC0ECCD /* Check Pods Manifest.lock */,
149179
DF8DFFCB1C61DA1B00174DA9 /* Sources */,
150180
DF8DFFCC1C61DA1B00174DA9 /* Frameworks */,
151181
DF8DFFCD1C61DA1B00174DA9 /* Resources */,
182+
C6B07ACD4B0204ECA7C9F9D1 /* Embed Pods Frameworks */,
183+
01D7D266B699C1B26832D32F /* Copy Pods Resources */,
152184
);
153185
buildRules = (
154186
);
@@ -222,11 +254,60 @@
222254
};
223255
/* End PBXResourcesBuildPhase section */
224256

257+
/* Begin PBXShellScriptBuildPhase section */
258+
01D7D266B699C1B26832D32F /* Copy Pods Resources */ = {
259+
isa = PBXShellScriptBuildPhase;
260+
buildActionMask = 2147483647;
261+
files = (
262+
);
263+
inputPaths = (
264+
);
265+
name = "Copy Pods Resources";
266+
outputPaths = (
267+
);
268+
runOnlyForDeploymentPostprocessing = 0;
269+
shellPath = /bin/sh;
270+
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests-resources.sh\"\n";
271+
showEnvVarsInLog = 0;
272+
};
273+
8E2B56729DA1DBF78DC0ECCD /* Check Pods Manifest.lock */ = {
274+
isa = PBXShellScriptBuildPhase;
275+
buildActionMask = 2147483647;
276+
files = (
277+
);
278+
inputPaths = (
279+
);
280+
name = "Check Pods Manifest.lock";
281+
outputPaths = (
282+
);
283+
runOnlyForDeploymentPostprocessing = 0;
284+
shellPath = /bin/sh;
285+
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
286+
showEnvVarsInLog = 0;
287+
};
288+
C6B07ACD4B0204ECA7C9F9D1 /* Embed Pods Frameworks */ = {
289+
isa = PBXShellScriptBuildPhase;
290+
buildActionMask = 2147483647;
291+
files = (
292+
);
293+
inputPaths = (
294+
);
295+
name = "Embed Pods Frameworks";
296+
outputPaths = (
297+
);
298+
runOnlyForDeploymentPostprocessing = 0;
299+
shellPath = /bin/sh;
300+
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests-frameworks.sh\"\n";
301+
showEnvVarsInLog = 0;
302+
};
303+
/* End PBXShellScriptBuildPhase section */
304+
225305
/* Begin PBXSourcesBuildPhase section */
226306
DF8DFFCB1C61DA1B00174DA9 /* Sources */ = {
227307
isa = PBXSourcesBuildPhase;
228308
buildActionMask = 2147483647;
229309
files = (
310+
DF8DFFDC1C61E6B200174DA9 /* QNTestLogger.m in Sources */,
230311
DF8DFFD21C61DA1B00174DA9 /* TcpPingTest.m in Sources */,
231312
);
232313
runOnlyForDeploymentPostprocessing = 0;
@@ -259,6 +340,7 @@
259340
/* Begin XCBuildConfiguration section */
260341
DF8DFFD81C61DA1B00174DA9 /* Debug */ = {
261342
isa = XCBuildConfiguration;
343+
baseConfigurationReference = 32AB6E052E20B19230FF87D1 /* Pods-NetDiagTests.debug.xcconfig */;
262344
buildSettings = {
263345
INFOPLIST_FILE = NetDiagTests/Info.plist;
264346
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
@@ -269,6 +351,7 @@
269351
};
270352
DF8DFFD91C61DA1B00174DA9 /* Release */ = {
271353
isa = XCBuildConfiguration;
354+
baseConfigurationReference = E7FCF14D691EF09CEBD2808D /* Pods-NetDiagTests.release.xcconfig */;
272355
buildSettings = {
273356
INFOPLIST_FILE = NetDiagTests/Info.plist;
274357
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
@@ -385,6 +468,7 @@
385468
DF8DFFD91C61DA1B00174DA9 /* Release */,
386469
);
387470
defaultConfigurationIsVisible = 0;
471+
defaultConfigurationName = Release;
388472
};
389473
DFCEC6401C2EC01D0058EE65 /* Build configuration list for PBXProject "NetDiag" */ = {
390474
isa = XCConfigurationList;

NetDiag.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NetDiag/QNNTcpPing.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
@interface QNNTcpPingResult : NSObject
1313

1414
@property (readonly) NSInteger code;
15-
@property (readonly) NSInteger maxRtt;
16-
@property (readonly) NSInteger minRtt;
17-
@property (readonly) NSInteger avgRtt;
15+
@property (readonly) NSTimeInterval maxRtt;
16+
@property (readonly) NSTimeInterval minRtt;
17+
@property (readonly) NSTimeInterval avgRtt;
1818
@property (readonly) NSInteger count;
1919

2020
-(NSString*) description;

NetDiag/QNNTcpPing.m

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@
1818

1919
#import "QNNTcpPing.h"
2020

21+
@interface QNNTcpPingResult()
22+
23+
-(instancetype)init:(NSInteger)code
24+
max:(NSTimeInterval)maxRtt
25+
min:(NSTimeInterval)minRtt
26+
avg:(NSTimeInterval)avgRtt
27+
count:(NSInteger)count;
28+
@end
29+
30+
@implementation QNNTcpPingResult
31+
32+
33+
-(NSString*) description{
34+
if (_code == 0) {
35+
return [NSString stringWithFormat:@"tcp connect min/avg/max = %f/%f/%fms", _minRtt, _avgRtt, _maxRtt];
36+
}
37+
return [NSString stringWithFormat:@"tcp connect failed %d", _code];
38+
}
39+
40+
-(instancetype)init:(NSInteger)code
41+
max:(NSTimeInterval)maxRtt
42+
min:(NSTimeInterval)minRtt
43+
avg:(NSTimeInterval)avgRtt
44+
count:(NSInteger)count{
45+
if (self = [super init]) {
46+
_code = code;
47+
_minRtt = minRtt;
48+
_avgRtt = avgRtt;
49+
_count = count;
50+
}
51+
return self;
52+
}
53+
54+
@end
55+
2156
@interface QNNTcpPing ()
2257

2358
@property (readonly) NSString* host;
@@ -62,7 +97,7 @@ -(void) run{
6297
[self.output write:@"Problem accessing the DNS"];
6398
if (_complete != nil) {
6499
dispatch_async(dispatch_get_main_queue(), ^(void) {
65-
_complete(nil);
100+
_complete([self buildResult:-1006 durations:nil count:0]);
66101
});
67102
}
68103
return;
@@ -85,13 +120,33 @@ -(void) run{
85120
} while (++index < _count && !_stopped);
86121
if (_complete) {
87122
dispatch_async(dispatch_get_main_queue(), ^(void) {
88-
_complete(nil);
123+
_complete([self buildResult:0 durations:intervals count:index]);
89124
});
90125
}
91126
free(intervals);
92127
}
93128

94-
129+
-(QNNTcpPingResult*)buildResult:(NSInteger)code
130+
durations:(NSTimeInterval*)durations
131+
count:(NSInteger)count{
132+
if (code < 0) {
133+
return [[QNNTcpPingResult alloc] init:code max:0 min:0 avg:0 count:1];
134+
}
135+
NSTimeInterval max = 0;
136+
NSTimeInterval min = 10000000;
137+
NSTimeInterval sum = 0;
138+
for (int i = 0; i<count; i++) {
139+
if (durations[i]>max) {
140+
max = durations[i];
141+
}
142+
if (durations[i]<min) {
143+
min = durations[i];
144+
}
145+
sum += durations[i];
146+
}
147+
NSTimeInterval avg = sum/count;
148+
return [[QNNTcpPingResult alloc]init:0 max:max min:min avg:avg count:count];
149+
}
95150

96151
-(NSInteger) connect:(struct sockaddr_in*) addr{
97152
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -134,10 +189,7 @@ +(instancetype) start:(NSString*)host
134189
output:output
135190
complete:complete
136191
count:count];
137-
138-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void) {
139-
[t run];
140-
});
192+
[t run];
141193
return t;
142194
}
143195

NetDiagTests/QNTestLogger.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// QNTestLogger.h
3+
// NetDiag
4+
//
5+
// Created by bailong on 16/2/3.
6+
// Copyright © 2016年 Qiniu Cloud Storage. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
#import "QNNProtocols.h"
12+
13+
@interface QNTestLogger : NSObject<QNNOutputDelegate>
14+
15+
@end

NetDiagTests/QNTestLogger.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// QNTestLogger.m
3+
// NetDiag
4+
//
5+
// Created by bailong on 16/2/3.
6+
// Copyright © 2016年 Qiniu Cloud Storage. All rights reserved.
7+
//
8+
9+
#import "QNTestLogger.h"
10+
11+
@implementation QNTestLogger
12+
13+
-(void) write:(NSString*)line{
14+
NSLog(line);
15+
}
16+
17+
@end

NetDiagTests/TcpPingTest.m

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#import <XCTest/XCTest.h>
1010

11+
#import <AGAsyncTestHelper.h>
12+
13+
#import "QNNTcpPing.h"
14+
#import "QNTestLogger.h"
15+
1116
@interface NetDiagTests : XCTestCase
1217

1318
@end
@@ -25,16 +30,26 @@ - (void)tearDown {
2530
}
2631

2732
- (void)testFail {
28-
// This is an example of a functional test case.
29-
// Use XCTAssert and related functions to verify your tests produce the correct results.
3033
}
3134

3235
- (void)testTimeout{
3336

3437
}
3538

39+
-(void)testStop{
40+
41+
}
42+
3643
- (void)testOK{
44+
__block BOOL run = NO;
45+
id<QNNStopDelegate> h = [QNNTcpPing start:@"www.baidu.com" output:[[QNTestLogger alloc]init] complete:^(QNNTcpPingResult * r) {
46+
XCTAssertNotNil(r, @"need result");
47+
XCTAssertEqual(0, r.code, @"normal code");
48+
run = YES;
49+
}];
3750

51+
AGWW_WAIT_WHILE(!run, 30.0);
52+
XCTAssert(run, @"PASS");
3853
}
3954

4055

0 commit comments

Comments
 (0)