static void CASelectReturned(fd_set *readFds);
static void CAReceiveMessage(int fd);
static void CAReceiveHandler(void *data);
-static int CATCPCreateSocket(int family, CATCPSessionInfo_t *tcpServerInfo);
+static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *tcpServerInfo);
#define CHECKFD(FD) \
if (FD > caglobals.tcp.maxfd) \
else if (0 == len)
{
OIC_LOG(INFO, TAG, "Received disconnect from peer. Close connection");
+ item->state = DISCONNECTED;
return CA_DESTINATION_DISCONNECTED;
}
{
CATCPSessionInfo_t *svritem =
(CATCPSessionInfo_t *) u_arraylist_get(caglobals.tcp.svrlist, i);
- if (svritem && 0 <= svritem->fd)
+ if (svritem && 0 <= svritem->fd && CONNECTED == svritem->state)
{
FD_SET(svritem->fd, &readFds);
}
svritem->fd = sockfd;
svritem->sep.endpoint.flags = flag;
svritem->sep.endpoint.adapter = CA_ADAPTER_TCP;
+ svritem->state = CONNECTED;
CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, clientlen,
svritem->sep.endpoint.addr, &svritem->sep.endpoint.port);
}
}
-static void CAWakeUpForReadFdsUpdate(const char *host)
+static ssize_t CAWakeUpForReadFdsUpdate(const char *host)
{
if (caglobals.tcp.connectionFds[1] != -1)
{
{
OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
}
+ return len;
}
+ return -1;
}
static CAResult_t CATCPConvertNameToAddr(int family, const char *host, uint16_t port,
return CA_STATUS_OK;
}
-static int CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
+static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
{
VERIFY_NON_NULL_RET(svritem, TAG, "svritem", -1);
if (-1 == fd)
{
OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
- return -1;
+ return CA_SOCKET_OPERATION_FAILED;
}
+ svritem->fd = fd;
// #2. convert address from string to binary.
struct sockaddr_storage sa = { .ss_family = family };
svritem->sep.endpoint.port, &sa);
if (CA_STATUS_OK != res)
{
- close(fd);
- return -1;
+ return CA_SOCKET_OPERATION_FAILED;
}
// #3. set socket length.
OIC_LOG_V(ERROR, TAG, "failed to connect socket, %s", strerror(errno));
CALogSendStateInfo(svritem->sep.endpoint.adapter, svritem->sep.endpoint.addr,
svritem->sep.endpoint.port, 0, false, strerror(errno));
- close(fd);
- return -1;
+ return CA_SOCKET_OPERATION_FAILED;
}
OIC_LOG(DEBUG, TAG, "connect socket success");
- CAWakeUpForReadFdsUpdate(svritem->sep.endpoint.addr);
- return fd;
+ svritem->state = CONNECTED;
+ CHECKFD(svritem->fd);
+ ssize_t len = CAWakeUpForReadFdsUpdate(svritem->sep.endpoint.addr);
+ if (-1 == len)
+ {
+ return CA_SOCKET_OPERATION_FAILED;
+ }
+ return CA_STATUS_OK;
}
static int CACreateAcceptSocket(int family, CASocket_t *sock)
svritem->sep.endpoint.port = endpoint->port;
svritem->sep.endpoint.flags = endpoint->flags;
svritem->sep.endpoint.ifindex = endpoint->ifindex;
+ svritem->state = CONNECTING;
- // #2. create the socket and connect to TCP server
- int family = (svritem->sep.endpoint.flags & CA_IPV6) ? AF_INET6 : AF_INET;
- int fd = CATCPCreateSocket(family, svritem);
- if (-1 == fd)
- {
- OICFree(svritem);
- return NULL;
- }
-
- // #3. add TCP connection info to list
- svritem->fd = fd;
+ // #2. add TCP connection info to list
oc_mutex_lock(g_mutexObjectList);
if (caglobals.tcp.svrlist)
{
}
oc_mutex_unlock(g_mutexObjectList);
- CHECKFD(fd);
+ // #3. create the socket and connect to TCP server
+ int family = (svritem->sep.endpoint.flags & CA_IPV6) ? AF_INET6 : AF_INET;
+ if (CA_STATUS_OK != CATCPCreateSocket(family, svritem))
+ {
+ return NULL;
+ }
- // pass the connection information to CA Common Layer.
+ // #4. pass the connection information to CA Common Layer.
if (g_connectionCallback)
{
g_connectionCallback(&(svritem->sep.endpoint), true);
close(svritem->fd);
svritem->fd = -1;
OIC_LOG(DEBUG, TAG, "close socket");
+ svritem->state = (CONNECTED == svritem->state) ? DISCONNECTED : svritem->state;
+
+ // pass the connection information to CA Common Layer.
+ if (g_connectionCallback && DISCONNECTED == svritem->state)
+ {
+ g_connectionCallback(&(svritem->sep.endpoint), false);
+ }
}
u_arraylist_remove(caglobals.tcp.svrlist, index);
OICFree(svritem->data);
svritem->data = NULL;
- // pass the connection information to CA Common Layer.
- if (g_connectionCallback)
- {
- g_connectionCallback(&(svritem->sep.endpoint), false);
- }
-
OICFree(svritem);
svritem = NULL;
void CATCPDisconnectAll()
{
oc_mutex_lock(g_mutexObjectList);
+
uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
CATCPSessionInfo_t *svritem = NULL;
for (size_t i = 0; i < length; i++)
close(svritem->fd);
OICFree(svritem->data);
svritem->data = NULL;
+ svritem->state = (CONNECTED == svritem->state) ? DISCONNECTED : svritem->state;
// pass the connection information to CA Common Layer.
- if (g_connectionCallback)
+ if (g_connectionCallback && DISCONNECTED == svritem->state)
{
g_connectionCallback(&(svritem->sep.endpoint), false);
}