About pipeline communication

xiaoxiao2021-03-06  17

Download Demo Project - 40 KB Download Source - 10 KB

Introduction

The main objective of this application was to learn about the various Inter Process Communication methods and how it can be used for data transfer. This application also helps us to decide which of the available IPC methods is best to use. For a description of these IPC mechanisms I have taken a structure pointer named EmployeeData whose pointer is passed from the client to the server. For simplicity this demo application describes one way inter-process communication (ie) from the client to the server. The server will just listen to its clients I Have Described 5 Types of IPCs.

1ClipBoardLocal Mc2File MappingLocal Mc3mailslotslocal MC / Intranet4named Pipeslocal MC / Intranet5 Socketslocal MC / Intranet / Internet

For Detailed Information About The Inter Process Communication, You Should Read MSDN Library / Platform SDK Documentation / Base Services / Interprocess Communications

CLIPBOARD

The clipboard is a set of functions and messages that enable applications to transfer data. Because all applications have access to the clipboard, data can be easily transferred between applications or within an application.

To Write in the clipboard

OpenClipboard Register Clipboard Format (optional) Use Global Allocation for the Data SetClipboardData CloseClipboard.

To read from the clipbaord

Openclipboard Register Clipboard Format (optional) Check The Clipboard Format Is Available Get The Clipboard Data Unlock The Handle

SDK functions are available for the above operations. CClipboard is a class that provides developers with a simple way to implement the clipboard functions. Write the data in the clipboard as easy as shown below.

Cclipboard M_ObjClipBoard;

m_objclipboard.open (); m_objclipboard.write ((char *) DATA, SIZE);

Read The Data from The Clipboard as Shown Below.

Cclipboard M_ObjClipBoard;

m_objclipboard.open ();

m_objclipboard.read (data, size);

Many Standard Clipboard Formats Are Available. If You Want To Support More Applications To Work On The Clipboard At The Same Time, The Register A New Clipboard Format. (Optional)

m_objclipboard.register ("new format") // ann name

In this Demo Application, The Client Notifies The Server by Posting Message with handle hwnd_broadcast.

File mapping

File mapping is an efficient way for two or more processes on the same computer to share data. Here i have not described about the synchronization of the process. In order to access the file's contents, the processes uses virtual address space called file view. Processes Read from AND Write to the file view using pointers, Just As They Would With Dynamical Allocated Memory.

To Write in the file

Create File Mapping Map The Handle To The Process Virtual Address Space (MapViewoffile) Copy The Data in The File

To Read from the file

Create File Mapping Maps The Handle To The Process Virtual Address Space (MapViewoffile) Copy The Data from The File.

CFileMapping is a class which encapsulates the file mapping functions provided by the SDK. Using the above class sharing the data between two or more processes can be easily done. Write the data in the shared file as shown below.

CFILEMAPPING M_OBJFILEMAPPING;

m_objfilemapping.initialize ("Any File Name", Size / * Size of the FileMap * /);

m_objfilemapping.write ((char *) DATA);

IF the name of the filemap is known. We can read the data from any other process as shown below.

CFILEMAPPING M_OBJFILEMAPPING;

m_objfilemapping.initialize ("any file name", size); m_objfilemapping.read (data);

Notify the server using HWND_BROADCAST handle. We have successfully transferred the pointer between two processes on the same local machine. We get curious of how to pass this pointer in the Intranet if this two processes resides on different computers. Next section is Named Pipes and Mailslots .

Mailslots

Any win32 application can store messages in a mailslot and the owner will retrieve messages that are stored there. Mailslot is a simple way for a process to broadcast messages to multiple processes using datagram. Hence there will not be any confirmation of the receipt. This IS A Mechanism for One Way Interpcess Communication.

To Write in the mailslot

Create a mailslot (createmailslot) Write data in the mailslot (Writefile)

To read from the mailslot

Open the mailslot (createfile) Check for the message in the mail slot (getmailslotinfo) Read data from the mailslot (readfile)

CMAILSLOT IS A Class That Can Be Used To Create Both Mail Slot Server As Well As Mail Slot Client.

Mailslot Server Mailslot ClientCMailslot m_objMailslot; CMailslot m_objMailslot; m_objMailslot.Initialize (true / * IsServer * /, "Any Mailslot Name"); m_objMailslot.Initialize (false / * IsServer * /, "Any Mailslot Name"); m_objMailslot.Listen (messageId , lparam); m_objmailslot.write (data) Fired to messageid m_objmailslot.read (data);

INTERNAL DETAILS: - A MAILSLOT Server IS A Process That Creates And Owns a mailslot. When the Server Creates a mailslot handle.

m_StrMailSlot.format (".//mailslot //% s", pmailslotname;

m_hinputslot = createmailslot (m_strmailslot, // mailslot name

0, // Message Can Be of any sizemailslot_wait_forever, // Waits Forever for a Message

Null // it cannot be inherited

);

The mailslot server can read messages in the mailslot at any time. For the automatic response of the server you can use Listen (messageId, lparam). Here the messageId is the RegisterWindowMessage identity. In the listen function, a worker thread is created to continuously check for messages in the mailslot. If any messages found, it fires to the messageId for the read operation. The total messages will be reduced only at the end of read operation. Hence in this function SendMessageTimeOut is used in order to decrement the message count Let us Check the Total Messages in the mailslot.

While (1) {

GetMailslotinfo (this-> GetInputslot (), NULL, & Message,

& noofMessages, NULLs;

IF (NOOFMESSAGES> 0) {

// The Total Messages Will Be Reduced Only At The end of read operation

// hence don't use post message operation.

Lresult returnval = :: seundMessagetimeout (hwnd_broadcast,

This-> GetTransferMessage (),

Null, this-> getlparam (), smto_block,

Messagetimeout, NULL);

IF (returnval == 0)

"" Unable to send message. Error IS% D ",

GetLastError ());

}

}

Read the mailslot message.. ..

While (NOOFMESSAGES! = 0) {

Readfile (M_HINPUTSLOT, BUFFER, SIZE, & DATAREAD, NULL);

Getmailslotinfo (M_HINPUTSLOT, // Mailslot Handle

0, // Message Can Be of any size

& Message, // Size of Next Message

& noofMessages, // Number of Messages

Null // read time-out interval

);

}

A mailslot client is a process that writes a message to a mailslot. Any process that has the name of a mailslot can put a message there. New messages follow any existing messages in the mailslot. The client opens the mailslot using CreateFile.m_strMailslot.Format ("* // mailslot //% s", pMailslotname);

M_HOUTPUTSLOT = CREATEFILE (m_strmailslot, // file name

Generic_Write, // Only Write Permission

0, // cannot be Shared

Null, // SD

Open_existing, // Opens the file. Failsiff

// file doesn't exist

FILE_ATTRIBUTE_NORMAL, // File Attributes

Null // Handle to Template File

);

Write the data in the mailslot is as simple as shown Below.

DWORD SIZEWRITTEN

Writefile (M_HOUTPUTSLOT, PDATA, M_NSIZE, & SizeWritten, NULL);

IF (SizeWritten == 0)

"" Unable to write to the mail slot. Error IS% D ",

GetLastError ());

Namedpipes

A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients Named pipes are like telephone calls:. You talk only to one party, but you know that the message is being received.

To Write in the PIPE.

Create a named pipe. (With or welch security attributes) Write the data in the file. (At the time of write the server limited

To read from the pipe

Open a named pipe. Continuously Read from The Pipe Notify That The Message Has Been Received.

CNAMEDPIPE IS A Simple Class Used to Support Named Pipe Operations.

Named Pipe Server Named Pipe ClientCNamedPipe m_objNamedPipe; CNamedPipe m_objNamedPipe; m_objNamedPipe.Initialize (true / * IsServer * /, / * Server name * /, "Any NamedPipe Name" "."); M_objNamedPipe.Initialize (false / * IsServer * /, "server" / * Server name * /, "Any NamedPipe name"); m_objNamedPipe.Listen (messageId, lparam); m_objNamedPipe.Write (data) fired to messageId m_objNamedPipe.Read (data); Call the Initialize function of the CNamedPipe to Create Named Pipe Server and named Pipe Client. a named Pipe Server, Refers to a Process That Creates a named pipe.

m_strpipename.format (".// Pipe //% S", PPIPENAME);

M_HINPUTPIPEHANDLE = CREATENAMEDPIPE

m_strpipename, // pipe name

PIPE_ACCESS_INBOUND, // Client To The Server

PIPE_WAIT, / / ​​BLOCKING MODE IS ENABED

1, // Support Given to Only One Client

0, // Output Buffer Size

0, // Input Buffer Size

PIPE_TIMEOUT, / / ​​TIME-OUT INTERVAL

NULL // Security Attributes

);

IF The Security Attribute Is Null, THE Transfer of Data Will Be Available To Only Local Machine. Inorder to Support Across The Network, Set The Security Attribute.

Security_Attributes Sapipe;

Sapipe.lpsecurityDescriptor = (psecurity_descriptor) Malloc

(Security_Descriptor_min_length);

InitializesecurityDescriptor (Sapipe.lpsecurityDescriptor,

Security_descriptor_revision;

// Access Control List is asiigned as null inorder to allow all access to

// the object.

SetSecurityDescriptOracl (Sapipe.lpsecurityDescriptor, true, (pacl) null, false;

Sapipe.nlength = sizeof (Sapipe);

Sapipe.binherithandle = true;

This created pipe will listen continuously for the client to write data. Inorder to satisfy this condition a worker thread is created. In the mailslot the infinite thread is an optional, we can give an external control to read from the mailslot. Since the pipe is created in the blocking mode, this infinite thread is necessary. Otherwise the client will get hanged while writing data. This thread named as Listenerproc will continuously read the file. If the data is read then it will notify to the messageId.UINT ListenerProc (LPVOID PNAMEDPIPE)

{

CNAMEDPIPE * THIS = (CNAMEDPIPE *) PNAMEDPIPE;

IF (this == NULL)

Return 0;

INT size = this-> getsize ();

// Assign the size of the data to be read.

AskERT (size! = 0);

Assert (this-> getTransferMessage ()! = Null);

Char * buff = new char [size];

DWORD DATAREAD;

While (1) {

Bool isread = readfile (this-> getInputpipeHandle (),

BUFF, SIZE, & DATAREAD, NULL

IF (DataRead> 0) {

// ACTS AS A POST THREAD MESSAGE

// Since this Is Internal to the process, IT Can Also

// Work on the network satisfying the named pipe advantage.

:: PostMessage (hwnd_broadcast,

This-> GetTransferMessage (), (WPARAM) BUFF, this-> getlparam ());

}

}

Return 1;

}

A Named Pipe Client, Opens the name. With the help of the handle. This procedure is same as in the mailslot. Please refer above.

Sockets

A socket is a communication endpoint - an object through which a Windows Sockets application sends or receives packets of data across a network.Here I have used a only a bit of portion for data transfer from the huge topic Window Sockets This application uses stream socket. . MFC class CSocket provides encapsulation of the windows socket API.Server SocketClient SocketConstruct a socketConstruct a socketCreate the socketCreate the socketStart listening <--- ConnectAccept the client <--- Send DataReceive data.

.

History

Version 1.0

About Venkat Raman

. Iam a software developer from Chennai, India I've been working mainly under windows environment.I am attracted to various programming languages ​​including:. C / C , Python I've been programming VC / MFC, ATL / COM, Lex & Yacc And Database Design / Development (SQL / Oracle) for the Past TWO Years. Click Here to View Venkat Raman's Online Profile.

转载请注明原文地址:https://www.9cbs.com/read-41288.html

New Post(0)