summaryrefslogtreecommitdiffstats
path: root/PICA_nodewait.c
diff options
context:
space:
mode:
authorme_desk <me_desk@fb96df88-216d-11de-830c-f3023f12d8ac>2013-07-07 00:36:07 +0000
committerme_desk <me_desk@fb96df88-216d-11de-830c-f3023f12d8ac>2013-07-07 00:36:07 +0000
commitcc968fd408127707b8d885ebee2b141416aaf362 (patch)
tree7a5682f531c433d98d25b06c36be76cc26db0f75 /PICA_nodewait.c
parent1214e541265a91ef932e34405660338da263fec8 (diff)
downloadpica-pica-cc968fd408127707b8d885ebee2b141416aaf362.tar.gz
pica-pica-cc968fd408127707b8d885ebee2b141416aaf362.zip
nodewait_start_connection() is split to the two functions, nodewait_start_resolve() and nodewait_start_connect(), each starts new thread for resolving and connecting. Between resolving and connecting check for existing connections with resolved address is performed
git-svn-id: svn://localhost/project_picapica/trunk@251 fb96df88-216d-11de-830c-f3023f12d8ac
Diffstat (limited to 'PICA_nodewait.c')
-rw-r--r--PICA_nodewait.c83
1 files changed, 68 insertions, 15 deletions
diff --git a/PICA_nodewait.c b/PICA_nodewait.c
index 50d02f3..00afdce 100644
--- a/PICA_nodewait.c
+++ b/PICA_nodewait.c
@@ -53,6 +53,9 @@ while(p)
//
//free resources
//
+ if (nw->ai)
+ freeaddrinfo(nw->ai);
+
free(nw);
return;
@@ -64,16 +67,16 @@ while(p)
}
#ifdef WIN32
-static DWORD WINAPI nodewait_thread (void *arg)
+static DWORD WINAPI nodewait_resolve_thread (void *arg)
#else
-static void *nodewait_thread (void *arg)
+static void *nodewait_resolve_thread (void *arg)
#endif
{
struct nodewait *nw = (struct nodewait *)arg;
-struct addrinfo h,*r,*ap;
+struct addrinfo h;
char portbuf[8];
-nw->state = PICA_NODEWAIT_RUNNING;
+nw->state = PICA_NODEWAIT_RESOLVING;
memset(&h,0,sizeof(struct addrinfo));
@@ -90,10 +93,29 @@ h.ai_protocol=IPPROTO_TCP;
sprintf(portbuf,"%u",nw->addr.port);
-if (0!=getaddrinfo(nw->addr.addr, portbuf, &h, &r))
- return 0;
+nw->ai_errorcode = getaddrinfo(nw->addr.addr, portbuf, &h, &nw->ai);
+
+if (nw->ai_errorcode)
+ nw->state = PICA_NODEWAIT_RESOLVING_FAILED;
+else
+ nw->state = PICA_NODEWAIT_RESOLVED;
+
+return 0;
+}
+
+
+#ifdef WIN32
+static DWORD WINAPI nodewait_connect_thread (void *arg)
+#else
+static void *nodewait_connect_thread (void *arg)
+#endif
+{
+struct nodewait *nw = (struct nodewait *)arg;
+struct addrinfo *ap;
-ap = r;
+nw->state = PICA_NODEWAIT_CONNECTING;
+
+ap = nw->ai;
while(ap)
{
@@ -120,18 +142,16 @@ while(ap)
if (ap)
{
nw->nc.addr=*((struct sockaddr_in*)(ap->ai_addr));
- freeaddrinfo(r);
- nw->state = PICA_NODEWAIT_FINISHED_OK;
+ nw->state = PICA_NODEWAIT_CONNECTED;
return 1;
}
-freeaddrinfo(r);
-nw->state = PICA_NODEWAIT_FINISHED_ERR;
+nw->state = PICA_NODEWAIT_CONNECT_FAILED;
return 0;
}
-void nodewait_start_connection(struct PICA_nodeaddr *a)
+void nodewait_start_resolve(struct PICA_nodeaddr *a)
{
struct nodewait *nw;
#ifndef WIN32
@@ -149,7 +169,39 @@ if (!nw)
nw ->addr = *a;
-PICA_debug1("starting connection to node %.255s %u ...", nw->addr.addr, nw->addr.port);
+PICA_debug1("starting to resolve node address %.255s %u ...", nw->addr.addr, nw->addr.port);
+
+#ifndef WIN32
+if (0 != pthread_attr_init(&attr))
+ PICA_error("pthread_attr_init() call failed.");
+
+pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+if (0 != pthread_create(&thr, &attr, nodewait_resolve_thread, (void*)nw))
+ PICA_error("unable to create thread: %s", strerror(errno));
+
+pthread_attr_destroy(&attr);
+#else
+thread_h = CreateThread(NULL, 4096, nodewait_resolve_thread, (void*)nw, 0, &thread_id);
+
+if (NULL != thread_h)
+ CloseHandle(thread_h);
+else
+ PICA_error("unable to create thread: %u", GetLastError());
+#endif
+}
+
+void nodewait_start_connect(struct nodewait *nw)
+{
+#ifndef WIN32
+pthread_t thr;
+pthread_attr_t attr;
+#else
+DWORD thread_id;
+HANDLE thread_h;
+#endif
+
+PICA_debug1("connecting to node %.255s %u ...", nw->addr.addr, nw->addr.port);
#ifndef WIN32
if (0 != pthread_attr_init(&attr))
@@ -157,16 +209,17 @@ if (0 != pthread_attr_init(&attr))
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-if (0 != pthread_create(&thr, &attr, nodewait_thread, (void*)nw))
+if (0 != pthread_create(&thr, &attr, nodewait_connect_thread, (void*)nw))
PICA_error("unable to create thread: %s", strerror(errno));
pthread_attr_destroy(&attr);
#else
-thread_h = CreateThread(NULL, 4096, nodewait_thread, (void*)nw, 0, &thread_id);
+thread_h = CreateThread(NULL, 4096, nodewait_connect_thread, (void*)nw, 0, &thread_id);
if (NULL != thread_h)
CloseHandle(thread_h);
else
PICA_error("unable to create thread: %u", GetLastError());
#endif
+
}