开源程序LAN_share 单例模式巧用

以前写单例模式时,考虑的单个类的单例,看看下面的代码如何实现单例类和主程序分离,便于复用
作者主要使用QLocalSocket和QLocalServer进行程序的注册和监听,所以再次有同名称的程序打看,就会给之前的程序发消息,令其主动弹出,而自身则悄然关闭,实现了单例运行程序。

singleinstance.h
/*
    LANShare - LAN file transfer.
    Copyright (C) 2016 Abdul Aris R. <abdularisrahmanudin10@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef SINGLEINSTANCE_H
#define SINGLEINSTANCE_H

#include <QObject>
#include <QtNetwork/QLocalServer>

/*
 * SingleInstance, digunakan agar aplikasi hanya memiliki satu instance
 * yang berjalan disistem operasi
 */
class SingleInstance : public QObject
{
    Q_OBJECT

public:
    SingleInstance(const QString& id, QObject* parent = 0);
    ~SingleInstance();

    QString getLastErrorString() const;
    bool start();
    bool hasPreviousInstance();

Q_SIGNALS:
    void newInstanceCreated();

private Q_SLOTS:
    void onNewConnection();

private:

    QLocalServer mServer;
    QString mName;
};

#endif // SINGLEINSTANCE_H

singleinstance.cpp
/*
    LANShare - LAN file transfer.
    Copyright (C) 2016 Abdul Aris R. <abdularisrahmanudin10@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtNetwork/QLocalSocket>

#include "singleinstance.h"

SingleInstance::SingleInstance(const QString& name, QObject* parent)
    : QObject(parent), mName(name)
{
    connect(&mServer, &QLocalServer::newConnection, this, &SingleInstance::onNewConnection);
}

SingleInstance::~SingleInstance()
{
}

QString SingleInstance::getLastErrorString() const
{
    return mServer.errorString();
}

bool SingleInstance::start()
{
    mServer.removeServer(mName);
    return mServer.listen(mName);
}

bool SingleInstance::hasPreviousInstance()
{
    QLocalSocket socket;
    socket.connectToServer(mName);

    return socket.waitForConnected();
}

void SingleInstance::onNewConnection()
{
    emit newInstanceCreated();
}

这样我们只要需要单例的地方加上这句话,就可以实现对象的单例

    QObject::connect(&si, &SingleInstance::newInstanceCreated, [&mainWindow]() {
        mainWindow.setMainWindowVisibility(true);
    });

最后要了解这个QLocalSocket和QLocalServer是个啥?他们是进程通信用到的类。

进程通信(IPC)的方法有很多,项目开发中,需要根据业务需求来选择适合的IPC方式。所谓LocalSocket,其实就是在socket的基础上衍生出来的一种IPC通信机制。其旨在解决同一台主机上不同进程间互相通信的问题,不能像网络通信使用的socket一样实现不同主机间通信。但正因为这一点,它不需要经过网络协议栈,不需要打包拆包、计算校验,所以执行效率要更高。
参考网址:https://blog.csdn.net/m0_46577050/article/details/123499117

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可峰科技

生活不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值