当前位置: 首页>>代码示例>>C++>>正文


C++ Alarm::SetAlarm方法代码示例

本文整理汇总了C++中Alarm::SetAlarm方法的典型用法代码示例。如果您正苦于以下问题:C++ Alarm::SetAlarm方法的具体用法?C++ Alarm::SetAlarm怎么用?C++ Alarm::SetAlarm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Alarm的用法示例。


在下文中一共展示了Alarm::SetAlarm方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: net_read_with_timeout

/* This does a blocking read for size amount of bytes. However, there is
	a timeout associated with it. This function is closely allied in
	implementation with _condor_full_read, but due to the alarm signal
	hilarity, it gets its own implementation here. The logic of this
	is a bit screwed up, but I had to fit it within the mindset of the
	ckpt server codebase, which is odd. The reason the logic is
	screwed up is because it is problematic to discover if the function
	failed due to timeout, or a different reason.
*/
read_result_t net_read_with_timeout(int fd, char *ptr, size_t nbytes,
	size_t *numread, int timeout)
{
    int nleft, nread;
	MyString log_msg;
	int save_errno;

	/* we use signal to implement breaking out of a permanently blocked 
		or very slow read situation */
	rt_alarm.SetAlarm(timeout);

    nleft = nbytes;
    while (nleft > 0) {
        REISSUE_READ: 
        nread = read(fd, ptr, nleft);
        if (nread < 0) {
			save_errno = errno;
            if (errno == EINTR) {
				/*	If the alarm is expired (we'll know because the SIGALARM
					handler tells the global rt_alarm object it is expired),
					then we timed out on the connection, otherwise
					ignore whatever signal it was and reissue the read.
				*/
				if (rt_alarm.IsExpired() == true) {

					rt_alarm.ResetAlarm();

					/* of course, we really don't know exactly how much we
						read, but that's ok, since we're closing the connection
						very soon at any rate. */
    				*numread = (nbytes - nleft);     

					return NET_READ_TIMEOUT;
				} 

				/* However, if it was some other kind of signal instead of
					the alarm, we'll be generous and resubmit the read with
					the same timeout again. This does make it possible for
					the timeout to never fire of the right frequency of non
					alarm signals happens, but this checkpoint server code
					is horrible and probably going away very soon. So we'll
					soak that small chance. */
				rt_alarm.SetAlarm(timeout);
                goto REISSUE_READ;
            }

            /* The caller has no idea how much was actually read in this
                scenario, but we know we aren't going to be reading anymore.
			*/
			rt_alarm.ResetAlarm();
			
			errno = save_errno;
			/* This represents confirmed bytes read, more could have been
				read off of the socket, however, and lost by the kernel when
				the error occured. */
			*numread = (nbytes - nleft);
            return NET_READ_FAIL;

        } else if (nread == 0) {
            /* We've reached the end of file marker, so stop looping. */
            break;
        }

		/* update counters */
        nleft -= nread;
        ptr = ((char *)ptr) + nread;
    }

	rt_alarm.ResetAlarm();

    /* return how much was actually read, which could include 0 in an
        EOF situation */
    *numread = (nbytes - nleft);     

	return NET_READ_OK;
}
开发者ID:bbockelm,项目名称:condor-network-accounting,代码行数:85,代码来源:protocol.cpp


注:本文中的Alarm::SetAlarm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。