示例#1
0
文件: proxy.c 项目: fbbs/fbbs
static bool data_received_callback(int fd, int bytes)
{
	connection_t *conn = connections + fd;
	while (bytes > 0) {
		uchar_t buf[4096];
		int rc = file_read(fd, buf,
				bytes >= sizeof(buf) ? sizeof(buf) : bytes);
		if (rc <= 0) {
			return false;
		} else {
			int received = conn->received;
			conn->received += rc;
			if (received < PARCEL_SIZE_LENGTH) {
				for (int i = received; i < PARCEL_SIZE_LENGTH
						&& i < conn->received; ++i) {
					conn->length |= (buf[i - received] << (i * 8));
				}
			}

			if (conn->received >= PARCEL_SIZE_LENGTH
					&& conn->received > conn->length) {
				return false;
			}

			if (conn->client && conn->remote_fd == REMOTE_NULL) {
				conn->remote_fd = assign_server(fd);
			}

			int remain = conn->received < PARCEL_SIZE_LENGTH
					? -1 : conn->length - received;
			if (valid_remote_fd(fd)) {
				file_write(conn->remote_fd, buf,
						remain > 0 && remain < rc ? remain : rc);
			}

			if (conn->received >= PARCEL_SIZE_LENGTH
					&& conn->received == conn->length) {
				conn->received = 0;
				conn->length = 0;
				if (!conn->client && valid_remote_fd(fd)) {
					connections[conn->remote_fd].remote_fd = REMOTE_NULL;
					conn->remote_fd = REMOTE_NULL;
				}
				if (conn->client && conn->remote_fd == REMOTE_BUSY) {
					parcel_t parcel;
					parcel_new(&parcel);
					parcel_write_bool(&parcel, true);
					parcel_flush(&parcel, fd);
					parcel_free(&parcel);
				}
				return true;
			}
		}
		bytes -= rc;
	}
	return bytes == 0;
}
示例#2
0
int process_recover_credit(hpx_parcel_t *p) {
  hpx_addr_t process = p->pid;
  if (process == HPX_NULL) {
    return HPX_SUCCESS;
  }

  if (!p->credit) {
    return HPX_SUCCESS;
  }

  hpx_parcel_t *pp = parcel_new(process, _proc_return_credit, 0, 0, 0,
                                &p->credit, sizeof(p->credit));
  if (!pp) {
    dbg_error("parcel_recover_credit failed.\n");
  }
  pp->credit = 0;

  hpx_parcel_send_sync(pp);
  return HPX_SUCCESS;
}