|
|
|
@ -203,7 +203,7 @@ int _sntll_writemsg(conn_t *co, sxmsg_t *msg)
|
|
|
|
|
if(head->payload_length && !msg->payload) return SNE_FAILED;
|
|
|
|
|
|
|
|
|
|
/* write the head and payload if applicable */
|
|
|
|
|
pthread_mutex_lock(&co->sslinout[2]);
|
|
|
|
|
pthread_mutex_lock(&co->sslinout[1]);
|
|
|
|
|
rd = __conn_write(co, head, sizeof(sntllv2_head_t));
|
|
|
|
|
if(rd < 0) {
|
|
|
|
|
co->flags |= SNSX_CLOSED;
|
|
|
|
@ -213,7 +213,7 @@ int _sntll_writemsg(conn_t *co, sxmsg_t *msg)
|
|
|
|
|
/* check up again */
|
|
|
|
|
if(rd < 0) { co->flags |= SNSX_CLOSED; r = SNE_ESSL; }
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(&co->sslinout[2]);
|
|
|
|
|
pthread_mutex_unlock(&co->sslinout[1]);
|
|
|
|
|
|
|
|
|
|
if(!(co->flags & SNSX_CLOSED)) r = SNE_SUCCESS;
|
|
|
|
|
|
|
|
|
@ -359,6 +359,8 @@ static int __connection_second_alloc(conn_t *co)
|
|
|
|
|
{
|
|
|
|
|
usrtc_node_init(&co->csnode, co);
|
|
|
|
|
|
|
|
|
|
memset(&co->idx_ch, 0, sizeof(idx_allocator_t));
|
|
|
|
|
memset(&co->idx_msg, 0, sizeof(idx_allocator_t));
|
|
|
|
|
if((idx_allocator_init(&co->idx_ch, 512, 0))) goto __fail;
|
|
|
|
|
if((idx_allocator_init(&co->idx_msg, 1024, 0))) goto __fail;
|
|
|
|
|
|
|
|
|
@ -447,6 +449,7 @@ static void *__sntll_thread(void *b)
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
|
int dispatch = 0;
|
|
|
|
|
size_t rd, wr;
|
|
|
|
|
ulong_t mid;
|
|
|
|
|
|
|
|
|
|
/* byte buffer is following head */
|
|
|
|
|
bbuf += sizeof(sntllv2_head_t);
|
|
|
|
@ -472,8 +475,15 @@ static void *__sntll_thread(void *b)
|
|
|
|
|
while(1) {
|
|
|
|
|
__again:
|
|
|
|
|
pthread_mutex_lock(&(co->sslinout[0]));
|
|
|
|
|
if(co->flags & SNSX_CLOSED) {
|
|
|
|
|
pthread_mutex_unlock(&(co->sslinout[0]));
|
|
|
|
|
goto __finish;
|
|
|
|
|
}
|
|
|
|
|
rd = __conn_read(co, mhead, sizeof(sntllv2_head_t));
|
|
|
|
|
if(rd != sizeof(sntllv2_bundle_t)) {
|
|
|
|
|
#ifdef _VERBOSE_DEBUG
|
|
|
|
|
dumphead(mhead);
|
|
|
|
|
#endif
|
|
|
|
|
if(rd != sizeof(sntllv2_head_t)) {
|
|
|
|
|
__sslproto_error:
|
|
|
|
|
co->flags |= SNSX_CLOSED;
|
|
|
|
|
pthread_mutex_unlock(&(co->sslinout[0]));
|
|
|
|
@ -530,7 +540,8 @@ static void *__sntll_thread(void *b)
|
|
|
|
|
fprintf(stderr, "[sntllv2] Invalid index of the message.\n");
|
|
|
|
|
goto __again;
|
|
|
|
|
}
|
|
|
|
|
msg = co->messages[mhead->msgid];
|
|
|
|
|
mid = mhead->msgid;
|
|
|
|
|
msg = co->messages[mid];
|
|
|
|
|
if(!msg) goto __inval_idx_nor;
|
|
|
|
|
|
|
|
|
|
/* ok now we'are copy data and unlock wait mutex */
|
|
|
|
@ -580,10 +591,11 @@ static void *__sntll_thread(void *b)
|
|
|
|
|
if(mhead->payload_length) msg->payload = bbuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&co->idx_ch_lock);
|
|
|
|
|
idx_reserve(&co->idx_ch, mhead->msgid);
|
|
|
|
|
mid = mhead->msgid;
|
|
|
|
|
pthread_mutex_lock(&co->idx_msg_lock);
|
|
|
|
|
idx_reserve(&co->idx_msg, mid);
|
|
|
|
|
co->messages[mhead->msgid] = msg;
|
|
|
|
|
pthread_mutex_unlock(&co->idx_ch_lock);
|
|
|
|
|
pthread_mutex_unlock(&co->idx_msg_lock);
|
|
|
|
|
|
|
|
|
|
/* now we are able to process the message */
|
|
|
|
|
_message_process(msg);
|
|
|
|
@ -594,10 +606,10 @@ static void *__sntll_thread(void *b)
|
|
|
|
|
if(!msg) goto __inval_idx_nor;
|
|
|
|
|
|
|
|
|
|
/* message dialog is closed - remove this right now */
|
|
|
|
|
pthread_mutex_lock(&co->idx_ch_lock);
|
|
|
|
|
idx_free(&co->idx_ch, mhead->msgid);
|
|
|
|
|
pthread_mutex_lock(&co->idx_msg_lock);
|
|
|
|
|
idx_free(&co->idx_msg, mhead->msgid);
|
|
|
|
|
co->messages[mhead->msgid] = NULL;
|
|
|
|
|
pthread_mutex_unlock(&co->idx_ch_lock);
|
|
|
|
|
pthread_mutex_unlock(&co->idx_msg_lock);
|
|
|
|
|
|
|
|
|
|
if(msg->mhead.attr & SXMSG_TIMEDOUT) { /* nobody wait for it */
|
|
|
|
|
/* now just free it */
|
|
|
|
@ -622,10 +634,10 @@ static void *__sntll_thread(void *b)
|
|
|
|
|
if(!msg) goto __inval_idx_nor;
|
|
|
|
|
|
|
|
|
|
if(msg->mhead.attr & SXMSG_TIMEDOUT) { /* nobody wait for it */
|
|
|
|
|
pthread_mutex_lock(&co->idx_ch_lock);
|
|
|
|
|
idx_free(&co->idx_ch, mhead->msgid);
|
|
|
|
|
pthread_mutex_lock(&co->idx_msg_lock);
|
|
|
|
|
idx_free(&co->idx_msg, mhead->msgid);
|
|
|
|
|
co->messages[mhead->msgid] = NULL;
|
|
|
|
|
pthread_mutex_unlock(&co->idx_ch_lock);
|
|
|
|
|
pthread_mutex_unlock(&co->idx_msg_lock);
|
|
|
|
|
|
|
|
|
|
/* now just free it */
|
|
|
|
|
pthread_mutex_destroy(&msg->wait);
|
|
|
|
@ -775,7 +787,7 @@ conn_t *connection_master_link(conn_sys_t *ssys, int sck, struct in_addr *addr)
|
|
|
|
|
head->opcode = r;
|
|
|
|
|
if(r != SNE_SUCCESS) { /* we finish */
|
|
|
|
|
head->payload_length = 0;
|
|
|
|
|
__conn_write(co, buf, sizeof(sntllv2_head_t));
|
|
|
|
|
__conn_write(co, head, sizeof(sntllv2_head_t));
|
|
|
|
|
destroy_sexp(sx);
|
|
|
|
|
goto __fail3;
|
|
|
|
|
}
|
|
|
|
@ -793,6 +805,10 @@ conn_t *connection_master_link(conn_sys_t *ssys, int sck, struct in_addr *addr)
|
|
|
|
|
r = __connection_second_alloc(co);
|
|
|
|
|
if(r != SNE_SUCCESS) goto __fail3;
|
|
|
|
|
|
|
|
|
|
/* free message */
|
|
|
|
|
co->messages[0] = NULL;
|
|
|
|
|
free(msg);
|
|
|
|
|
|
|
|
|
|
/* and now we're need to create a thread poll */
|
|
|
|
|
if(!(bundle = malloc(sizeof(sntllv2_bundle_t)))) { r = SNE_ENOMEM; goto __fail4; }
|
|
|
|
|
else {
|
|
|
|
@ -992,18 +1008,27 @@ conn_t *connection_link(conn_sys_t *ssys, const char *host,
|
|
|
|
|
sx = parse_sexp(bbuf, rd);
|
|
|
|
|
if(!sx) { r = SNE_BADPROTO; goto __fail2; }
|
|
|
|
|
r = __eval_syssexp(co, sx);
|
|
|
|
|
if(!r) r = SNE_SUCCESS;
|
|
|
|
|
destroy_sexp(sx);
|
|
|
|
|
|
|
|
|
|
/* write back */
|
|
|
|
|
head->opcode = r;
|
|
|
|
|
head->payload_length = 0;
|
|
|
|
|
wr = __conn_write(co, head, sizeof(sntllv2_head_t));
|
|
|
|
|
if(wr < 0) goto __fail2;
|
|
|
|
|
if(r != SNE_SUCCESS) goto __fail2;
|
|
|
|
|
if(wr < 0) {
|
|
|
|
|
blub("fuck");
|
|
|
|
|
r = SNE_LINKERROR; goto __fail2;}
|
|
|
|
|
if(r != SNE_SUCCESS) { r = SNE_LINKERROR; goto __fail2;}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* if we're there - negotiation is done, going to init messaging mode */
|
|
|
|
|
r = __connection_second_alloc(co);
|
|
|
|
|
if(r != SNE_SUCCESS) goto __fail3;
|
|
|
|
|
|
|
|
|
|
/* free message */
|
|
|
|
|
co->messages[0] = NULL;
|
|
|
|
|
free(msg);
|
|
|
|
|
|
|
|
|
|
/* and now we're need to create a thread poll */
|
|
|
|
|
if(!(bundle = malloc(sizeof(sntllv2_bundle_t)))) { r = SNE_ENOMEM; goto __fail4; }
|
|
|
|
|
else {
|
|
|
|
|