Giter Site home page Giter Site logo

newyaroslav / simple-named-pipe-server Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 276 KB

Очень простая серверная и клиентская библиотека Named Pipe, реализованная с использованием C++11

License: MIT License

C++ 59.41% MQL4 3.15% MQL5 37.44%
named-pipe client server win32 cpp cpp11 mingw simple-named-pipe-server easy mql4

simple-named-pipe-server's Introduction

simple-named-pipe-server

Очень простая, многопоточная серверная и клиентская библиотека Named Pipe, реализованная с использованием C++11.

Проект был проверен на компиляторе mingw 7.3.0 x64. Папка code_blocks содержит примеры для IDE Code::Blocks. Не забудьте в проектах указать свой компилятор, иначе проект не соберется.

Особенности использования Named Pipe сервера

  • Для отправки сообщения всем клиентам используйте метод 'send_all'.
  • Чтобы отправить сообщение конкретному клиенту, используйте метод 'send' клиента, указатель на которого передается в функции обратного вызова в момент наступления события 'on_open' или 'on_message'.
  • Чтобы узнать количество подключений, используйте метод 'get_connections()'.

Важные фиксы

  • Методы 'get_connections()' и 'send_all' можно вызывать внутри 'on_open', 'on_message', 'on_close', 'on_error'

Пример сервера на C++

#include <iostream>
#include "named-pipe-server.hpp"

int main() {
    /* в конструкторе сервера можно также задать размер буфера */
    SimpleNamedPipe::NamedPipeServer server("my_server");

    /* обработчики событий */
    server.on_open = [&](SimpleNamedPipe::NamedPipeServer::Connection* connection) {
        std::cout << "open, handle: " << connection->get_handle() << std::endl;
    };

    server.on_message = [&](SimpleNamedPipe::NamedPipeServer::Connection* connection, 
			const std::string &in_message) {
        /* обрабатываем входящие сообщения */
        std::cout << "message " << in_message << ", handle: " << connection->get_handle() << std::endl;
        connection->send("ok");
    };

    server.on_close = [&](SimpleNamedPipe::NamedPipeServer::Connection* connection) {
        std::cout << "close, handle: " << connection->get_handle() << std::endl;
    };

    server.on_error = [&](SimpleNamedPipe::NamedPipeServer::Connection* connection, const std::error_code &ec) {
        std::cout << "error, handle: " << connection->get_handle() << ", what " << ec.value() << std::endl;
    };

    /* запускаем сервер */
    server.start();
    std::system("pause");

    /* останавливаем сервер 
     * (деструктор класса сам выполнит остановку, вызывать не обязательно)
     */
    server.stop();
    std::cout << "close program" << std::endl;
    return EXIT_SUCCESS;
}

Пример клиента на C++

#include <iostream>
#include "named-pipe-client.hpp"

using namespace std;

int main() {
    /* в конструкторе клиента можно также задать размер буфера */
    SimpleNamedPipe::NamedPipeClient client("my_server");

    /* обработчики событий */
    client.on_open = [&]() {
	std::cout << "open, handle: " << client.get_handle() << std::endl;
	client.send("Hello!");
    };

    client.on_message = [&](const std::string &in_message) {
	std::cout << "message " << in_message << ", handle: " << client.get_handle() << std::endl;
	client.send("ok");
	//client.close(); // можно закрыть соединение
    };
	
    client.on_close = [&]() {
	std::cout << "close, handle: " << client.get_handle() << std::endl;
    };
	
    client.on_error = [&](const std::error_code &ec) {
	std::cout << "error, handle: " << client.get_handle() << ", what " << ec.value() << std::endl;
    };

    /* запускаем клиент */
    client.start();
	
    std::system("pause");
    std::cout << "close program" << std::endl;
    return EXIT_SUCCESS;
}

Пример клиента для Meta Trader 5

//+------------------------------------------------------------------+
//|                                            named_pipe_client.mq5 |
//|                                     Copyright 2021, NewYaroslav. |
//|          https://github.com/NewYaroslav/simple-named-pipe-server |
//+------------------------------------------------------------------+
#include "..\include\named_pipe_client.mqh"

input string pipe_name      = "test-pipe"; // Named Pipe for the stream of quotes
input int    timer_period   = 10; // Timer period for processing incoming messages

NamedPipeClient     *pipe           = NULL;

//+------------------------------------------------------------------+
//| NamedPipeClient function                                   |
//+------------------------------------------------------------------+
void NamedPipeClient::on_open(NamedPipeClient *pointer) {
    Print("open connection with ", pointer.get_pipe_name());
}

void NamedPipeClient::on_close(NamedPipeClient *pointer) {
    Print("closed connection with ", pointer.get_pipe_name());
}

void NamedPipeClient::on_message(NamedPipeClient *pointer, const string &message) {
    Print("message: " + message);
}

void NamedPipeClient::on_error(NamedPipeClient *pointer, const string &error_message) {
    Print("Error! What: " + error_message);
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
    if (pipe == NULL) {
        if ((pipe = new NamedPipeClient(pipe_name)) == NULL) return(false);
    }

    EventSetMillisecondTimer(timer_period);
    return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
    EventKillTimer();
    if (pipe != NULL) delete pipe;
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {

}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer() {
    static int ticks = 0;
    if (pipe != NULL) pipe.on_timer();
    
    ticks += timer_period;
    if (ticks >= 1000) {
        ticks = 0;

        if (pipe != NULL) {
            if (pipe.connected()) {
                const string str_ping = "ping";
                pipe.write(str_ping);
            } // if (pipe.connected())
        } // if (pipe != NULL)
    } // if (ticks >= 1000)
}
//+------------------------------------------------------------------+

simple-named-pipe-server's People

Contributors

newyaroslav avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jojodav21

simple-named-pipe-server's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.