acl/app/jaws/global/client_entry.c

146 lines
4.1 KiB
C
Raw Normal View History

#include "lib_acl.h"
#include "service.h"
#ifdef ACL_BCB_COMPILER
#pragma hdrstop
#endif
void client_entry_free(CLIENT_ENTRY *entry)
{
acl_myfree(entry);
}
/* <20><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD> */
static void onclose_client_stream(ACL_VSTREAM *stream, void *arg)
{
const char *myname = "onclose_client_stream";
CLIENT_ENTRY *entry = (CLIENT_ENTRY*) arg;
acl_msg_info("%s(%d), fd: %d, nrefer: %d, nrefer: %d, addr: %lx, %lx, domain(%s)",
__FUNCTION__, __LINE__, ACL_VSTREAM_SOCK(stream),
stream->nrefer, entry->nrefer,
(long) entry, (long) entry->client, entry->domain_key);
if (stream->nrefer != 0)
acl_msg_fatal("%s: stream->nrefer(%d) != 0", myname, stream->nrefer);
/* <20><>Ҫ<EFBFBD><D2AA>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>, <20>Է<EFBFBD><D4B7>ڵ<EFBFBD><DAB5><EFBFBD> acl_aio_iocp_close <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> */
entry->client = NULL;
if (entry->server)
acl_aio_iocp_close(entry->server);
/* <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> acl_aio_iocp_lose() <20><><EFBFBD><EFBFBD>Է<EFBFBD>ֹ entry <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͷ<EFBFBD> */
entry->nrefer--;
if (entry->nrefer > 0)
return;
if (entry->server != NULL)
acl_msg_fatal("%s(%d): server not null", myname, __LINE__);
entry->free_fn(entry);
}
CLIENT_ENTRY *client_entry_new(SERVICE *service, size_t size, ACL_ASTREAM *client)
{
const char *myname = "client_entry_new";
CLIENT_ENTRY *entry;
ACL_VSTREAM *stream;
if (size < sizeof(CLIENT_ENTRY))
acl_msg_fatal("%s(%d): size(%d) invalid", myname, __LINE__, size);
entry = (CLIENT_ENTRY* ) acl_mycalloc(1, size);
entry->service = service;
entry->client = client;
entry->nrefer++;
stream = acl_aio_vstream(client);
acl_vstream_add_close_handle(stream, onclose_client_stream, entry);
return (entry);
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD> */
static void onclose_server_stream(ACL_VSTREAM *stream, void *arg)
{
const char *myname = "onclose_server_stream";
CLIENT_ENTRY *entry = (CLIENT_ENTRY*) arg;
acl_msg_info("%s(%d), nrefer: %d", __FUNCTION__, __LINE__, stream->nrefer);
if (stream->nrefer != 0)
acl_msg_fatal("%s: stream->nrefer(%d) != 0", myname, stream->nrefer);
/* <20><>Ҫ<EFBFBD><D2AA>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>, <20>Է<EFBFBD><D4B7>ڵ<EFBFBD><DAB5><EFBFBD> acl_aio_iocp_close <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> */
entry->server = NULL;
if (entry->client) {
acl_aio_iocp_close(entry->client);
}
/* <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> acl_aio_iocp_lose() <20><><EFBFBD><EFBFBD>Է<EFBFBD>ֹ entry <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͷ<EFBFBD> */
entry->nrefer--;
if (entry->nrefer > 0)
return;
if (entry->client != NULL)
acl_msg_fatal("%s(%d): client not null", myname, __LINE__);
entry->free_fn(entry);
}
void client_entry_set_server(CLIENT_ENTRY *entry, ACL_ASTREAM *server)
{
const char *myname = "client_entry_set_server";
ACL_VSTREAM *stream;
if (entry->server != NULL)
acl_msg_fatal("%s(%d): entry->server not null", myname, __LINE__);
entry->server = server;
entry->nrefer++;
stream = acl_aio_vstream(server);
acl_vstream_add_close_handle(stream, onclose_server_stream, entry);
}
int client_entry_detach(CLIENT_ENTRY *entry, ACL_VSTREAM *stream)
{
return (client_entry_detach3(entry, stream, 1));
}
int client_entry_detach3(CLIENT_ENTRY *entry, ACL_VSTREAM *stream, int auto_free)
{
const char *myname = "client_entry_detach";
/* <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ڹر<DAB9><D8B1><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD> */
if (entry->client && acl_aio_vstream(entry->client) == stream) {
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ص<EFBFBD> */
acl_vstream_delete_close_handle(stream, onclose_client_stream, entry);
entry->client = NULL;
entry->nrefer--;
} else if (entry->server && acl_aio_vstream(entry->server) == stream) {
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ص<EFBFBD> */
acl_vstream_delete_close_handle(stream, onclose_server_stream, entry);
entry->server = NULL;
entry->nrefer--;
} else {
acl_msg_fatal("%s(%d): unknown stream", myname, __LINE__);
}
/* <20><><EFBFBD><EFBFBD> entry <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0<CEAA><30><EFBFBD><EFBFBD>Ҫ<EFBFBD>ͷŵ<CDB7><C5B5>ö<EFBFBD><C3B6><EFBFBD> */
if (entry->nrefer == 0) {
if (auto_free)
entry->free_fn(entry);
/* <20><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>ߣ<EFBFBD> entry <20><>Դ<EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>ͷţ<CDB7><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> */
return (1);
} else if (entry->client == NULL && entry->server == NULL) {
acl_msg_warn("%s(%d): nrefer=%d, client %s, server %s\n",
myname, __LINE__, entry->nrefer,
entry->client ? "not null" : "null",
entry->server ? "not null" : "null");
}
return (0);
}