Ejemplo n.º 1
0
static void client_to_remote(struct ss_conn_ctx *conn)
{
	int readed;
	int ret;
	struct ss_server_ctx *server = conn->server_entry;
	struct buf *buf = server->buf;
	struct ss_remote_ctx *remote;

	if (conn->remote == NULL) {
		ss_server_del_conn(server, conn);
		return;
	}
	readed = server->ss_recv(conn->conn_fd, buf->data, buf->max,
				 MSG_DONTWAIT, conn);
	if (readed <= 0) {
		ss_server_del_conn(server, conn);
		return;
	}
	remote = conn->remote;
	ret = send(remote->remote_fd, buf->data, readed, 0);
	if (ret != readed) {
		debug_print("send return %d, should send %d: %s",
			    ret, readed, strerror(errno));
		if (ret == -1 && errno != EAGAIN)
			ss_server_del_conn(server, conn);
	}
}
Ejemplo n.º 2
0
/*
 * read from local
 */
static void ss_io_handle(void *conn, int fd, void *data, int mask)
{
	/* TODO */
	struct ss_conn_ctx *conn_ptr = conn;
	int ret;
	struct conn_info remote_info;
	struct io_event event = {
		.mask = AE_READABLE,
		.rfileproc = ss_remote_io_handle, /* server 可读 */
		.wfileproc = NULL,
		.para = NULL,
	};

	switch (conn_ptr->ss_conn_state) {
	case OPENING: /* reply */
		ret = ss_request_handle(conn_ptr, &remote_info);
		if (ret < 0)
			goto err;
		if (ss_conn_add_remote(conn_ptr, AE_READABLE,
				&remote_info, &event) == NULL) {
			debug_print("ss_conn_add_remote() failed: %s",
				    strerror(errno));
			goto err;
		}
		conn_ptr->ss_conn_state = CONNECTING;
		break;
	case CONNECTING: /* forwarding */
		client_to_remote(conn_ptr);
		break;
	default:
		debug_print("unknow status: %d", conn_ptr->ss_conn_state);
		goto err;
	}
	return;
err:
	debug_print("close");
	ss_server_del_conn(conn_ptr->server_entry, conn_ptr);
}
Ejemplo n.º 3
0
static void ss_io_handle(void *conn, int fd, void *data, int mask)
{
	struct ss_conn_ctx *conn_ptr = conn;
	struct conn_info remote_info;
	struct io_event event = {
		.mask = AE_READABLE,
		.rfileproc = ss_remote_io_handle, /* server 可读 */
		.wfileproc = NULL,
		.para = NULL,
	};

	strncpy(remote_info.ip, remote_ip, sizeof(remote_info.ip));
	remote_info.port = remote_port;
	switch (conn_ptr->ss_conn_state) {
	case OPENING:
		if (ss_handshake_handle(conn_ptr) < 0)
			return;
		if (ss_conn_add_remote(conn_ptr, AE_READABLE,
				&remote_info, &event) == NULL) {
			debug_print("ss_conn_add_remote() failed: %s",
				    strerror(errno));
			goto err;
		}
		conn_ptr->ss_conn_state = CONNECTING;
		break;
	case CONNECTING:
	case CONNECTED:
		/* TODO */
		client_to_remote(conn_ptr);
		break;
	default:
		debug_print("unknow state: %d", conn_ptr->ss_conn_state);
	}
	return;
err:
	debug_print("close");
	ss_server_del_conn(conn_ptr->server_entry, conn_ptr);
}