bool TRf24Radio::Send(const uint16& NodeAddr, const uchar& Command, const TMem& Buff) { bool ReceivedAck = false; try { Notify->OnNotifyFmt(ntInfo, "Sending message to node %d ...", NodeAddr); RF24NetworkHeader Header(NodeAddr, Command); TRpiUtil::SetMaxPriority(); uint16 From; uchar Type; TMem Payload; Payload.Gen(PAYLOAD_LEN); TLock Lock(CriticalSection); int RetryN = 0; while (!ReceivedAck && RetryN < RETRY_COUNT) { if (RetryN > 0) { Notify->OnNotifyFmt(ntInfo, "Re-sending message, count %d", RetryN); } // write the message _Send(Header, Buff); // wait for an ACK const uint64 StartTm = TTm::GetCurUniMSecs(); while (!ReceivedAck && TTm::GetCurUniMSecs() - StartTm < ACK_TIMEOUT) { UpdateNetwork(); while (Read(From, Type, Payload)) { if (Type == REQUEST_ACK) { if (From != NodeAddr) { Notify->OnNotifyFmt(ntWarn, "WTF!? received ACK from incorrect node, expected: %u, got: %u", NodeAddr, From); } else { Notify->OnNotifyFmt(ntInfo, "Received ack from node %u", NodeAddr); ReceivedAck = true; } } else { ReadThread.AddToQueue(TMsgInfo(From, Type, Payload)); } } } RetryN++; } TRpiUtil::SetDefaultPriority(); } catch (const PExcept& Except) { Notify->OnNotifyFmt(TNotifyType::ntErr, "Exception when sending!"); TRpiUtil::SetDefaultPriority(); return false; } if (!ReceivedAck) { Notify->OnNotifyFmt(ntInfo, "Failed to send message!"); } return ReceivedAck; }
/********************************************************************* * * JLINKMEM_Process * * Function description * This function should be called more or less regularily to allow * memory reads while the application progam is running. * The more often it is called, the higher the transfer speed. */ void JLINKMEM_Process(void) { if (OS_IsRunning()) { /* No communication until the embOS starts */ if (!_IsInited) { _Init(); _IsInited = 1; } if (HOST_CON) { /* Do nothing until the host connects to us */ // // Handle Timeout timer // if (_TxTimeoutTimer != 0) { _TxTimeoutTimer--; if (_TxTimeoutTimer == 0) { _TxTimeout = 1; } } if (_TxTimeout) { HOST_CON = 0; _TxTimeout = 0; _TxIsPending = 0; _DropTxData(); RX_CNT = 0; /* Drop all bytes form receiving buffer. */ } else { _Receive(); _Send(); } } } }
void Socket::OnTimer( int mselapsed ) { LOCK_SOCKET; if ( m_rate > 0 ) { m_sent -= int( ( mselapsed / 1000.0 ) * m_rate ); if ( m_sent < 0 ) m_sent = 0; if ( m_buffer.length() > 0 ) _Send(wxEmptyString); } else { m_sent = 0; } }
/********************************************************************* * * _Client */ static void _Client(void * p) { long TCPSockID; struct sockaddr_in ServerAddr; int ConnectStatus; int r; // // Wait until link is up and network interface is configured. // while (IP_IFaceIsReady() == 0) { OS_Delay(50); } while(1) { TCPSockID = socket(AF_INET, SOCK_STREAM, 0); // Open socket if (TCPSockID == 0) { // Error, Could not get socket while (1) { BSP_ToggleLED(0); OS_Delay(20); } } else { // // Connect to server // BSP_SetLED(0); ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(SERVER_PORT); ServerAddr.sin_addr.s_addr = htonl(SERVER_IP_ADDR); ConnectStatus = connect(TCPSockID, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in)); if (ConnectStatus != SOCKET_ERROR) { while(1) { if (DIRECTION & 1) { r = _Receive(TCPSockID); if (r == -1) { break; } _Statistics.RxCnt++; } if (DIRECTION & 2) { r = _Send(TCPSockID); if (r == -1) { break; } _Statistics.TxCnt++; } OS_Delay(50); } } } _Statistics.ErrCnt++; closesocket(TCPSockID); OS_Delay(1000); } }
DWORD WINAPI CHttpSender::HttpSendThread(VOID* pParam) { HttpSendThreadParams* params = (HttpSendThreadParams*)pParam; HttpSendThreadParams param_copy = *params; SetEvent(params->an->m_hCompletionEvent); int nResult = _Send(params->m_sURL, params->m_sFileName, params->an); param_copy.an->m_nCompletionStatus = nResult?0:1; SetEvent(param_copy.an->m_hCompletionEvent); return nResult; }
bool TRf24Radio::Read(uint16& From, uchar& Type, TMem& Payload) { TLock Lock(CriticalSection); try { RF24NetworkHeader Header; if (Network->available()) { // read the message Network->peek(Header); const uchar MsgType = Header.type; Notify->OnNotifyFmt(TNotifyType::ntInfo, "Received message of type %u from node %u, reading ...", MsgType, Header.from_node); if (!TRadioProtocol::IsValidType(MsgType)) { Network->read(Header, nullptr, 0); } else if (!TRadioProtocol::HasPayload(MsgType)) { Network->read(Header, nullptr, 0); } else { Network->read(Header, Payload(), PAYLOAD_LEN); } From = Header.from_node; Type = MsgType; // acknowledge if (Type != REQUEST_ACK) { RF24NetworkHeader Header(From, REQUEST_ACK); _Send(Header); } return true; } } catch (...) { Notify->OnNotifyFmt(TNotifyType::ntErr, "Exception while reading!"); } return false; }
//! @brief Send data over connection. bool Socket::Send( const wxString& data ) { LOCK_SOCKET; return _Send( data ); }
static void _MainThread ( void* apParam ) { u32 lBits[ 4 ]; u32 lEventFlag; while ( 1 ) { WaitEventFlag ( s_EventFlag, 0x00000155, 1, lBits ); if ( lBits[ 0 ] & 0x00000001 ) { /* SIO2_TransferInit0 */ ClearEventFlag ( s_EventFlag, ~0x00000001 ); lEventFlag = 0x00000002; } else if ( lBits[ 0 ] & 0x00000004 ) { /* SIO2_TransferInit1 */ ClearEventFlag ( s_EventFlag, ~0x00000004 ); lEventFlag = 0x00000008; } else if ( lBits[ 0 ] & 0x00000010 ) { /* SIO2_TransferInit2 */ ClearEventFlag ( s_EventFlag, ~0x00000010 ); lEventFlag = 0x00000020; } else if ( lBits[ 0 ] & 0x00000040 ) { /* SIO2_TransferInit3 */ ClearEventFlag ( s_EventFlag, ~0x00000040 ); lEventFlag = 0x00000080; } else if ( lBits[ 0 ] & 0x00000100 ) { /* SIO2_TransferInit4 */ ClearEventFlag ( s_EventFlag, ~0x00000100 ); lEventFlag = 0x00000200; } else break; loop: SetEventFlag ( s_EventFlag, lEventFlag ); WaitEventFlag ( s_EventFlag, 0x00001400, 1, lBits ); /* SIO2_Transfer or SIO2_TransferReset */ if ( lBits[ 0 ] & 0x00001000 ) { /* SIO2_TransferReset */ ClearEventFlag ( s_EventFlag, ~0x00001000 ); continue; } /* end if */ ClearEventFlag ( s_EventFlag, ~0x00000400 ); /* SIO2_Transfer */ SIO2_SwitchCtrlC (); _Send ( s_pTransferData ); SIO2_SwitchCtrl1 (); WaitEventFlag ( s_EventFlag, 0x00002000, 0, NULL ); ClearEventFlag ( s_EventFlag, ~0x00002000 ); _Recv ( s_pTransferData ); lEventFlag = 0x00000800; goto loop; } /* end while */ } /* end _MainThread */