NeoClient::NeoClient(const string addr, const unsigned short port,const SERVICE_TYPE svctype): clientSwitch(true) { m_pNEOBaseLib = CNEOBaseLibrary::getInstance("neoclient",".","clientlog",NULL); m_pNEOBaseLib->m_pMemPool->Register(m_pNEOBaseLib,"NeoClient::m_pNEOBaseLib"); setInetAddr(addr,port); if(init(svctype)) { doConnection(); doRecv(); doSend(); } }
void CANWrap::pollCallback(int status, int events) { if (status == 0) { if (events & UV_WRITABLE) { const int err = doSend() < 0 ? errno : 0; m_pollEvents &= ~UV_WRITABLE; doPoll(); if (!m_sentCallback.IsEmpty()) { Nan::HandleScope scope; Local<Value> argv[1] = {Nan::New(err)}; m_sentCallback.Call(1, argv); } else { callErrorCallback(err); } } else if (events & UV_READABLE) { const int err = doRecv(); if (err < 0) { callErrorCallback(errno); } else if (!m_messageCallback.IsEmpty()) { Nan::HandleScope scope; Local<Value> argv[] = { Nan::New(m_recvBuffer.can_id), Nan::CopyBuffer(reinterpret_cast<char*>(&m_recvBuffer.data), m_recvBuffer.can_dlc) .ToLocalChecked()}; m_messageCallback.Call(2, argv); } } } else { callErrorCallback(status); } }
/** * return 0 on success/got reply, * <0 on fail. Errno returned. * >0 on success, but no packet (EINTR or dup packet) */ static int recvEchoReply(int fd) { int err; char packet[1024]; ssize_t packetlen; double now; char lag[128]; int isDup = 0; int isReorder = 0; int ttl; int tos; char tosString[128] = {0}; char ttlString[128] = {0}; struct GtpReply gtp; if (options.verbose > 2) { fprintf(stderr, "%s: recvEchoReply()\n", argv0); } now = clock_get_dbl(); memset(packet, 0, sizeof(packet)); if (0 > (packetlen = doRecv(fd, (void*)packet, sizeof(packet), &ttl, &tos))) { switch(errno) { case ECONNREFUSED: connectionRefused++; handleRecvErr(fd, "Port closed", 0); return 1; case EINTR: return 1; case EHOSTUNREACH: handleRecvErr(fd, "Host unreachable or TTL exceeded", 0); return 1; default: err = errno; fprintf(stderr, "%s: recv(%d, ...): %s\n", argv0, fd, strerror(errno)); return err; } } /* create ttl string */ if (0 <= ttl) { snprintf(ttlString, sizeof(ttlString), "ttl=%d ", ttl); } /* create tos string */ if (0 <= tos) { char scratch[128]; snprintf(tosString, sizeof(tosString), "%s ", tos2String(tos, scratch, sizeof(scratch))); } gtp = parseReply(packet, packetlen); if (!gtp.ok) { return 1; } if (gtp.msg != GTPMSG_ECHOREPLY) { fprintf(stderr, "%s: Got non-EchoReply type of msg (type: %d)\n", argv0, gtp.msg); return 1; } if (curSeq - gtp.seq >= TRACKPINGS_SIZE) { strcpy(lag, "Inf"); } else { int pos = gtp.seq % TRACKPINGS_SIZE; double lagf = now - sendTimes[pos]; if (gotIt[pos]) { isDup = 1; } gotIt[pos]++; snprintf(lag, sizeof(lag), "%.2f ms", 1000 * lagf); if (!isDup) { totalTime += lagf; totalTimeSquared += lagf * lagf; totalTimeCount++; if ((0 > totalMin) || (lagf < totalMin)) { totalMin = lagf; } if ((0 > totalMax) || (lagf > totalMax)) { totalMax = lagf; } } if (options.autowait) { options.wait = 2 * (totalTime / totalTimeCount); if (options.verbose > 1) { fprintf(stderr, "%s: Adjusting waittime to %.6f\n", argv0, options.wait); } } } /* detect packet reordering */ if (!isDup) { if (highestSeq > gtp.seq) { reorder++; isReorder = 1; } else { highestSeq = gtp.seq; } } if (options.flood) { if (!isDup) { printf("\b \b"); } } else { printf("%u bytes from %s: ver=%d seq=%u %s%stime=%s%s%s\n", (int)packetlen, options.targetip, gtp.version, gtp.seq, tosString[0] ? tosString : "", ttlString[0] ? ttlString : "", lag, isDup ? " (DUP)" : "", isReorder ? " (out of order)" : ""); } if (isDup) { dups++; } return isDup; }
int xsConnection::processEvent(xsEvent *e) { // xsConnection *conn; xsConnEvent evt; // handle resolve event if (e->type == XS_EVT_RESOLVE) { XS_TRACE("[NET]xsConnResolveHandler: state:%d", state); if (state == XS_CONN_RESOLVING) {// should connect if (e->sys->data.resolve.success) { addr.addr.n = e->sys->data.resolve.addr.addr.n; return doConnect(); } XS_ERROR("[NET]xsConnResolveHandler: Resolve failed."); } else { XS_ERROR("[NET]xsConnResolveHandler: Invoke XS_EVT_RESOLVE on wrong state: %d", state); } return XS_EC_ERROR; } // Cannot handle other event except socket event. if (e->type != XS_EVT_SOCK_READ && e->type != XS_EVT_SOCK_WRITE && e->type != XS_EVT_SOCK_CONNECT && e->type != XS_EVT_SOCK_CLOSE) return XS_EC_OK; // handle socket event if (e->sys->data.socket.status != XS_EC_OK) { XS_ERROR("socket error. %d", e->sys->data.socket.status); destroyInstance(); return XS_EC_OK; } switch (e->type) { case XS_EVT_SOCK_READ: XS_TRACE("[NET]xsConnHandler: OnRead"); if (state == XS_CONN_ESTABLISHED) return doRecv(); break; case XS_EVT_SOCK_WRITE: XS_TRACE("[NET]xsConnHandler: OnWrite"); if (state == XS_CONN_ESTABLISHED) return doSend(); break; case XS_EVT_SOCK_CONNECT: XS_TRACE("[NET]xsConnHandler: OnConnect"); if (state == XS_CONN_CONNECTING) { state = XS_CONN_ESTABLISHED; evt.type = XS_EVT_CONN_ESTABLISHED; xsEvent::send(this, &evt); } else { XS_ERROR("[NET]xsConnHandler: Invoke XS_EVT_SOCK_CONNECT on wrong state: %d", state); } break; case XS_EVT_SOCK_CLOSE: XS_TRACE("[NET]xsConnHandler: OnClose"); destroyInstance(); break; } return XS_EC_OK; }