管道的三种使用方案中,唯一正确而安全的使用方法
原创
©著作权归作者所有:来自51CTO博客作者拔山河的原创作品,请联系作者获取转载授权,否则将追究法律责任
系统自带的管道方法:
全局变量:
NSPipe * pipe = [NSPipe pipe] ;
NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ;
int pipeReadFd = [[pipe fileHandleForReading] fileDescriptor], fd];
int pipeWriteFd = [[pipe fileHandleForWriting] fileDescriptor], fd];
写管道处,注意这个函数是无返回的函数,无法判断管道异常: [self.pipeWriteFd writeData: [@”send” dataUsingEncoding: NSASCIIStringEncoding]]; 见我的文章管道,信号量,共享内存,socket的实际使用场景和NSPipe管道的使用
它的缺点也很显然,线程被杀,管道资源被回收时,你访问该管道,会出现管道破裂的系统级崩溃。详细见我的文章。
Signal 13 was raised(SIGPIPE管道破裂)
所以还时用系统级别的管道最安全,可以通过写管道是否可以写来判断管道是否可用。
这样使用申请管道也是错误的,因为你的线程申请的管道,在外部要可以用,肯定要能用,你申请个局部变量,等你离开线程时,你再使用管道,你就访问了空指针,造成管道破裂崩溃。下面是例子
int pipeSNO[2];
pipeSNO[0] = -1;
pipeSNO[1] = -1;
pipe(pipeSNO);
self.fdGuardModifyWaitTimeReadPipe = pipeSNO[0];
self.fdGuardModifyWaitTimeWritePipe = pipeSNO[1];
int set = 1;
setsockopt(self.fdGuardModifyWaitTimeReadPipe, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
int write = 1;
setsockopt(self.fdGuardModifyWaitTimeWritePipe, SOL_SOCKET, SO_NOSIGPIPE, (void *)&write, sizeof(int));
看来你向获得强大的功能,那么你就要从操作系统层做起,并且自己负责管道的申请和释放,不受系统ARC自动管理内存控制。下面是正确而强大的使用管道的部分例子,
申请管道部分:
if(self.fdGuardPipeHead != nil)
{
close(self.fdGuardModifyWaitTimeReadPipe);
close(self.fdGuardModifyWaitTimeWritePipe);
free(self.fdGuardPipeHead);
}
self.fdGuardPipeHead = malloc(2 * sizeof(int));
_fdGuardPipeHead[0] = -1;
_fdGuardPipeHead[1] = -1;
pipe(_fdGuardPipeHead);
self.fdGuardModifyWaitTimeReadPipe = _fdGuardPipeHead[0];
self.fdGuardModifyWaitTimeWritePipe = _fdGuardPipeHead[1];
int set = 1;
setsockopt(self.fdGuardModifyWaitTimeReadPipe, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
int write = 1;
setsockopt(self.fdGuardModifyWaitTimeWritePipe, SOL_SOCKET, SO_NOSIGPIPE, (void *)&write, sizeof(int));
[self creatSocketConnection];发送于异常判断
//通过长连接的监控管道发送char *格式的消息
-(BOOL)sendMessageBySocketMonitorPipeWithCharArr : (const char *)message
{
NSUInteger len = 0;
len = strlen(message);
if(0 == len)
{
return YES;
}
unsigned char input_msg[BUFFER_SIZE] = {0};
memcpy(input_msg, message, len);;
@try {
long ret = write(self.fdWritePipe, input_msg, len);
if(ret != len)
{
[self processSocketPipeClosed];
return NO;
}
else
{
return YES;
}
}
@catch (NSException *exception) {
FLDDLogInfo(@”exception:%@”, exception);
[self processSocketPipeClosed];
return NO;
}
@finally {
}
关闭管道的操作
-(void)processSocketPipeClosed
{
FLDDLogDebug(@”函数”);
if(self.socketThreadPipeHead != nil)
{
close(self.fdReadPipe);
close(self.fdWritePipe);
free(self.socketThreadPipeHead);
}
self.socketThreadPipeHead = malloc(2 * sizeof(int));
_socketThreadPipeHead[0] = -1;
_socketThreadPipeHead[1] = -1;
pipe(_socketThreadPipeHead);
self.fdReadPipe = _socketThreadPipeHead[0];
self.fdWritePipe = _socketThreadPipeHead[1];
int setPipe = 1;
setsockopt(self.fdReadPipe, SOL_SOCKET, SO_NOSIGPIPE, (void *)&setPipe, sizeof(int));
int writePipe = 1;
setsockopt(self.fdWritePipe, SOL_SOCKET, SO_NOSIGPIPE, (void *)&writePipe, sizeof(int));
if(![self sendMessageByPipeWithCharArr : g_guardThreadCheckIfNormal])
{
//守护线程的监控管道被系统关闭,需要建立守护线程时立刻重新建立长连接,防止无法通过监控管道控制守护线程
_connectTime = (long long)[[NSDate date] timeIntervalSince1970] - SOCKET_CONNECT_ABORT_WAIT_TIME - 1;
_currentSocketThreadDic = nil;
_socketThreadDic = nil;
if(self.fd > 0)
{
close(self.fd);
}
self.socketConnectStat = SOCKECT_CONNECT_ABNORMAL;
}
else
{
[self creatSocketConnection];
}
}