//-------------------------------------------------------------------------- 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 BMediaRecorder::Connect(const dormant_node_info& dormantNode, const media_format& format) { CALLED(); if (fInitErr != B_OK) return fInitErr; if (fConnected) return B_MEDIA_ALREADY_CONNECTED; media_node node; status_t err = BMediaRoster::Roster()->InstantiateDormantNode(dormantNode, &node, B_FLAVOR_IS_GLOBAL); if (err != B_OK) return err; fReleaseOutputNode = true; err = _Connect(node, NULL, format); if (err != B_OK) { BMediaRoster::Roster()->ReleaseNode(node); fReleaseOutputNode = false; } return err; }
bool Wininet::_CallURL(CALLURLDATA *pcud, MEMDATA *pBuf) { bool r = false; HttpTools::URLDATA ud; if(HttpTools::_parseUrl(pcud->pstrURL, &ud)) { DWORD dwRequestFlags = pcud->SendRequest_dwFlags; if(ud.scheme == HttpTools::UDS_HTTPS)dwRequestFlags |= WISRF_IS_HTTPS; else dwRequestFlags &= ~(WISRF_IS_HTTPS); for(BYTE bi = 0; bi < pcud->bTryCount; bi++) { //Delay. if(bi > 0) { if(pcud->hStopEvent != NULL) { if(CWA(kernel32, WaitForSingleObject)(pcud->hStopEvent, pcud->dwRetryDelay) != WAIT_TIMEOUT)goto END; } else CWA(kernel32, Sleep)(pcud->dwRetryDelay); } DWORD dwConnectFlags = pcud->Connect_dwFlags; BYTE pp_m = 1; if(pcud->bAutoProxy) { dwConnectFlags |= WICF_USE_IE_PROXY; pp_m++; } for(BYTE pp = 0; pp < pp_m; pp++) { if(pp == 1)dwConnectFlags &= ~(WICF_USE_IE_PROXY); //Connection. HINTERNET hConnect = _Connect(pcud->pstrUserAgent, ud.host, ud.port, dwConnectFlags); if(hConnect) { HINTERNET hRequest = _SendRequest(hConnect, ud.uri, NULL, pcud->SendRequest_pPostData, pcud->SendRequest_dwPostDataSize, dwRequestFlags); if(hRequest) { if(pcud->DownloadData_pstrFileName)r = _DownloadDataToFile(hRequest, pcud->DownloadData_pstrFileName, pcud->DownloadData_dwSizeLimit, pcud->hStopEvent); else r = _DownloadData(hRequest, pBuf, pcud->DownloadData_dwSizeLimit, pcud->hStopEvent); CWA(wininet, InternetCloseHandle)(hRequest); } _CloseConnection(hConnect); if(r)goto END; } } } END: HttpTools::_freeUrlData(&ud); } return r; }
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; }
status_t BMediaRecorder::Connect(const media_node& node, const media_output* output, const media_format* format) { CALLED(); if (fInitErr != B_OK) return fInitErr; if (fConnected) return B_MEDIA_ALREADY_CONNECTED; if (format == NULL && output != NULL) format = &output->format; return _Connect(node, output, *format); }
status_t BMediaRecorder::Connect(const media_format& format) { CALLED(); if (fInitErr != B_OK) return fInitErr; if (fConnected) return B_MEDIA_ALREADY_CONNECTED; status_t err = B_OK; media_node node; switch (format.type) { // switch on format for default case B_MEDIA_RAW_AUDIO: err = BMediaRoster::Roster()->GetAudioMixer(&node); break; case B_MEDIA_RAW_VIDEO: case B_MEDIA_ENCODED_VIDEO: err = BMediaRoster::Roster()->GetVideoInput(&node); break; // give up? default: return B_MEDIA_BAD_FORMAT; } if (err != B_OK) return err; fReleaseOutputNode = true; err = _Connect(node, NULL, format); if (err != B_OK) { BMediaRoster::Roster()->ReleaseNode(node); fReleaseOutputNode = false; } return err; }
BOOL CTCPConnect::StartSocket(CString strPort,CString strIP) { int port; CCommonConvert::CStringToint(port,strPort); m_uPort = port; m_strIP = strIP; CString strErr; if(m_bIsCreate) { return FALSE; } if(!Startup()) { strErr = _T("初始化Socket失败"); WriteErrLog(strErr); //ShowSocketError(); return FALSE; } if(!CreateSocket()) { strErr = _T("创建Socket套接字失败"); WriteErrLog(strErr); return FALSE; } if(!_Connect()) { CString str; str = (_T("连接服务器失败,确认IP地址端口无误")); WriteErrLog(str); CloseSocket(); return FALSE; } m_bIsCreate = TRUE; return TRUE; }
RemoteHWInterface::RemoteHWInterface(const char* target) : HWInterface(), fTarget(target), fRemoteHost(NULL), fRemotePort(10900), fIsConnected(false), fProtocolVersion(100), fConnectionSpeed(0), fListenPort(10901), fSendEndpoint(NULL), fReceiveEndpoint(NULL), fSendBuffer(NULL), fReceiveBuffer(NULL), fSender(NULL), fReceiver(NULL), fEventThread(-1), fEventStream(NULL), fCallbackLocker("callback locker") { fDisplayMode.virtual_width = 640; fDisplayMode.virtual_height = 480; fDisplayMode.space = B_RGB32; fRemoteHost = strdup(fTarget); char *portStart = strchr(fRemoteHost, ':'); if (portStart != NULL) { portStart[0] = 0; portStart++; if (sscanf(portStart, "%lu", &fRemotePort) != 1) { fInitStatus = B_BAD_VALUE; return; } fListenPort = fRemotePort + 1; } fSendEndpoint = new(std::nothrow) BNetEndpoint(); if (fSendEndpoint == NULL) { fInitStatus = B_NO_MEMORY; return; } fReceiveEndpoint = new(std::nothrow) BNetEndpoint(); if (fReceiveEndpoint == NULL) { fInitStatus = B_NO_MEMORY; return; } fInitStatus = fReceiveEndpoint->Bind(fListenPort); if (fInitStatus != B_OK) return; fSendBuffer = new(std::nothrow) StreamingRingBuffer(16 * 1024); if (fSendBuffer == NULL) { fInitStatus = B_NO_MEMORY; return; } fInitStatus = fSendBuffer->InitCheck(); if (fInitStatus != B_OK) return; fReceiveBuffer = new(std::nothrow) StreamingRingBuffer(16 * 1024); if (fReceiveBuffer == NULL) { fInitStatus = B_NO_MEMORY; return; } fInitStatus = fReceiveBuffer->InitCheck(); if (fInitStatus != B_OK) return; fSender = new(std::nothrow) NetSender(fSendEndpoint, fSendBuffer); if (fSender == NULL) { fInitStatus = B_NO_MEMORY; return; } fReceiver = new(std::nothrow) NetReceiver(fReceiveEndpoint, fReceiveBuffer); if (fReceiver == NULL) { fInitStatus = B_NO_MEMORY; return; } fEventStream = new(std::nothrow) RemoteEventStream(); if (fEventStream == NULL) { fInitStatus = B_NO_MEMORY; return; } fSendEndpoint->SetTimeout(3 * 1000 * 1000); fInitStatus = _Connect(); if (fInitStatus != B_OK) return; fEventThread = spawn_thread(_EventThreadEntry, "remote event thread", B_NORMAL_PRIORITY, this); if (fEventThread < 0) { fInitStatus = fEventThread; return; } resume_thread(fEventThread); }
void CEodLoadFutOptClosePriceFromPp::LoadFromPp() { Report(StepMask(),enRpMtMessage, _T("Loading future options close price from Price Provider...")); bool bIsStopped = false; m_nStartProgress = m_nProgress; try { EOD::CEodMktStructureMgrPtr spMktStrMgr = CEodServer::s_pEodServer->MktStructureMgr(); _PrepareIndexCollection(spMktStrMgr->UndByIdColl()); if(m_spIndexColl && m_spIndexColl->size()) { EODSM::CSmSettingsMgrPtr spSettingMgr(CEodServer::s_pEodServer->SettingsMgr()); _Connect(spSettingMgr); m_itNextIndex = m_spIndexColl->begin(); ATLASSERT(m_itNextIndex != m_spIndexColl->end()); HANDLE hStopEvent = GetStopEventHandle(); m_hIndexReceived = CreateEvent(NULL,FALSE,FALSE,NULL); m_hDisconnect = CreateEvent(NULL,FALSE,FALSE,NULL); const HANDLE hEvents[] = {hStopEvent, m_hIndexReceived, m_hDisconnect}; SetEvent(m_hIndexReceived); while(true) { DWORD dwRes = WaitForMultipleObjects(sizeof(hEvents) / sizeof(HANDLE), hEvents, FALSE, INFINITE ); if(dwRes == WAIT_OBJECT_0) throw EODCO::CEodStopException(); else if(dwRes == WAIT_OBJECT_0 + 1) { if(m_itNextIndex == m_spIndexColl->end()) break; else { _RequestFutureOptionsPrice(); } } else if(dwRes == WAIT_OBJECT_0 + 2) { _Connect(spSettingMgr); SetEvent(m_hIndexReceived); } } Disconnect(); if(m_hIndexReceived) { CloseHandle(m_hIndexReceived); m_hIndexReceived = NULL; } if(m_hDisconnect) { CloseHandle(m_hDisconnect); m_hDisconnect = NULL; } long nIndexCount, nOptCount, nTotalOptCount; _GetUpdatedContracts(nIndexCount, nOptCount, nTotalOptCount); Report(StepMask(),enRpMtMessage, _T("Loaded close price for %d future options of %d indexes out of %d requested future options of %d indexes"), nOptCount, nIndexCount, nTotalOptCount, m_spIndexColl->size()); } else { m_nProgress = 100L; Report(StepMask(),enRpMtProgress, _T("%d"), m_nProgress); CEodServer::s_pEodServer->StepsMgr()->IncProgress(); Report(StepMask(),enRpMtMessage, _T("There are no future options with positions exists in DB")); } } catch(EODCO::CEodStopException) { bIsStopped = true; } catch (exception& e) { Disconnect(); if(m_hIndexReceived) { CloseHandle(m_hIndexReceived); m_hIndexReceived = NULL; } if(m_hDisconnect) { CloseHandle(m_hDisconnect); m_hDisconnect = NULL; } throw e; } if(bIsStopped) throw EODCO::CEodStopException(); }
/** * Connect to a server. * * Connects to a server with specified *host* address and *port* number. After the connection has * succeeded, callback function {@link onConnect} is called. Listening data from the connected server also begins. * Replied messages from the connected server will be converted to {@link Invoke} classes and will be shifted to * the {@link WebCommunicator.listener listener}'s {@link IProtocol.replyData replyData()} method. * * If the connection fails immediately, either an event is dispatched or an exception is thrown: an error * event is dispatched if a host was specified, and an exception is thrown if no host was specified. Otherwise, * the status of the connection is reported by an event. If the socket is already connected, the existing * connection is closed first. * * @param ip The name or IP address of the host to connect to. * If no host is specified, the host that is contacted is the host where the calling file resides. * If you do not specify a host, use an event listener to determine whether the connection was * successful. * @param port The port number to connect to. */ virtual void connect(const std::string &ip, int port) { _Connect(ip, port); listen_message(); };