Example #1
0
inline void
SkyLinesTracking::Client::OnTrafficReceived(const TrafficResponsePacket &packet,
        size_t length)
{
    if (length < sizeof(packet))
        return;

    const unsigned n = packet.traffic_count;
    const ConstBuffer<TrafficResponsePacket::Traffic>
    list((const TrafficResponsePacket::Traffic *)(&packet + 1), n);

    if (length != sizeof(packet) + n * sizeof(list.front()))
        return;

    for (const auto &traffic : list)
        handler->OnTraffic(FromBE32(traffic.pilot_id),
                           FromBE32(traffic.time),
                           ImportGeoPoint(traffic.location),
                           (int16_t)FromBE16(traffic.altitude));
}
Example #2
0
void PlayResource (const TCHAR* lpName) {
    LKASSERT(lpName);
    
    if(!lpName || !bSoundFile || !bSoundInit || !EnableSoundModes) {
        return;
    }
    
    // Check if AudioChunk is already loaded.
    auto ib = audioChunkCache.insert(std::make_pair(lpName, nullptr));
    if(ib.second) {
        // Load AudioChunk
        ConstBuffer<void> sndBuffer = GetNamedResource(lpName);
        if(!sndBuffer.IsEmpty()) {
            SDL_RWops* SndBuffer = SDL_RWFromConstMem(sndBuffer.data, sndBuffer.size);
            if(SndBuffer) {
                ib.first->second = Mix_LoadWAV_RW(SndBuffer, 1);
            }
        }
    }
    
    if(ib.first->second) {
        Mix_PlayChannel(-1, ib.first->second, 0);
    }
}
Example #3
0
std::size_t Packet::read(const ConstBuffer& buf) 
{			
	// https://github.com/LearnBoost/socket.io-spec#Encoding

	// Reset all data
	_type = Packet::Message;
	_id = 0;
	_endpoint = "";
	_message = "";
	_size = 0;

	if (buf.size() < 3)
		return 0;

	std::string data(bufferCast<const char*>(buf), buf.size());	
	std::vector<std::string> frags = util::split(data, ':', 4);
	if (frags.size() < 1) {
		//DebugLS(this) << "Reading: Invalid Data: " << frags.size() << endl;
		return false;
	}
		
	if (!frags[0].empty()) {
		_type = util::strtoi<UInt32>(frags[0]);
		//DebugLS(this) << "Reading: Type: " << typeString() << endl;
	}

	if (_type < 0 || _type > 7) {
		//DebugLS(this) << "Reading: Invalid Type: " << _type << endl;
		return false;
	}
	if (frags.size() >= 2 && !frags[1].empty()) {
		_ack = (frags[1].find('+') != std::string::npos);
		_id = util::strtoi<UInt32>(frags[1]);
	}	
	if (frags.size() >= 3 && !frags[2].empty()) {
		_endpoint = frags[2];
	}
	if (frags.size() >= 4 && !frags[3].empty()) {
		_message = frags[3];
	}

	// For Ack packets the ID is at the start of the message
	if (_type == 6) {
		_ack = true; // This flag is mostly for requests, but we'll set it anyway

		std::string data(frags[frags.size() - 1]);
		std::string::size_type pos = data.find('+');
		if (pos != std::string::npos) 
		{	// complex ack
			_id = util::strtoi<UInt32>(data.substr(0, pos));
			_message = data.substr(pos + 1, data.length());
		}
		else
		{	// simple ack
			_message = data;
		}

#if 0
		frags.clear();
		util::split(_message, '+', frags, 2);
		if (frags.size() != 2) {
			assert(frags.size() == 2 && "invalid ack response");
			return false;
		}

		_ack = true; // This is mostly for requests, but we'll set it anyway
		_id = util::strtoi<UInt32>(frags[0]);
		_message = frags[1];
#endif
	}

	_size = data.length();
	//DebugLS(this) << "Parse success: " << toString() << endl;

	return _size;
}
std::size_t Packet::read(const ConstBuffer& buf)
{
    DebugN(this) << "Read raw packet: " << buf.str() << endl;
    assert(buf.size() > 0);

    // Reset all data
    _frame = Frame::Unknown;
    _type = Type::Unknown;
    _id = -1;
    _nsp = "/";
    _message = "";
    _size = -1;

    BitReader reader(buf);

    // look up frame type
	  char frame[2] = {'\0'};
    reader.get(frame, 1);
    _frame = static_cast<Packet::Frame>(atoi(frame));//alternative: std::stoi(std::string(frame, 1))

    DebugN(this) << "Read raw packet: atoi(&frame): " << atoi(frame) << endl;
    if (_frame == Packet::Frame::Message) {

        // look up packet type
		    char type[2] = {'\0'}; //; //
        reader.get(type, 1);
        _type = static_cast<Packet::Type>(atoi(type));//std::stoi(std::string(type, 1))
        // if (_type < TypeMin || _type > TypeMax) {
        //     WarnN(this) << "Invalid message type: " << _type << endl;
        //     return false;
        // }

        // TraceN(this) << "Parse type: " << type << ": " << typeString() << endl;
    }

    // look up attachments if type binary (not implemented)

    // look up namespace (if any)
    if (reader.peek() == '/') {
        reader.readToNext(_nsp, ',');
    }

    // look up id
    if (reader.available() && isdigit(reader.peek())) {
        char next;
        std::string id;
        reader.get(&next, 1);
        while (reader.available() && isdigit(next)) {
            id += next;
            reader.get(&next, 1);
        }
        _id = util::strtoi<int>(id);
    }

    // look up json data
    // TODO: Take into account joined messages
    if (reader.available()) {
        std::string temp;
        reader.get(temp, reader.available());

        json::Value json;
        json::Reader reader;
        if (reader.parse(temp, json)) {
            if (json.isArray()) {
                if (json.size() < 2) {
                    _event = "message";
                    _message = json::stringify(json[0], true);
                }
                else {
                    assert(json[0].isString());
                _event = json[0].asString();
                _message = json::stringify(json[1], true);

                }
            }
            else if (json.isObject()) {
                _message = json::stringify(json, true);
            }
        }
    }

    _size = reader.position();

    // DebugN(this) << "Parse success: " << toString() << endl;

    return _size;
}
std::size_t Message::read(const ConstBuffer& buf) 
{
    return read(buf.str()); // refactor
}
Example #6
0
std::size_t Message::read(const ConstBuffer& buf) //BitReader& reader
{
    TraceL << "Parse STUN packet: " << buf.size() << endl;

    try {
        BitReader reader(buf);

        // Message type
        UInt16 type;
        reader.getU16(type);
        if (type & 0x8000) {
            // RTP and RTCP set MSB of first byte, since first two bits are version,
            // and version is always 2 (10). If set, this is not a STUN packet.
            WarnL << "Not STUN packet" << endl;
            return 0;
        }

        //UInt16 method = (type & 0x000F) | ((type & 0x00E0)>>1) |
        //	((type & 0x0E00)>>2) | ((type & 0x3000)>>2);

        UInt16 classType = type & 0x0110;
        UInt16 methodType = type & 0x000F;

        if (!isValidMethod(methodType)) {
            WarnL << "STUN message unknown method: " << methodType << endl;
            return 0;
        }

        _class = classType; // static_cast<UInt16>(type & 0x0110);
        _method = methodType; // static_cast<UInt16>(type & 0x000F);

        // Message length
        reader.getU16(_size);
        if (_size > buf.size()) {
            WarnL << "STUN message larger than buffer: " << _size << " > " << buf.size() << endl;
            return 0;
        }

        // TODO: Check valid method
        // TODO: Parse message class (Message::State)

        // Magic cookie
        reader.skip(kMagicCookieLength);
        //std::string magicCookie;
        //reader.get(magicCookie, kMagicCookieLength);

        // Transaction ID
        std::string transactionID;
        reader.get(transactionID, kTransactionIdLength);
        assert(transactionID.size() == kTransactionIdLength);
        _transactionID = transactionID;

        // Attributes
        _attrs.clear();
        //int errors = 0;
        int rest = _size;
        UInt16 attrType, attrLength, padLength;
        assert(int(reader.available()) >= rest);
        while (rest > 0) {
            reader.getU16(attrType);
            reader.getU16(attrLength);
            padLength =  attrLength % 4 == 0 ? 0 : 4 - (attrLength % 4);

            auto attr = Attribute::create(attrType, attrLength);
            if (attr) {
                attr->read(reader); // parse or throw
                _attrs.push_back(attr);

                // TraceL << "Parse attribute: " << Attribute::typeString(attrType) << ": " << attrLength << endl; //  << ": " << rest
            }
            else
                WarnL << "Failed to parse attribute: " << Attribute::typeString(attrType) << ": " << attrLength << endl;

            rest -= (attrLength + kAttributeHeaderSize + padLength);
        }

        TraceL << "Parse success: " << reader.position() << ": " << buf.size() << endl;
        assert(rest == 0);
        assert(reader.position() == _size + kMessageHeaderSize);
        return reader.position();
    }
    catch (std::exception& exc) {
        DebugL << "Parse error: " << exc.what() << endl;
    }

    return 0;
}
Example #7
0
ssize_t Packet::read(const ConstBuffer& buf)
{
    LTrace("Read raw packet: ", buf.str())
    assert(buf.size() > 0);

    // Reset all data
    _frame = Frame::Unknown;
    _type = Type::Unknown;
    _id = -1;
    _nsp = "/";
    _message = "";
    _size = 0;

    BitReader reader(buf);

    // parse frame type
    char frame[2] = {'\0'};
    reader.get(frame, 1);
    _frame = static_cast<Packet::Frame>(atoi(frame)); // std::stoi(std::string(frame, 1))

    if (_frame == Packet::Frame::Message) {

        // parse packet type
        char type[2] = {'\0'};
        reader.get(type, 1);
        _type = static_cast<Packet::Type>(atoi(type)); // std::stoi(std::string(type, 1))
        // if (_type < TypeMin || _type > TypeMax) {
        //     LWarn("Invalid message type: ", _type)
        //     return false;
        // }

        // LTrace("Parse type: ",  type,  ": ", typeString())
    }

    // parse attachments if type binary (not implemented)

    // parse namespace (if any)
    if (reader.peek() == '/') {
        reader.readToNext(_nsp, ',');
    }

    // parse id
    reader.readNextNumber((unsigned int&)_id);

    // parse json data
    // TODO: Take into account joined messages
    if (reader.available()) {
        std::string temp;
        reader.get(temp, reader.available());

        json::value json = json::value::parse(temp.begin(), temp.end());
        if (json.is_array()) {
            if (json.size() < 2) {
                _event = "message";
                _message = json[0].dump();
            } else {
                assert(json[0].is_string());
                _event = json[0].get<std::string>();
                _message = json[1].dump();
            }
        } 
        else if (json.is_object()) {
            _message = json.dump();
        }
    }

    _size = reader.position();

    // LDebug("Parse success: ", toString())

    return _size;
}