Exemplo n.º 1
0
	virtual void* run()
	{
		// 回写需要先设置远程连接地址
		acl::socket_stream* conn = (acl::socket_stream*)
			acl_pthread_getspecific(stream_key);
		acl_assert(conn);
		conn->set_peer(peer_addr_);
		conn->write(buf_, len_);

		// 因为该对象是动态分配的,所以需要释放掉
		delete this;
		return NULL;
	}
Exemplo n.º 2
0
redis_result::redis_result(dbuf_pool* pool)
: result_type_(REDIS_RESULT_NIL)
, pool_(pool)
, size_(0)
, idx_(0)
, argv_(NULL)
, lens_(NULL)
, children_(NULL)
, children_size_(10)
, children_idx_(0)
{
	acl_assert(pool_ != NULL);
}
Exemplo n.º 3
0
void aio_listen_stream::hook_accept()
{
	acl_assert(stream_);
	if (accept_hooked_)
		return;
	accept_hooked_ = true;

	acl_aio_ctl(stream_,
		ACL_AIO_CTL_ACCEPT_FN, accept_callback,
		ACL_AIO_CTL_CTX, this,
		ACL_AIO_CTL_END);
	acl_aio_accept(stream_);
}
Exemplo n.º 4
0
bool ipc_client::open(const char* addr, int timeout)
{
	acl_assert(sync_stream_ == NULL && async_stream_ == NULL);
	sync_stream_ = NEW socket_stream();
	if (sync_stream_->open(addr, timeout, 0) == false)
	{
		delete sync_stream_;
		sync_stream_ = NULL;
		return (false);
	}
	sync_stream_inner_ = sync_stream_;
	return (true);
}
Exemplo n.º 5
0
ACL_XML_NODE *acl_xml_node_next(ACL_XML_NODE *node)
{
    ACL_RING *ring_ptr = acl_ring_succ(&node->node);
    ACL_XML_NODE *parent;

    if (ring_ptr == &node->node)
        return (NULL);
    parent = node->parent;
    acl_assert(parent != NULL);
    if (ring_ptr == &parent->children)
        return (NULL);
    return (acl_ring_to_appl(ring_ptr, ACL_XML_NODE, node));
}
Exemplo n.º 6
0
memcache_session::memcache_session(const char* cache_addr,
	int conn_timeout /* = 180 */, int rw_timeout /* = 300 */,
	const char* prefix /* = NULL */, time_t ttl /* = 0 */, 
	const char* sid /* = NULL */, bool encode_key /* = true */)
: session(ttl, sid)
, auto_free_(true)
{
	acl_assert(cache_addr && *cache_addr);
	cache_ = NEW memcache(cache_addr, conn_timeout, rw_timeout);
	(*cache_).set_prefix(prefix && *prefix ? prefix : "_")
		.encode_key(encode_key)
		.auto_retry(true);
}
Exemplo n.º 7
0
xml_node* xml2_node::next_child(void)
{
	acl_assert(child_iter_);

	ACL_XML2_NODE* node = node_->iter_next(child_iter_, node_);
	if (node == NULL)
		return NULL;

	xml2_node* n = NEW xml2_node(xml_, node);
	nodes_tmp_.push_back(n);

	return n;
}
Exemplo n.º 8
0
int stream::read_hook(ACL_SOCKET, void *buf, size_t len, int,
	ACL_VSTREAM* vs, void *)
{
	stream* s = (stream*) acl_vstream_get_object(vs, HOOK_KEY);
	acl_assert(s);

	if (s->hook_ == NULL)
	{
		logger_error("hook_ null");
		return -1;
	}
	return s->hook_->read(buf, len);
}
Exemplo n.º 9
0
int master_threads2::service_on_handshake(ACL_VSTREAM *client)
{
	acl_assert(__mt != NULL);

	// client->context 在 service_on_accept 中被设置
	socket_stream* stream = (socket_stream*) client->context;
	if (stream == NULL)
		logger_fatal("client->context is null!");

	if (__mt->thread_on_handshake(stream) == true)
		return 0;
	return -1;
}
Exemplo n.º 10
0
static void handles_init(void)
{
	const char *myname = "handles_init";
	int   i;

	__handles = (HANDLE *) acl_mycalloc(__max_handle, sizeof(HANDLE));
	acl_assert(__handles);
	__cur_handle = 0;

	for (i = 0; i < __max_handle; i++) {
		__handles[i] = INVALID_HANDLE_VALUE;
	}
}
Exemplo n.º 11
0
int stream::fsend_hook(ACL_FILE_HANDLE, const void *buf, size_t len, int,
	ACL_VSTREAM* vs, void *)
{
	stream* s = (stream*) acl_vstream_get_object(vs, HOOK_KEY);
	acl_assert(s);

	if (s->hook_ == NULL)
	{
		logger_error("hook_ null");
		return -1;
	}
	return s->hook_->send(buf, len);
}
Exemplo n.º 12
0
hspool::hspool(const char* addr_rw, const char* addr_rd,
	bool cache_enable /* = true */, bool retry_enable /* = true */)
: cache_enable_(cache_enable)
, retry_enable_(retry_enable)
{
	acl_assert(addr_rw);
	addr_rw_ = acl_mystrdup(addr_rw);
	if (addr_rd != NULL)
		addr_rd_ = acl_mystrdup(addr_rd);
	else
		addr_rd_ = addr_rw_;
	locker_ = NEW locker(true);
}
Exemplo n.º 13
0
	void read_respond_body(HWND hWnd)
	{
		acl_assert(hdr_res_);

		/* 如果 HTTP 响应没有数据体则仅输出 HTTP 响应头即可 */

		if (hdr_res_->hdr.content_length == 0
			|| (hdr_res_->hdr.content_length == -1
			&& hdr_res_->reply_status > 300
			&& hdr_res_->reply_status < 400))
		{
			data_.i_ptr = NULL;
			data_.i_dlen = 0;
			// 如果没有数据体,也发送消息通知调用者数据结束
			report(hWnd, HTTP_MSG_DAT);
			return;
		}

		res_ = http_res_new(hdr_res_);

#define BUF_LEN		8192

		char* buf;
		int   ret;

		while (true)
		{
			buf = (char*) acl_mymalloc(BUF_LEN);
			ret = read_respond_body(buf, BUF_LEN - 1);
			if (ret <= 0)
			{
				acl_myfree(buf);
				break;
			}
			buf[ret] = 0;

			data_.i_ptr = buf;
			data_.i_dlen = (size_t) ret;
			report(hWnd, HTTP_MSG_DAT);

			if (respond_over_)
				break;
			if (nwait_ > 0)
				acl_doze(nwait_);
		}

		data_.i_ptr = NULL;
		data_.i_dlen = 0;
		report(hWnd, HTTP_MSG_DAT);
	}
Exemplo n.º 14
0
void master_trigger::service_pre_jail(char*, char**)
{
	acl_assert(__mt != NULL);

#ifndef WIN32
	if (__mt->daemon_mode())
	{
		ACL_EVENT* eventp = acl_trigger_server_event();
		__mt->set_event(eventp);  // 设置基类的事件引擎句柄
	}
#endif

	__mt->proc_pre_jail();
}
Exemplo n.º 15
0
void master_proc::service_pre_jail(char*, char**)
{
	acl_assert(__mp != NULL);

#ifndef ACL_WINDOWS
	if (__mp->daemon_mode())
	{
		ACL_EVENT* eventp = acl_single_server_event();
		__mp->set_event(eventp);
	}
#endif

	__mp->proc_pre_jail();
}
Exemplo n.º 16
0
bool HttpServletResponse::sendHeader(void)
{
    acl_assert(header_->is_request() == false);
    string buf;
    if (charset_[0] != 0)
        buf.format("%s; charset=%s", content_type_, charset_);
    else
        buf.format("%s", content_type_);
    header_->set_content_type(buf.c_str());

    buf.clear();
    header_->build_response(buf);
    return getOutputStream().write(buf) == -1 ? false : true;
}
Exemplo n.º 17
0
void master_fiber::service_main(ACL_VSTREAM *client, void*)
{
	acl_assert(__mf != NULL);

	socket_stream stream;
	if (stream.open(client) == false)
	{
		logger_error("open stream error(%s)", acl_last_serror());
		return;
	}

	__mf->on_accept(stream);
	stream.unbind();
}
Exemplo n.º 18
0
void status_manager::del_status(const char* key)
{
	acl_assert(key && *key);
	std::map<acl::string, server_status*>::iterator it =
		servers_status_.find(key);
	if (it == servers_status_.end())
	{
		logger_warn("key: %s not found", key);
		return;
	}

	delete it->second;
	servers_status_.erase(it);
}
Exemplo n.º 19
0
ACL_VSTREAM *private_vstream_fhopen(ACL_FILE_HANDLE fh, unsigned int oflags)
{
	ACL_VSTREAM *fp;

	acl_assert(fh != ACL_FILE_INVALID);

	fp = private_vstream_fdopen(ACL_SOCKET_INVALID, oflags,
		4096, 0, ACL_VSTREAM_TYPE_FILE);
	if (fp == NULL)
		return (NULL);

	fp->fd.h_file = fh;
	return (fp);
}
Exemplo n.º 20
0
void manager::init(ACL_EVENT* event, const char* addr, int rw_timeout)
{
	acl_assert(event);

	if (addr == NULL || *addr == 0)
		return;

	event_  = event;
	aio_    = acl_aio_create3(event);
	handle_ = new acl::aio_handle(aio_);
	server_ = new http_server(*handle_, rw_timeout);

	server_->open(addr);
}
Exemplo n.º 21
0
static void slice3_mbuf_alloc(ACL_SLICE *slice)
{
#ifdef	_LP64
	const char *myname = "slice3_mbuf_alloc";
#endif
	SLICE3 *slice3 = (SLICE3*) slice;
	MBUF3 *mbuf;
	int   i, incr_real = 0, n;
	char *ptr;

	mbuf = (MBUF3*) acl_default_malloc(__FILE__, __LINE__,
			slice->page_size);
	mbuf->mbuf.slice = slice;
	mbuf->mbuf.nused = 0;
	mbuf->mbuf.signature = SIGNATURE;
	ptr = mbuf->payload;

	slice->nalloc++;
	mbuf->mslots.slots = NULL;

	n = slice->page_nslots;
	MBUF_SLOTS_SPACE(slice, &mbuf->mslots, n, incr_real);
	acl_assert(mbuf->mslots.islots == 0);

	for (i = 0; i < slice->page_nslots; i++) {
		ptr += SLICE3_HEAD_SIZE;
#ifdef	_LP64
		if ((slice->flag & ACL_SLICE_FLAG_LP64_ALIGN)
			&& ((uintptr_t)ptr & 0x7) != 0)  /* just for AVL */
		{
			acl_msg_fatal("%s(%d): %s, ptr(%lx) invalid",
				myname, __LINE__, slice->name, (long) ptr);
		}
#endif
		*((int*) (ptr - SLICE_OFF_SIZE)) = (int) (ptr - (char*) mbuf);
		mbuf->mslots.slots[mbuf->mslots.islots++] = ptr;
		ptr += slice->slice_length;
	}

	for (i = slice->page_nslots; i < incr_real; i++)
		mbuf->mslots.slots[i] = NULL;

	SLICE_MBUF_SPACE(slice3, 1, incr_real, MBUF3);
	for (i = slice->nbuf; i < slice3->capacity; i++)
		slice3->mbufs[i] = NULL;
	slice3->mbufs[slice->nbuf] = mbuf;
	mbuf->ibuf = slice->nbuf;
	slice->nbuf++;
	slice->length += slice->page_size + sizeof(void*) * slice->page_nslots;
}
Exemplo n.º 22
0
void aio_ostream::hook_write()
{
	acl_assert(stream_);
	if (write_hooked_)
		return;
	write_hooked_ = true;

	/*
	acl_aio_ctl(stream_,
		ACL_AIO_CTL_WRITE_HOOK_ADD, write_callback, this,
		ACL_AIO_CTL_END);
	*/
	acl_aio_add_write_hook(stream_, write_callback, this);
}
Exemplo n.º 23
0
void master_threads::service_pre_jail(void*)
{
	acl_assert(__mt != NULL);

#ifndef WIN32
	if (__mt->daemon_mode())
	{
		ACL_EVENT* eventp = acl_ioctl_server_event();
		__mt->set_event(eventp);
	}
#endif

	__mt->proc_pre_jail();
}
Exemplo n.º 24
0
int acl_pthread_once(acl_pthread_once_t *once_control,
	void (*init_routine)(void))
{
	int   n = 0;

	if (once_control == NULL || init_routine == NULL) {
		acl_set_error(ACL_EINVAL);
		return ACL_EINVAL;
	}

	/* 只有第一个调用 InterlockedCompareExchange 的线程才会执行
	 * init_routine, 后续线程永远在 InterlockedCompareExchange
	 * 外运行,并且一直进入空循环直至第一个线程执行 init_routine
	 * 完毕并且将 *once_control 重新赋值, 只有在多核环境中多个线程
	 * 同时运行至此时才有可能出现短暂的后续线程空循环现象,如果
	 * 多个线程顺序至此,则因为 *once_control 已经被第一个线程重新
	 * 赋值而不会进入循环体内只所以如此处理,是为了保证所有线程在
	 * 调用 acl_pthread_once 返回前 init_routine 必须被调用且仅能
	 * 被调用一次, 但在VC6下,InterlockedCompareExchange 接口定义
	 * 有些怪异,需要做硬性指定参数类型,参见 <Windows 高级编程指南>
	 * Jeffrey Richter, 366 页
	 */
	while (1) {
#ifdef MS_VC6
		LONG prev = InterlockedCompareExchange((PVOID) once_control,
			(PVOID) 1, (PVOID) ACL_PTHREAD_ONCE_INIT);
#else
		LONG prev = InterlockedCompareExchange(
			once_control, 1, ACL_PTHREAD_ONCE_INIT);
#endif
		if (prev == 2)
			return 0;
		else if (prev == 0) {
			/* 只有第一个线程才会至此 */
			init_routine();
			/* 将 *conce_control 重新赋值以使后续线程不进入 while
			 * 循环或从 while 循环中跳出
			 */
			InterlockedExchange(once_control, 2);
			return 0;
		} else {
			acl_assert(prev == 1);

			/* 防止空循环过多地浪费CPU */
			Sleep(1);  /** sleep 1ms */
		}
	}
	return 1;  /* 不可达代码,避免编译器报警告 */
}
Exemplo n.º 25
0
int pipe_string::pop_end(string* out, size_t max /* = 0 */)
{
	if (out == NULL)
		return (0);
	size_t n = m_pBuf->length();
	acl_assert(n >= m_pos);
	n -= m_pos;
	if (n == 0)
		return (0);
	if (max > 0 && n > max)
		n = max;
	out->append(m_pBuf->c_str() + m_pos, n);
	m_pos += n;
	return (n);
}
Exemplo n.º 26
0
CMsnClient::CMsnClient(IMsnClient* callback, const char* account,
	const char* passwd, int timeout)
: callback_(callback)
, account_(account)
, passwd_(passwd)
, ds_client_(NULL)
, ns_client_(NULL)
, ticket_(NULL)
, timeout_(timeout)
, sid_(1)
{
	msn_service_ = NEW CMsnService();
	acl_assert(callback);
	acl::log::open("./msg_client.log", "test");
}
Exemplo n.º 27
0
memcache::memcache(const char* addr /* = "127.0.0.1:11211" */,
	int conn_timeout /* = 30 */, int rw_timeout /* = 30 */)
: keypre_(NULL)
, coder_(false, false)
, encode_key_(false)
, opened_(false)
, retry_(true)
, content_length_(0)
, length_(0)
, conn_(NULL)
{
	acl_assert(addr && *addr);
	addr_ = acl_mystrdup(addr);
	set_timeout(conn_timeout, rw_timeout);
}
Exemplo n.º 28
0
mysql_pool::mysql_pool(const char* dbaddr, const char* dbname,
                       const char* dbuser, const char* dbpass, int dblimit /* = 64 */,
                       unsigned long dbflags /* = 0 */, bool auto_commit /* = true */,
                       int conn_timeout /* = 60 */, int rw_timeout /* = 60 */,
                       const char* charset /* = "utf8" */)
    : db_pool(dbaddr, dblimit)
{
    acl_assert(dbaddr && *dbaddr);
    acl_assert(dbname && *dbname);

    conf_ = NEW mysql_conf(dbaddr, dbname);

    if (dbuser && *dbuser)
        conf_->set_dbuser(dbuser);
    if (dbpass && *dbpass)
        conf_->set_dbpass(dbpass);
    if (charset && *charset)
        conf_->set_charset(charset);
    conf_->set_dbflags(dbflags);
    conf_->set_dblimit(dblimit);
    conf_->set_auto_commit(auto_commit);
    conf_->set_conn_timeout(conn_timeout);
    conf_->set_rw_timeout(rw_timeout);
}
Exemplo n.º 29
0
void master_proc::service_main(ACL_VSTREAM *stream, char*, char**)
{
	socket_stream* client = NEW socket_stream();
	if (client->open(stream) == false)
		logger_fatal("open stream error!");

	acl_assert(__mp != NULL);
#ifndef	ACL_WINDOWS
	if (__mp->daemon_mode_)
		acl_watchdog_pat();  // 必须通知 acl_master 框架一下
#endif
	__mp->on_accept(client);
	client->unbind();
	delete client;
}
Exemplo n.º 30
0
void aio_istream::hook_read()
{
	acl_assert(stream_);
	if (read_hooked_)
		return;
	read_hooked_ = true;

	/*
	acl_aio_ctl(stream_,
		ACL_AIO_CTL_READ_HOOK_ADD, read_callback, this,
		ACL_AIO_CTL_CTX, this,
		ACL_AIO_CTL_END);
	*/
	acl_aio_add_read_hook(stream_, read_callback, this);
}