void NWork::Peer::Run(std::function<void(const Peer*)>& ondisconnect, std::function<void(const Peer*)>& onconnect, std::function<void(const Peer*, std::vector<Packet>&)>& onreceive){ std::vector<NWork::Packet> temp; unsigned char channelid=0; auto highest= ConnectTime;//use this as time bool bufferoverrun = false; for(auto& x : _Channels){ bufferoverrun = x.Buffer_Overrun(); if(bufferoverrun)break; x.BuildAcks(channelid++, SendingId); if(x.LastTimeReceivedGoodData>highest) highest = x.LastTimeReceivedGoodData; for(auto& y : x.get_Packets()){ int k =0; switch(y.get_Header()->Command){ case(CONNECT): _Connect(y); onconnect(this); break; case(VERIFY_CONNECT): _Verify_Connect(y); onconnect(this); break; case(DISCONNECT): ondisconnect(this); _Disconnect(); break; case(RELIABLE): case(UNRELIABLE): temp.emplace_back(std::move(y)); break; default: break; }; } } if(bufferoverrun){ ondisconnect(this); _Disconnect(); OUTPUT_NETWORK_DEBUG_MSG("Peer "<<SendingId<<" overran the receiving buffer"); return; } if(!temp.empty()) onreceive(this, temp);//allow processing of any packets //check for timeout auto currtime = std::chrono::steady_clock::now(); if(std::chrono::duration_cast<std::chrono::milliseconds>(currtime - highest).count()> TIMEOUT){ ondisconnect(this); _Disconnect(); OUTPUT_NETWORK_DEBUG_MSG("Peer "<<SendingId<<" Timed out"); } if(currtime > NextPingTime && State == PeerState::CONNECTED){//time to send a ping Packet p(nullptr, 0, Commands::PING); Send(p, 0); NextPingTime = currtime + std::chrono::milliseconds(PINGRATE); } }
status_t CDDBQuery::GetSites(bool (*eachFunc)(const char *site, int port, const char *latitude, const char *longitude, const char *description, void *state), void *passThru) { if (!_IsConnected()) _Connect(); BString tmp; tmp = "sites\n"; STRACE((">%s", tmp.String())); if (fSocket.Send(tmp.String(), tmp.Length()) == -1) return B_ERROR; _ReadLine(tmp); if (tmp.FindFirst("210") == -1) { _Disconnect(); return B_ERROR; } for (;;) { BString site; int32 sitePort; BString latitude; BString longitude; BString description; _ReadLine(tmp); if (tmp == ".") break; const char *scanner = tmp.String(); scanner = _GetToken(scanner, site); BString portString; scanner = _GetToken(scanner, portString); sitePort = atoi(portString.String()); scanner = _GetToken(scanner, latitude); scanner = _GetToken(scanner, longitude); description = scanner; if (eachFunc(site.String(), sitePort, latitude.String(), longitude.String(), description.String(), passThru)) break; } _Disconnect(); return B_OK; }
//-------------------------------------------------------------------------- void Connection::UpdateBackground() { for(VeUInt32 i(0); i < m_kCommandList.Size(); ++i) { switch(m_kCommandList[i]) { case COM_CONNECT: _Connect(); break; case COM_DISCONNECT: _Disconnect(); break; default: break; } } m_kCommandList.Clear(); if(m_pkPeer) { Packet* pkPacket(NULL); for(pkPacket = m_pkPeer->Receive(); pkPacket; pkPacket = m_pkPeer->Receive()) { m_kPacketCache.PushBack(pkPacket); } } }
status_t CDDBQuery::_IdentifySelf() { BString username, hostname; username = getenv("USER"); hostname = getenv("HOSTNAME"); if (username.Length() < 1) username = "******"; if (hostname.Length() < 1) hostname = "antares"; BString tmp; tmp << "cddb hello " << username << " " << hostname << " AntaresCDPlayer 1.0\n"; STRACE((">%s", tmp.String())); if (fSocket.Send(tmp.String(), tmp.Length())==-1) { _Disconnect(); return B_ERROR; } _ReadLine(tmp); return B_OK; }
void CDDBQuery::SetToSite(const char *server, int32 port) { _Disconnect(); fServerName = server; fPort = port; fState = kInitial; }
void BaseSignal::Gc() { for(auto it : m_garbage) { _Disconnect(it); } m_garbage.clear(); }
status_t Protocol::Disconnect() { if (IsConnected()) { RawCommand command("LOGOUT"); ProcessCommand(command); } return _Disconnect(); }
status_t Protocol::HandleResponse(Command* command, bigtime_t timeout, bool disconnectOnTimeout) { status_t commandStatus = B_OK; IMAP::ResponseParser parser(*fBufferedSocket); if (IMAP::LiteralHandler* literalHandler = dynamic_cast<IMAP::LiteralHandler*>(command)) parser.SetLiteralHandler(literalHandler); IMAP::Response response; bool done = false; while (!done) { try { status_t status = parser.NextResponse(response, timeout); if (status != B_OK) { // we might have lost the connection, clear the connection state if (status != B_TIMED_OUT || disconnectOnTimeout) _Disconnect(); return status; } if (response.IsUntagged() || response.IsContinuation()) { bool handled = false; for (int32 i = fHandlerList.CountItems(); i-- > 0;) { if (fHandlerList.ItemAt(i)->HandleUntagged(response)) { handled = true; break; } } if (!handled) printf("Unhandled S: %s\n", response.ToString().String()); } else { CommandIDMap::iterator found = fOngoingCommands.find(response.Tag()); if (found != fOngoingCommands.end()) { status_t status = found->second->HandleTagged(response); if (status != B_OK) commandStatus = status; fOngoingCommands.erase(found); } else printf("Unknown tag S: %s\n", response.ToString().String()); } } catch (ParseException& exception) { printf("Error during parsing: %s\n", exception.Message()); } if (fOngoingCommands.size() == 0) done = true; } return commandStatus; }
status_t UnixEndpoint::Close() { TRACE("[%ld] %p->UnixEndpoint::Close()\n", find_thread(NULL), this); UnixEndpointLocker locker(this); if (fState == UNIX_ENDPOINT_CONNECTED) { UnixEndpointLocker peerLocker; if (_LockConnectedEndpoints(locker, peerLocker) == B_OK) { // We're still connected. Disconnect both endpoints! fPeerEndpoint->_Disconnect(); _Disconnect(); } } if (fState == UNIX_ENDPOINT_LISTENING) _StopListening(); _Unbind(); fState = UNIX_ENDPOINT_CLOSED; RETURN_ERROR(B_OK); }
status_t CDDBQuery::_Connect() { if (fConnected) _Disconnect(); BNetAddress address; status_t status = address.SetTo(fServerName.String(), fPort); if (status != B_OK) return status; status = fSocket.Connect(address); if (status != B_OK) return status; fConnected = true; BString tmp; _ReadLine(tmp); _IdentifySelf(); return B_OK; }
void GlobalEventManager::UnSubscribeAll(wxEvtHandler* evh) { _Disconnect(evh, ANY_EVENT); }
GlobalEvent::~GlobalEvent(){ if(m_handler!=NULL) _Disconnect(m_handler); }
void CTraderApi::Disconnect() { _Disconnect(false); }
CTraderApi::~CTraderApi(void) { _Disconnect(false); }
void CTraderApi::QueryInThread(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3) { int iRet = 0; switch (type) { case E_Init: iRet = _Init(); break; case E_Disconnect: _Disconnect(true); // 不再循环 return; case E_ReqUserLoginField: iRet = _ReqUserLogin(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case E_InputOrderField: iRet = _ReqOrderInsert(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case E_InputOrderActionField: iRet = _ReqOrderAction(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case E_QryDepthMarketDataField: iRet = _Subscribe(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; //case QueryType::ReqQryOrder : //case QueryType::ReqQryTrade: //case QueryType::ReqQryInvestor: //case QueryType::ReqQryTradingAccount: //case QueryType::ReqQryInvestorPosition: case E_ReqQueryData_STRUCT: iRet = _ReqQuery(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; default: break; } if (0 == iRet) { //返回成功,填加到已发送池 m_nSleep = 1; } else { m_msgQueue_Query->Input_Copy(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); //失败,按4的幂进行延时,但不超过1s m_nSleep *= 4; m_nSleep %= 1023; } // 将时定时查询功能放在查询队列中,这样退出就不会出问题了 // 由于队列中一直没有东西,所以不会再来触发这个循环,需要隔一定的时间向队列中放一个,最好放在最后 TestInThread(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); if (m_msgQueue_Query->Size() == 0) { m_msgQueue_Query->Input_Copy(E_Heartbeat, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); m_nSleep = 32; } this_thread::sleep_for(chrono::milliseconds(m_nSleep)); }
status_t CDDBQuery::_ReadFromServer(BString &data) { // This function queries the given CDDB server for the existence of the disc's data and // saves the data to file once obtained. // Query for the existence of the disc in the database char idString[10]; sprintf(idString, "%08lx", fCDData.DiscID()); BString query; int32 trackCount = GetTrackCount(&fSCSIData); BString offsetString = OffsetsToString(&fSCSIData); int32 discLength = (fCDData.DiscTime()->GetMinutes() * 60) + fCDData.DiscTime()->GetSeconds(); query << "cddb query " << idString << ' ' << trackCount << ' ' << offsetString << ' ' << discLength << '\n'; STRACE((">%s", query.String())); if (fSocket.Send(query.String(), query.Length()) == -1) { _Disconnect(); return B_ERROR; } BString tmp; _ReadLine(tmp); STRACE(("<%s", tmp.String())); BString category; BString queryDiscID(idString); if (tmp.FindFirst("200") != 0) { if (tmp.FindFirst("211") == 0) { // A 211 means that the query was not exact. To make sure that we don't // have a problem with this in the future, we will choose the first entry that // the server returns. This may or may not be wise, but in my experience, the first // one has been the right one. _ReadLine(tmp); // Get the category from the what the server returned _GetToken(tmp.String(), category); // Now we will get the disc ID for the CD. We will need this when we query for // the track name list. If we send the track name query with the real discID, nothing // will be returned. However, if we send the one from the entry, we'll get the names // and we can take these names attach them to the disc that we have. _GetToken(tmp.String() + category.CountChars(), queryDiscID); // This is to suck up any more search results that the server sends us. BString throwaway; _ReadLine(throwaway); while (throwaway.ByteAt(0) != '.') _ReadLine(throwaway); } else { // We get here for any time the CDDB server does not recognize the CD, amongst other things STRACE(("CDDB lookup error: %s\n", tmp.String())); fCDData.SetGenre("misc"); return B_NAME_NOT_FOUND; } } else { _GetToken(tmp.String() + 3, category); } STRACE(("CDDBQuery::_ReadFromServer: Genre: %s\n", category.String())); if (!category.Length()) { STRACE(("Set genre to 'misc'\n")); category = "misc"; } // Query for the disc's data - artist, album, tracks, etc. query = ""; query << "cddb read " << category << ' ' << queryDiscID << '\n' ; if (fSocket.Send(query.String(), query.Length()) == -1) { _Disconnect(); return B_ERROR; } while (true) { _ReadLine(tmp); if (tmp == "." || tmp == ".\n") break; data << tmp << '\n'; } return B_OK; }
status_t Protocol::Connect(const BNetworkAddress& address, const char* username, const char* password, bool useSSL) { TRACE("Connect\n"); if (useSSL) fSocket = new(std::nothrow) BSecureSocket(address); else fSocket = new(std::nothrow) BSocket(address); if (fSocket == NULL) return B_NO_MEMORY; status_t status = fSocket->InitCheck(); if (status != B_OK) return status; fBufferedSocket = new(std::nothrow) BBufferedDataIO(*fSocket, 32768, false, true); if (fBufferedSocket == NULL) return B_NO_MEMORY; TRACE("Login\n"); fIsConnected = true; LoginCommand login(username, password); status = ProcessCommand(login); if (status != B_OK) { _Disconnect(); return status; } _ParseCapabilities(login.Capabilities()); if (fCapabilities.IsEmpty()) { CapabilityHandler capabilityHandler; status = ProcessCommand(capabilityHandler); if (status != B_OK) return status; _ParseCapabilities(capabilityHandler.Capabilities()); } if (Capabilities().Contains("ID")) { // Get the server's ID into our log class IDCommand : public IMAP::Command, public IMAP::Handler { public: BString CommandString() { return "ID NIL"; } bool HandleUntagged(IMAP::Response& response) { if (response.IsCommand("ID") && response.IsListAt(1)) { puts("Server:"); ArgumentList& list = response.ListAt(1); for (int32 i = 0; i < list.CountItems(); i += 2) { printf(" %s: %s\n", list.ItemAt(i)->ToString().String(), list.ItemAt(i + 1)->ToString().String()); } return true; } return false; } }; IDCommand idCommand; ProcessCommand(idCommand); } return B_OK; }
status_t RemoteHWInterface::Shutdown() { _Disconnect(); return B_OK; }
void CTraderApi::QueryInThread(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3) { int iRet = 0; switch (type) { case E_Init: iRet = _Init(); break; case E_Disconnect: _Disconnect(true); // 不再循环 return; } if (m_pApi) { switch (type) { case E_ReqUserLoginField: iRet = _ReqUserLogin(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case E_UserLogoutField: iRet = _ReqUserLogout(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case QueryType::QueryType_ReqQryTradingAccount: iRet = _ReqQryTradingAccount(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case QueryType::QueryType_ReqQryInvestorPosition: iRet = _ReqQryInvestorPosition(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; // case QueryType::QueryType_ReqQryInstrument: // iRet = _ReqQryInstrument(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); // break; // case QueryType::QueryType_ReqQryInvestor: // iRet = _ReqQryInvestor(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); // break; case QueryType::QueryType_ReqQryOrder: iRet = _ReqQryOrder(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case QueryType::QueryType_ReqQryTrade: iRet = _ReqQryTrade(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; #ifdef HAS_Quote case QueryType::QueryType_ReqQryQuote: iRet = _ReqQryQuote(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; #endif // HAS_Quote #ifdef HAS_Settlement case E_SettlementInfoConfirmField: iRet = _ReqSettlementInfoConfirm(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; case QueryType::QueryType_ReqQrySettlementInfo: iRet = _ReqQrySettlementInfo(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); break; #endif // HAS_Settlement default: break; } } if (0 == iRet) { //返回成功,填加到已发送池 m_nSleep = 1; } else { m_msgQueue_Query->Input_Copy(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3); //失败,按4的幂进行延时,但不超过1s m_nSleep *= 4; m_nSleep %= 1023; } this_thread::sleep_for(chrono::milliseconds(m_nSleep)); }