Use ASYNCIPC communication between PPAPI and browser

Source: Internet
Author: User
Tags skia

Use ASYNCIPC this project (HTTPS://GITHUB.COM/HICDRE/ASYNCIPC) to complete PPAPI plugin process communication with the browser process.

Foruok original, if you need to reprint please pay attention to Foruok subscription number "program Horizon" contact Foruok.

The IPC implementation of the ASYNCIPC is based on the pipe, which is referenced by the IPC code of the chromium, and is suitable for Windows. Chromium uses a similar IPC mechanism between the render and browser processes, each render will establish a two-way channel with browser. ASYNCIPC uses a similar concept and abstracts the channel for interprocess communication.

Basic usage

Describes how to integrate ASYNCIPC and key class libraries.

Introduction method

ASYNCIPC can be compiled into static libraries and dynamic libraries. You can also add the source code directly to the project as needed.

Class Library

AYNCIPC defines a namespace IPC. The ipc::endpoint represents an access point that needs to be created when used. When there is a peer-to-peer process access, endpoint sends some notifications that are sent through the Ipc::listener interface, so the listener interface can be implemented.

The constructor prototypes for the endpoint class are as follows:

Endpoint(const std::string& name, Listener* listener, bool start_now);

It needs a listener, so we need to implement the listener interface first. Ipc::listener is defined as follows:

class Listener { public:  // Called when a message is received.  Returns true iff the message was  // handled.  virtual bool OnMessageReceived(Message* message) = 0;  // Called when the channel is connected and we have received the internal  // Hello message from the peer.  virtual void OnChannelConnected(int32 peer_pid) {}  // Called when an error is detected that causes the channel to close.  // This method is not called when a channel is closed normally.  virtual void OnChannelError() {} protected:  virtual ~Listener() {}};

We derive classes from listener, implementing Onmessagereceived, onchannelconnected, and onchannelerror three methods to process IPC messages and states.

Now we just need one line of code to create an access point to wait for the connection:

m_endPoint = new IPC::Endpoint("ppapi_ipc", this);

Note that the this pointer represents a class that implements the Ipc::listener interface, and the class declares the following:

class IPCImageClient : public IPC::Listener{public:    IPCImageClient();    virtual bool OnMessageReceived(IPC::Message* msg);    virtual void OnChannelConnected(int32 peer_pid);    virtual void OnChannelError();private:    void SendRawImage();    void SendDecodedImage();protected:    int m_peerPid;    IPC::Endpoint *m_endPoint;};

The message class is used when sending messages, receiving or processing messages. The code to send the message resembles the following:

    scoped_ref_ptr<IPC::Message> msg(new IPC::Message(GetCurrentProcessId(), 101, (IPC::Message::PriorityValue)0));    msg->WriteBytes(data, size);    m_endPoint->Send(msg.get());

The code that receives the message resembles this:

bool IPCImageClient::OnMessageReceived(IPC::Message* msg){    uint32 type = msg->type();    switch (type)    {    case 1: // data was consumed,write again        OutputDebugString(_T("SendImage to plugin\r\n"));        SendDecodedImage();        break;    }    return true;}

or this:

bool IPCSimpleClient::OnMessageReceived(IPC::Message* msg){    const void *data = msg->payload();;    unsigned int len = msg->payload_size();    UIImage *imageView = (UIImage*)m_view;    uint32 type = msg->type();    switch (type)    {    case 100: // decoded image data        {            OutputDebugString(_T("received decoded image\r\n"));            SkBitmap received;            SkImageInfo info = SkImageInfo::Make(1280, 720,                 kBGRA_8888_SkColorType, kPremul_SkAlphaType,                 kLinear_SkColorProfileType);            received.installPixels(info, (void*)data, info.minRowBytes());            imageView->cloneBitmapFrom(received);            imageView->requestPaint(NULL);        }    break;    ...    }}

To summarize, the steps to use ASYNCIPC are as follows:

    • Implementing the Ipc::listener Interface
    • Create a Ipc::endpoint object, pass the listener interface to it
    • Send a message when the connection is established (can be in the Onchannelconnected method)
    • Handling messages in the Listener::onmessagereceived method

That's it.

Other reference articles:

    • CEF Windows Development Environment Setup
    • CEF Load Ppapi Plugin
    • VS2013 compiling the simplest Ppapi plugin
    • Understanding the design of PPAPI
    • Ppapi plugin-to-browser interaction process
    • Windows compiled from the source CEF
    • Media_stream_video Example of compiling PPAPI
    • PPAPI plug-in drawing and input event handling
    • Creating a local window in the PPAPI plug-in
    • PPAPI plug-in communication with the browser
    • Skia compiled from source under Windows
    • Using Skia drawing in the Ppapi plug-in
    • Load a picture resource in a DLL to generate a Skbitmap object in Skia
    • Ppapi+skia implementation of Graffiti Board
    • 3D graphics interface using chromium in PPAPI
    • Using OpenGL ES drawing in Ppapi
    • JS in CEF interacts with C + +
    • Communication between browser process and render process in CEF
    • Multi-process model and related parameters of chromium and CEF

Use ASYNCIPC communication between PPAPI and browser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.