Exemplo n.º 1
0
void WSStreamerHandler::on_open(websocketpp::session_ptr client) {
	if (client->get_resource() != "/") {
		shared_ptr<ifstream> resource(new ifstream(client->get_resource().substr(1).c_str(), ios::binary));
		if (resource->is_open()) {
			cout << "INFO: Client has connected and opened " + client->get_resource() << endl;
			// Check if it is a rtpdump file. If it is, fast foward past file header
			if (resource->good()) {
				streampos filestart = resource->tellg();
				char* rtpdumphdr = new char[strlen(RTPPLAY_MAGIC)];
				resource->read(rtpdumphdr, strlen(RTPPLAY_MAGIC));
				if (resource->gcount() == strlen(RTPPLAY_MAGIC) && strncmp(rtpdumphdr, RTPPLAY_MAGIC, strlen(RTPPLAY_MAGIC)) == 0) {
					cout << "INFO: Requested file is an rtpdump file. Fast forwarding past file header" << endl;
					resource->ignore(sizeof(RD_hdr_t));
				}
				else {
					// Reset file to beginning if we do not see rtpdump header
					resource->seekg(filestart);
				}
				delete[] rtpdumphdr;
			}
			WSSClientInfo clientInfo;
			clientInfo.resource = resource;
			connections.insert(pair<websocketpp::session_ptr, WSSClientInfo>(client, clientInfo));
		}
		else {
			cerr << "ERROR: Client has connected but server is unable to access " + client->get_resource() << endl;
			client->send("ERROR: Failed to open resource");
		}
	}
}
Exemplo n.º 2
0
void WSStreamerHandler::validate(websocketpp::session_ptr client) {
	// Check if requested resource exists
	if (client->get_resource() == "/") {
		cout << "INFO: Client is connecting without asking for a resource" << endl;
	}
	else {
		ifstream resource(client->get_resource().substr(1).c_str(), ios::binary);
		if (!resource.is_open()) {
			string err = "Request for unknown resource " + client->get_resource();
			cerr << err << endl;
			throw(websocketpp::handshake_error(err, 404));
		}
		else {
			cout << "INFO: Client request for " + client->get_resource() + " accepted" << endl;
			resource.close();
		}
	}
}
Exemplo n.º 3
0
int sendNAL(websocketpp::session_ptr client, shared_ptr<ifstream> file, int nals) {
	if (file->good()) {
		vector<unsigned char> buffer = readNAL(*file.get(), nals);
		client->send(buffer);
		//debug.write((char*)&buffer[0], buffer.size());
		return buffer.size();
	}
	else {
		return 0;
	}
}
Exemplo n.º 4
0
streamsize sendChunk(websocketpp::session_ptr client, shared_ptr<ifstream> file, int chunk_size) {
	if (file->good()) {
		char* read_buffer = new char[chunk_size];
		vector<unsigned char> send_buffer;
		file->read(read_buffer, chunk_size);
		send_buffer.assign(read_buffer, read_buffer + file->gcount());
		client->send(send_buffer);
		delete [] read_buffer;
		return file->gcount();
	}
	else {
		return 0;
	}
}
Exemplo n.º 5
0
streamsize sendRTP(websocketpp::session_ptr client, shared_ptr<ifstream> file, int packets) {
	if (file->good()) {
		// Read file header
		char packet_hdr_array[sizeof(RD_packet_t)];
		file->read(packet_hdr_array, sizeof(packet_hdr_array));
		RD_packet_t* packet_hdr = (RD_packet_t*)packet_hdr_array;
		packet_hdr->length = ntohs(packet_hdr->length);
		packet_hdr->plen = ntohs(packet_hdr->plen);
		packet_hdr->offset = ntohl(packet_hdr->offset);
		// Extract actual RTP packet
		char* rtp_packet = new char[packet_hdr->plen];
		file->read(rtp_packet, packet_hdr->plen);
		vector<unsigned char> send_buffer;
		send_buffer.assign(rtp_packet, rtp_packet + file->gcount());
		client->send(send_buffer);
		delete [] rtp_packet;
		return file->gcount();
	}
	else {
		return 0;
	}
}
Exemplo n.º 6
0
void data_server_handler::on_client_connect(websocketpp::session_ptr client){
	static const boost::regex nonolith_domain("^https?://[[:w:]\\.-]*?nonolithlabs.com$");
	static const boost::regex localhost_domain("^https?://localhost(:[[:d:]]+)?$");
	const string origin = client->get_client_header("Origin");

	if (!allowAnyOrigin 
			&& origin!=""
			&& origin!="null"
			&& !regex_match(origin, localhost_domain) 
			&& !regex_match(origin, nonolith_domain)
			){
		client->start_http(403, "Origin not allowed");
		std::cerr << "Rejected client with unknown origin " << origin << std::endl;
		return;
	}
	
	const std::string resource = client->get_resource();
	Url url(resource);
	UrlPath path(url, 1);

	try{
		if (path.leaf()){ // "/"
			client->set_header("Location", redir_url);
			client->start_http(301);
		}else if (path.matches("rest")){
			handleJSONRequest(path.sub(), client);
		}else if (path.matches("ws")){
			client->start_websocket();
		}else{
			client->start_http(404, "Not found");
		}
	}catch(std::exception& e){
		JSONNode j;
		j.push_back(JSONNode("error", e.what()));
		std::cerr << "Exception while processing request: " << e.what() <<std::endl;
		respondJSON(client, j, 500);
	}
}
Exemplo n.º 7
0
void echo_server_handler::on_message(websocketpp::session_ptr client,
	const std::vector<unsigned char> &data) {
	client->send(data);
}
Exemplo n.º 8
0
void echo_server_handler::on_message(websocketpp::session_ptr client, const std::string &msg) {
	client->send(msg);
}