dms代码

该博客展示了如何使用C++实现一个基于UPnP的媒体服务器和Socket通信的示例。通过创建Socket监听连接,初始化UPnP设备,并处理dmsStartStop命令来启动和停止服务。代码中包含了错误处理和通信协议细节。
/*
 * date            20120510
 * last author:    baojinyu
 * usage:    successful as socket server
 *           successful as dlna media file server
 * */
#include "Neptune.h"
#include "Platinum.h"
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h> /* definations of sa_family structre,htons,INADDR_ANY,... */

#include <arpa/inet.h> /* name ip format */

#include <sys/types.h> /*give the defination of pid_t*/
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <stdlib.h>


#include <ctype.h> /* isspace()  */

#define BUFFER_MAX    1024  /* input buffer for commands */
#define TOKEN_MAX     8     /* max number of arguments in buffer */
#define REPLY_MAX     256   /* largest reply allowed */
#define MAX_QUE_CONN_NM    10

NPT_SET_LOCAL_LOGGER("platinum.media.server.file")

//example:NPT_Debug("ERROR: failed to load entity (%d)\n", result);

int sockfd;
static NPT_Flags deviceStartFlag = 0; //服务启动状态为1,服务停止则状态为0
PLT_UPnP upnp;
PLT_DeviceHostReference device;

int init_sock()
{
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        NPT_LOG_INFO("baojinyu---------------create socket error! \n");
        exit(1);
    }
    NPT_LOG_INFO("baojinyu---------------create socket successfully .\n");
    
    return 0;
}

int init_device()
{
    device = new PLT_FileMediaServer(
        "/data/local/1.3gp",//"/data/local/if.mp3", 
        "Platinum UPnP Media Server",
        false,
        "SAMEDEVICEGUID", // NULL for random ID
        (NPT_UInt16)1194);
    NPT_List<NPT_IpAddress> list;
    NPT_CHECK_SEVERE(PLT_UPnPMessageHelper::GetIPAddresses(list));
    NPT_String ip = list.GetFirstItem()->ToString();

    device->m_ModelDescription = "Platinum File Media Server";
    device->m_ModelURL = "http://www.plutinosoft.com/";
    device->m_ModelNumber = "1.0";
    device->m_ModelName = "Platinum File Media Server";
    device->m_Manufacturer = "Plutinosoft";
    device->m_ManufacturerURL = "http://www.plutinosoft.com/";

    upnp.AddDevice(device);
    NPT_CHECK_SEVERE(upnp.Start());//Just for test.
    
    return 0;
}

int init()
{
    init_sock();
    init_device();
    return 0;
}

//the following are commands.
static int HandleCmd_dmsStartStop(char **arg, char reply[REPLY_MAX])
{
    if (!deviceStartFlag){
        //start service
        NPT_LOG_INFO("baojinyu------------socket cmd Start。");
        NPT_CHECK_SEVERE(upnp.Start());
        NPT_LOG_INFO("baojinyu------------upnp Start success!");
        deviceStartFlag = 1;
    }
    else{
        //stop service
        NPT_LOG_INFO("baojinyu------------socket cmd stop。");
        upnp.Stop();
        NPT_LOG_INFO("baojinyu------------upnp stop success!");
        deviceStartFlag = 0;
    }
    return deviceStartFlag;

}

struct cmdinfo {
    const char *name;
    unsigned numargs;
    int (*func)(char **arg, char reply[REPLY_MAX]);
};

struct cmdinfo cmds[] = {
    //{ "isMouse",                 1, isMouse },
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值