foo_catnap 源码分析
foo_catpap 是一个 foobar2000 插件,用于通过 http 协议的方式控制播放器。
此工会使用了 Poco 库,使用了 Poco::Net 模块,简单的做一个 http 服务器
CatNapServer.cpp
#include "stdafx.h"
#include "CatNapServer.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerParams.h"
#include <iostream>
#include "CatNapRequestHandlerFactory.h"
using Poco::Net::HTTPServer;
using Poco::Net::HTTPServerParams;
namespace CatNap {
CatNapServer::CatNapServer() : server(NULL)
{
}
void CatNapServer::start()
{
unsigned short port = 20402; // 0x4FB2
HTTPServerParams *params = new HTTPServerParams();
server = new HTTPServer(new CatNapRequestHandlerFactory(), port, params);
server->start();
}
void CatNapServer::stop()
{
if (server != NULL)
{
server->stop();
delete server;
}
}
} // namespace CatNap
CatNapRequestHandlerFactory.cpp
#include "stdafx.h"
#include "CatNapRequestHandlerFactory.h"
#include "RootRequestHandler.h"
#include "PlaybackEventsRequestHandler.h"
#include "PlaylistsRequestHandler.h"
#include "PlaylistsEventsRequestHandler.h"
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPServerRequest;
namespace CatNap {
CatNapRequestHandlerFactory::CatNapRequestHandlerFactory()
{
}
HTTPRequestHandler* CatNapRequestHandlerFactory::createRequestHandler(const HTTPServerRequest& request)
{
if (request.getURI() == "/")
return new RootRequestHandler();
else if (request.getURI() == "/playback/events")
return new PlaybackEventsRequestHandler(playbackFacade);
else if (request.getURI() == "/playlists")
return new PlaylistsRequestHandler();
else if (request.getURI() == "/playlists/events")
return new PlaylistsEventsRequestHandler();
else
return 0;
}
} // namespace CatNap
简单的生成一个 http 服务器,使用工厂来路由。