bool Socket::WriteHandler(std::unique_lock<std::mutex>& guard) { if (!IsOpen()) return false; std::size_t bytesToSend = _writeBuffer.GetActiveSize(); if (bytesToSend == 0) return HandleQueue(guard); boost::system::error_code error; std::size_t bytesWritten = _socket.write_some(boost::asio::buffer(_writeBuffer.GetReadPointer(), bytesToSend), error); if (error) { if (error == boost::asio::error::would_block || error == boost::asio::error::try_again) return AsyncProcessQueue(guard); return false; } else if (bytesWritten == 0) return false; else if (bytesWritten < bytesToSend) { _writeBuffer.ReadCompleted(bytesWritten); _writeBuffer.Normalize(); return AsyncProcessQueue(guard); } // now bytesWritten == bytesToSend _writeBuffer.Reset(); return HandleQueue(guard); }
ListNode * HandleRest(ListNode * resthead, ParkingLot * parkinglot, int timeunit, FILE * outputfile) { Rest * rest; ListNode * aux; int i=0; /*Get the head of the restricion list to the aux pointer for the first while comparation*/ aux = resthead; if(aux == NULL) return NULL; /*Get the first restriction*/ rest = (Rest*) getItemLinkedList(aux); while(aux != NULL && GetRestTime(rest) == timeunit) { if(GetRestFlag(rest) == 'p') { i = FindIP(GetVertices(parkinglot), GetRestCoord(rest, 'x'), GetRestCoord(rest, 'y'), GetRestCoord(rest, 'z'), GetDecoder(parkinglot) ); if(GetIP_Flagres(i, GetDecoder(parkinglot) ) == 1) { ReleasePos(i, GetDecoder(parkinglot) ); IncFreeSpots(parkinglot); if( GetFreeSpots(parkinglot) != 0) HandleQueue(parkinglot, outputfile, timeunit); } else { RestrictPos(i, GetDecoder(parkinglot) ); DecFreeSpots(parkinglot); } } else if(GetRestFlag(rest) == 'f') { if( IsFloorRestricted( GetRestCoord(rest, 'z'), parkinglot ) ) { ReleaseWholeFloor(i, parkinglot ); /* la dento temos de fazer bue release spots */ if( GetFreeSpots(parkinglot) != 0) HandleQueue(parkinglot, outputfile, timeunit); } else RestrictWholeFloor( GetRestCoord(rest, 'z'), parkinglot ); } aux = getNextNodeLinkedList(aux); rest = (Rest*) getItemLinkedList(aux); } /*Return the new head of the linked list*/ return aux; }
void CDplusProtocol::Task(void) { CBuffer Buffer; CIp Ip; CCallsign Callsign; CDvHeaderPacket *Header; CDvFramePacket *Frame; CDvLastFramePacket *LastFrame; // handle incoming packets if ( m_Socket.Receive(&Buffer, &Ip, 20) != -1 ) { // crack the packet if ( (Frame = IsValidDvFramePacket(Buffer)) != NULL ) { //std::cout << "DPlus DV frame" << std::endl; // handle it OnDvFramePacketIn(Frame); } else if ( (Header = IsValidDvHeaderPacket(Buffer)) != NULL ) { //std::cout << "DPlus DV header:" << std::endl << *Header << std::endl; // callsign muted? if ( g_GateKeeper.MayTransmit(Header->GetMyCallsign(), Ip, PROTOCOL_DPLUS, Header->GetRpt2Module()) ) { // handle it OnDvHeaderPacketIn(Header, Ip); } else { delete Header; } } else if ( (LastFrame = IsValidDvLastFramePacket(Buffer)) != NULL ) { //std::cout << "DPlus DV last frame" << std::endl; // handle it OnDvLastFramePacketIn(LastFrame); } else if ( IsValidConnectPacket(Buffer) ) { std::cout << "DPlus connect request packet from " << Ip << std::endl; // acknowledge the request m_Socket.Send(Buffer, Ip); } else if ( IsValidLoginPacket(Buffer, &Callsign) ) { std::cout << "DPlus login packet from " << Callsign << " at " << Ip << std::endl; // callsign authorized? if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_DPLUS) ) { // acknowledge the request EncodeLoginAckPacket(&Buffer); m_Socket.Send(Buffer, Ip); // create the client CDplusClient *client = new CDplusClient(Callsign, Ip); // and append g_Reflector.GetClients()->AddClient(client); g_Reflector.ReleaseClients(); } else { // deny the request EncodeLoginNackPacket(&Buffer); m_Socket.Send(Buffer, Ip); } } else if ( IsValidDisconnectPacket(Buffer) ) { std::cout << "DPlus disconnect packet from " << Ip << std::endl; // find client CClients *clients = g_Reflector.GetClients(); CClient *client = clients->FindClient(Ip, PROTOCOL_DPLUS); if ( client != NULL ) { // remove it clients->RemoveClient(client); // and acknowledge the disconnect EncodeDisconnectPacket(&Buffer); m_Socket.Send(Buffer, Ip); } g_Reflector.ReleaseClients(); } else if ( IsValidKeepAlivePacket(Buffer) ) { //std::cout << "DPlus keepalive packet from " << Ip << std::endl; // find all clients with that callsign & ip and keep them alive CClients *clients = g_Reflector.GetClients(); int index = -1; CClient *client = NULL; while ( (client = clients->FindNextClient(Ip, PROTOCOL_DPLUS, &index)) != NULL ) { client->Alive(); } g_Reflector.ReleaseClients(); } else { std::cout << "DPlus packet (" << Buffer.size() << ")" << std::endl; } } // handle end of streaming timeout CheckStreamsTimeout(); // handle queue from reflector HandleQueue(); // keep client alive if ( m_LastKeepaliveTime.DurationSinceNow() > DPLUS_KEEPALIVE_PERIOD ) { // HandleKeepalives(); // update time m_LastKeepaliveTime.Now(); } }