Author: Hu Huisheng Xie xiaofang
In the design of a new Fully Distributed Ship command and control system, the communication structure shown in Figure 1 needs to be implemented. The line in the figure indicates the possible connection of the system during work, it can be dynamically connected or disconnected during system operation. The core problem of implementing the system is to design a universal communication interface module.
Figure 1 communication structure of a fully distributed system
Windows NT provides two main network programming interfaces: pipelines and Windows Sockets. The purpose of Windows Sockets is to block network details. programmers do not need to care about the specific implementation and protocols of the network. Although Windows Socket only defines an abstract description of the TCP/IP protocol, any protocol family can follow Windows Sockets as long as it provides a dynamic Connection Library for the implementation.
In view of the operating characteristics of the system, the customer/Server mode of Windows Sockets is used for network communication. Radar detectors, artillery devices, and other weapons and equipment are used as servers, and they only wait for connection requests from the display console instead of actively requesting connection. The display consoles are both servers and clients, you can send connection requests to any unit at any time, and accept connection requests from other display consoles at any time. These network functions are implemented by designing a communication Thread class.
1. Implementation of the Communication Thread class ccomthread class
In Visual C ++'s MFC, The casyncsocket class describes the basic Windows Sockets programming interface. The csocket class is derived from the casyncsocket class, it abstracts sockets at a higher level and can use the carchive class of MFC to make inter-process communication similar to file serialization in MFC. Therefore, use the csocket class to implement the communication Thread class. The communication Thread class is derived from the cwinthread class. The following describes the basic steps for its implementation:
1.1 derive the clisteningsocket class and
Cconnectedsocket class
The clisteningsocket class object is used by the server to listen for network connection requests. Its main member is the pointer m_pcomthread of the Communication thread, which is assigned a value in the constructor to send messages to the communication thread. This class reloads the onaccept () notification function of the base class, which notifies the communication thread when there is a network connection request.
Cconnectedsocket objects are used to represent communication tasks with established connections. Its main members are: the pointer of the Communication thread and the local window pointer with the connection are used to send various messages; the socket number is used to identify the connected socket in the communication thread; two file object pointers are used to receive and send data for the communication connection. The main methods are: Initialize the init () function to specify the communication thread pointer and the local window pointer, and reload the onreceive () function to notify the communication thread to receive data; the overload base class onclose () function is used to notify the communication thread that the connection is established or has been disconnected.
1.2 derive the cmsg class and cmsghead class from the cobject class
The cmsg class is used to describe the message base class. Any data messages transmitted online must be derived from this class. The data member is identified by the message name, and the specific data message class must have the data member to be transmitted. The method is a serialization function, which is used to serialize the message name identifier. The specific data message class needs to overload the serialization function, call the serialization functions of the base class and serialize other data members to be transmitted.
The cmsghead class is used to describe the message header class. Before sending data, the communication thread must first send the message header object to identify the data message type to be sent. Its members and methods are similar to the cmsg class.
1.3 Main data member of the ccomthread class
The clisteningsocket Class Object Pointer is m_plisteningsocket. When a communication thread acts as a server, it listens for network connection requests. The pointer linked list m_connectedsocketlist is used to save the cconnectedsocket object pointer with established connections, the communication thread searches for specific communication connections based on the linked list to send data. The m_pipenumber counter identifies the socket Number of the current established connection. m_pwnd is used to save the server window pointer. Several temporary object pointers are used to pass the message object pointer and the temporary cconnectedsocket Object Pointer used to receive or request the connection.
1.4 Implementation of main ccomthread Methods
Createsocket () creates a socket message processing function: when a communication thread user wants to become a server, it sends a socket message to the communication thread. In this message processing function, the communication thread constructs the m_plisteningsocket object based on the specified port number and starts listening.
Connectsocket () Connection Request message processing function: the client window sends this message to the server to send a connection request. In this function, the communication thread constructs a cconnectedsocket object and sends a connection request based on the specified server address and port number. If the connection is successful, the counter is counted, set the socket number, add the socket to the socket linked list, and notify the customer window of successful connection and the socket Number of the connection.
Processpendingaccept () accepts the connection method: This function is called when the server's listening socket receives the connection request. The Code is as follows:
Cconnectedsocket * psocket = new cconnectedsocket (this );
If (m_plisteningsocket-> Accept (* psocket ))
{
M_pipenumber ++;
Psocket-> Init (m_pipenumber, m_sourcewnd );
M_connectedsocketlist.addtail (psocket );
M_pwnd-> postmessage (mm_listen_accept, m_pipenumber, null );
}
Else Delete psocket;
Sendmsg (): When the communication Party sends data, it sends a data message to the communication thread to pass the message object pointer and socket number. The message processing function code is as follows:
Void ccomthread: sendmsg (cmsg * PMSG, uint npipeid)
{
For (positionpos = m_connectedsocketlist.
Getheadposition (); pos! = NULL ;)
{
Cconnectedsocket * psocket
= (Cconnectedsocket *) m_connectedsocketlist. getnext (POS );
If (psocket-> m_pipeid = npipeid)
{
Cmsghead msghead (PMSG-> m_msgname );
Msghead. serialize (* (psocket-> m_parchiveout ));
PMSG-> serialize (* (psocket-> m_parchiveout ));
Psocket-> m_parchiveout-> flush ();
Pos = NULL;
}
}
}
Processpendingread () method of receiving data: This method is called when a socket in the linked list of a socket has data waiting for receiving. This method is called first:
Msghead. serialize (* (psocket-> m_parchivein ));
Read the message header, construct a message Class Object Based on the message name in the message header, and then call: m_pmsg-> serialize (* (psocket-> m_parchivein ));
Read the message object and call the following code:
Psocket-> m_pwnd-> postmessage (mm_receive_msg, (long) msgname, (long) m_pmsg );
Receive data in the target window of the notification data transmission.
Deleteconnect () Disconnection handler: This message handler is called when the communication party wants to disconnect or the other party is disconnected. In the function, the communication thread searches for and deletes the socket in the connected socket list based on the socket number specified for disconnection.
2 Conclusion
The communication thread implemented by this method has the advantages of simple programming and reliable operation. It can basically meet the design requirements and better meet the universal requirements.