summaryrefslogtreecommitdiffstats
path: root/PICA_nodewait.c
diff options
context:
space:
mode:
authorAnton <anton@picapica>2016-04-07 21:17:57 +0400
committerAnton <anton@picapica>2016-04-07 21:17:57 +0400
commitfff351c20da3294f9f8d13d94660fea099093738 (patch)
treef6230721c0d836c9d3fb9c63dc9b1e94344f9a55 /PICA_nodewait.c
parentd56c7301b718754c7ec6b2baad603ff001d5a46a (diff)
downloadpica-pica-fff351c20da3294f9f8d13d94660fea099093738.tar.gz
pica-pica-fff351c20da3294f9f8d13d94660fea099093738.zip
fixed code style using "astyle --style=allman --lineend=linux --indent=tab --pad-oper *.c *.h"
Diffstat (limited to 'PICA_nodewait.c')
-rw-r--r--PICA_nodewait.c220
1 files changed, 110 insertions, 110 deletions
diff --git a/PICA_nodewait.c b/PICA_nodewait.c
index d9b49b0..4be919b 100644
--- a/PICA_nodewait.c
+++ b/PICA_nodewait.c
@@ -1,4 +1,4 @@
-
+
#include "PICA_nodewait.h"
#include "PICA_log.h"
#include "PICA_common.h"
@@ -26,45 +26,45 @@ struct nodewait *nodewait_list;
struct nodewait *nodewait_list_addnew()
{
-struct nodewait *nw;
-
-nw = (struct nodewait*)calloc(1, sizeof(struct nodewait));
-
-if (!nw)
- return 0;
+ struct nodewait *nw;
+
+ nw = (struct nodewait*)calloc(1, sizeof(struct nodewait));
+
+ if (!nw)
+ return 0;
-nw->next = nodewait_list;
-nodewait_list = nw;
+ nw->next = nodewait_list;
+ nodewait_list = nw;
-nw->state = PICA_NODEWAIT_NEW;
-return nw;
+ nw->state = PICA_NODEWAIT_NEW;
+ return nw;
}
void nodewait_list_delete(struct nodewait *nw)
{
-struct nodewait **pp = &nodewait_list;
-struct nodewait *p = nodewait_list;
+ struct nodewait **pp = &nodewait_list;
+ struct nodewait *p = nodewait_list;
-PICA_debug3("nodewait_list_delete(%p)", nw);
+ PICA_debug3("nodewait_list_delete(%p)", nw);
-while(p)
+ while(p)
{
- if (p == nw)
+ if (p == nw)
{
- *pp = nw->next;
- //
- //free resources
- //
- if (nw->ai)
- freeaddrinfo(nw->ai);
-
- free(nw);
-
- return;
+ *pp = nw->next;
+ //
+ //free resources
+ //
+ if (nw->ai)
+ freeaddrinfo(nw->ai);
+
+ free(nw);
+
+ return;
}
-
- pp = &p->next;
- p = p->next;
+
+ pp = &p->next;
+ p = p->next;
}
}
@@ -74,15 +74,15 @@ static DWORD WINAPI nodewait_resolve_thread (void *arg)
static void *nodewait_resolve_thread (void *arg)
#endif
{
-/*
-* DO NOT CALL LOGGING FUNCTIONS HERE, THEY ARE NOT THREAD-SAFE
-*/
-struct nodewait *nw = (struct nodewait *)arg;
-struct addrinfo h;
-char portbuf[8];
+ /*
+ * DO NOT CALL LOGGING FUNCTIONS HERE, THEY ARE NOT THREAD-SAFE
+ */
+ struct nodewait *nw = (struct nodewait *)arg;
+ struct addrinfo h;
+ char portbuf[8];
-memset(&h,0,sizeof(struct addrinfo));
+ memset(&h, 0, sizeof(struct addrinfo));
#ifdef AI_ADDRCONFIG
#ifdef AI_IDN
@@ -94,21 +94,21 @@ memset(&h,0,sizeof(struct addrinfo));
#define H_AI_FLAGS 0
#endif
-h.ai_flags=H_AI_FLAGS;
-h.ai_family=AF_INET;//CONF - IPv4, IPv6, IPv4 then IPv6, IPv6 then IPv4
-h.ai_socktype=SOCK_STREAM;
-h.ai_protocol=IPPROTO_TCP;
+ h.ai_flags = H_AI_FLAGS;
+ h.ai_family = AF_INET; //CONF - IPv4, IPv6, IPv4 then IPv6, IPv6 then IPv4
+ h.ai_socktype = SOCK_STREAM;
+ h.ai_protocol = IPPROTO_TCP;
-sprintf(portbuf,"%u",nw->addr.port);
+ sprintf(portbuf, "%u", nw->addr.port);
-nw->ai_errorcode = getaddrinfo(nw->addr.addr, portbuf, &h, &nw->ai);
+ 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;
+ if (nw->ai_errorcode)
+ nw->state = PICA_NODEWAIT_RESOLVING_FAILED;
+ else
+ nw->state = PICA_NODEWAIT_RESOLVED;
-return 0;
+ return 0;
}
@@ -118,121 +118,121 @@ static DWORD WINAPI nodewait_connect_thread (void *arg)
static void *nodewait_connect_thread (void *arg)
#endif
{
-/*
-* DO NOT CALL LOGGING FUNCTIONS HERE, THEY ARE NOT THREAD-SAFE
-*/
-struct nodewait *nw = (struct nodewait *)arg;
-struct addrinfo *ap;
+ /*
+ * DO NOT CALL LOGGING FUNCTIONS HERE, THEY ARE NOT THREAD-SAFE
+ */
+ struct nodewait *nw = (struct nodewait *)arg;
+ struct addrinfo *ap;
-ap = nw->ai;
+ ap = nw->ai;
-while(ap)
+ while(ap)
{
- nw->nc.sck=socket(ap->ai_family,ap->ai_socktype,ap->ai_protocol);
- if (nw->nc.sck==SOCKET_ERROR)
+ nw->nc.sck = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
+ if (nw->nc.sck == SOCKET_ERROR)
{
- ap=ap->ai_next;
- continue;
+ ap = ap->ai_next;
+ continue;
}
- if (0 != connect(nw->nc.sck,ap->ai_addr,ap->ai_addrlen))
+ if (0 != connect(nw->nc.sck, ap->ai_addr, ap->ai_addrlen))
{
- ap=ap->ai_next;
-
- CLOSE(nw->nc.sck);
- continue;
+ ap = ap->ai_next;
+
+ CLOSE(nw->nc.sck);
+ continue;
}
- else
+ else
{
- break;
+ break;
}
}
-if (ap)
+ if (ap)
{
- nw->nc.addr=*((struct sockaddr_in*)(ap->ai_addr));
+ nw->nc.addr = *((struct sockaddr_in*)(ap->ai_addr));
- nw->state = PICA_NODEWAIT_CONNECTED;
- return 1;
+ nw->state = PICA_NODEWAIT_CONNECTED;
+ return 1;
}
-nw->state = PICA_NODEWAIT_CONNECT_FAILED;
-return 0;
+ nw->state = PICA_NODEWAIT_CONNECT_FAILED;
+ return 0;
}
void nodewait_start_resolve(struct PICA_nodeaddr *a)
{
-struct nodewait *nw;
+ struct nodewait *nw;
#ifndef WIN32
-pthread_t thr;
-pthread_attr_t attr;
+ pthread_t thr;
+ pthread_attr_t attr;
#else
-DWORD thread_id;
-HANDLE thread_h;
+ DWORD thread_id;
+ HANDLE thread_h;
#endif
-nw = nodewait_list_addnew();
+ nw = nodewait_list_addnew();
-if (!nw)
- return;
+ if (!nw)
+ return;
-nw ->addr = *a;
+ nw ->addr = *a;
-PICA_debug1("starting to resolve node address %.255s %u ...", nw->addr.addr, nw->addr.port);
+ PICA_debug1("starting to resolve node address %.255s %u ...", nw->addr.addr, nw->addr.port);
-nw->state = PICA_NODEWAIT_RESOLVING;
+ nw->state = PICA_NODEWAIT_RESOLVING;
#ifndef WIN32
-if (0 != pthread_attr_init(&attr))
- PICA_error("pthread_attr_init() call failed.");
+ if (0 != pthread_attr_init(&attr))
+ PICA_error("pthread_attr_init() call failed.");
-pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ 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));
+ if (0 != pthread_create(&thr, &attr, nodewait_resolve_thread, (void*)nw))
+ PICA_error("unable to create thread: %s", strerror(errno));
-pthread_attr_destroy(&attr);
+ pthread_attr_destroy(&attr);
#else
-thread_h = CreateThread(NULL, 4096, nodewait_resolve_thread, (void*)nw, 0, &thread_id);
+ 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());
+ 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;
+ pthread_t thr;
+ pthread_attr_t attr;
#else
-DWORD thread_id;
-HANDLE thread_h;
+ DWORD thread_id;
+ HANDLE thread_h;
#endif
-PICA_debug1("connecting to node %.255s %u ...", nw->addr.addr, nw->addr.port);
+ PICA_debug1("connecting to node %.255s %u ...", nw->addr.addr, nw->addr.port);
-nw->state = PICA_NODEWAIT_CONNECTING;
+ nw->state = PICA_NODEWAIT_CONNECTING;
#ifndef WIN32
-if (0 != pthread_attr_init(&attr))
- PICA_error("pthread_attr_init() call failed.");
+ if (0 != pthread_attr_init(&attr))
+ PICA_error("pthread_attr_init() call failed.");
-pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-if (0 != pthread_create(&thr, &attr, nodewait_connect_thread, (void*)nw))
- PICA_error("unable to create thread: %s", strerror(errno));
+ if (0 != pthread_create(&thr, &attr, nodewait_connect_thread, (void*)nw))
+ PICA_error("unable to create thread: %s", strerror(errno));
-pthread_attr_destroy(&attr);
+ pthread_attr_destroy(&attr);
#else
-thread_h = CreateThread(NULL, 4096, nodewait_connect_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());
+ if (NULL != thread_h)
+ CloseHandle(thread_h);
+ else
+ PICA_error("unable to create thread: %u", GetLastError());
#endif
}