void tcpsvr_notifyclose(tcpsvr_t * svr,tcpclient_t * cli) { struct rainMsg msg; msg.data = &cli->id; msg.sz = sizeof(cli->id); msg.type = CLOSE; rain_send(svr->ctx,svr->watchdog,msg,RAIN_COPY,NULL); tcpclient_destroy(cli); }
//static void _timercb(struct ev_loop * loop, ev_timer *w, int revents); void tcpsvr_delete(void *env,int code) { if( !env ) { return; } tcpsvr_t * svr = (tcpsvr_t *)env; int i=0; for(; i<TCPSVR_MAX_CONNECT; i++) { if( svr->clients[i].binuse ) { tcpclient_destroy(&svr->clients[i]); } } if(svr->fd >=0) { close(svr->fd); } wod_event_destroy(svr->loop); free(svr); }
static void _recv(void *env,rain_routine_t src,struct rainMsg msg,rain_session_t session) { tcpsvr_t * svr = (tcpsvr_t *)env; tcp_cmd_t * p = (tcp_cmd_t *)(msg.data); tcpclient_t *cli = tcpsvr_query(svr,p->id); if(cli) { if(p->cmd == CMD_SEND) { int ret = tcpclient_write(cli,NULL,0); if(ret < 0) { printf("send:error"); } } else if(p->cmd == CMD_CLOSE) { tcpclient_destroy(cli); } } else if(session != RAIN_INVALID_SESSION) { char buf[]="error_cmd"; struct rainMsg tmpmsg= {buf,sizeof(buf),-1}; rain_responce(svr->ctx,src,tmpmsg,RAIN_COPY,session); } free(msg.data); }
/* parsing lists {{{*/ if(service) { xmlNodePtr memberships = findNode(service->children, "Memberships", 1); xmlNodePtr ms; xmlNodePtr role; xmlNodePtr members, member; xmlNodePtr pname; xmlNodePtr type; xmlNodePtr lastchange; xmlChar *content; int flag = 0; lastchange = findNode(service->children, "LastChange", 1); content = xmlNodeGetContent(lastchange); cl->lastchange = strdup((char*)content); DMSG(stderr, "Contact: lastchange = %s\n", cl->lastchange); if(!memberships) { fprintf(stderr, "NULL membership\n"); count = 0; goto cleanup; } for(ms=memberships->children;ms;ms=ms->next) { int ctype = 1; if(!ms->children) continue; role = findNode(ms->children, "MemberRole", 1); if(!role) { fprintf(stderr, "Null role\n"); count = 0; goto cleanup; } members = findNode(role, "Members", 1); if(!members) continue; if(xmlStrEqual(role->children->content, (xmlChar*)"Allow")) flag = 3; else if(xmlStrEqual(role->children->content, (xmlChar*)"Block")) flag = 4; else continue; for(member=members->children;member;member=member->next) { Contact *c; type = findNode(member->children, "Type", 1); content = xmlNodeGetContent(type); if(!content) { fprintf(stderr, "NULL Type\n"); continue; } if(xmlStrEqual(content, (xmlChar*)"Passport")) { pname = findNode(member->children, "PassportName", 1); ctype = 1; } else if(xmlStrEqual(content, (xmlChar*)"Email")) { pname = findNode(member->children, "Email", 1); ctype = 32; } else continue; xmlFree(content); if(!pname) { fprintf(stderr, "NULL PassportName or Email\n"); continue; } content = xmlNodeGetContent(pname); if(content) { char name[32]; char domain[32]; if(sscanf((char*)content, "%[^@]@%s", name, domain) != 2) { fprintf(stderr, "parse contact: malformed email: %s\n", content); continue; } c = contact_new((char*)content); c->name = strdup(name); c->type = ctype; c->status = NA; c->inlist |= flag; c->domain = NULL; /* should be filled during sort */ cl_append_contact(cl, c, name, domain); xmlFree(content); count++; } } } }/*}}}*/ DMSG(stderr, "parsed contact count: %d\n", count); cleanup: cl->flag &= ~CL_INITLIST; return count; }/*}}}*/ int _cl_do_soapreq_ab(CL *cl)/*{{{*/ { TCPClient *client; char *req = NULL; char *header; char buf[512]; int ret, len; char *ptr = NULL; client = tcpclient_new("contacts.msn.com", 80); ret = _cl_load_soapreq_ab(cl, cl->ablastchange, &req, TRUE); if(ret) { tcpclient_connect(client); header = (char*)xmalloc(strlen(ab_request_header) + 32); DMSG(stderr, "sending ab request\n"); len = sprintf(header, "%s%d\r\n\r\n", ab_request_header, ret); if(tcpclient_send(client, header, len) <= 0) goto cleanup; if(tcpclient_send(client, req, ret) <= 0) goto cleanup; len = tcpclient_recv_header(client, &ptr); /* header */ if(ptr) { HTTPHeader *header; xmlDocPtr doc; xmlParserCtxtPtr ctxt; FILE *fp; DMSG(stderr, "AB response header:\n%s", ptr); header = http_parse_header(ptr); len = header->content_length; DMSG(stderr, "Length: %d\n", len); http_header_destroy(header); memset(buf, 0, sizeof(buf)); fp = fopen("addressbook.xml", "w"); fprintf(fp, buf); len -= (ret = tcpclient_recv(client, buf, sizeof(buf)-1)); ctxt = xmlCreatePushParserCtxt(NULL, NULL, buf, ret, "addressbook.xml"); fprintf(fp, buf); if(ctxt == NULL) { fprintf(stderr, "failed to create parser context"); return 0; } while(len > 0) { memset(buf, 0, sizeof(buf)); len -= (ret=tcpclient_recv(client, buf, sizeof(buf)-1)); fprintf(fp, buf); xmlParseChunk(ctxt, buf, ret, 0); } fclose(fp); xmlParseChunk(ctxt, buf, 0, 1); tcpclient_destroy(client); client = NULL; doc = ctxt->myDoc; len = ctxt->wellFormed; xmlFreeParserCtxt(ctxt); //count += _cl_parse_contacts(cl, doc); xmlFreeDoc(doc); xmlCleanupParser(); DMSG(stderr, "addressbook xml parsing done: %s\n", len?"good":"malformed"); xfree(ptr); } else { DMSG(stderr, "ab: no header found\n\r"); } } else { fprintf(stderr, "failed to load abreq\n"); } cleanup: xfree(header); return 0; }/*}}}*/