DIOCP writing the first application (iii)

Source: Internet
Author: User

Client

The design features are as follows:

1. Establish a connection with the server

2, the request connection, the encryption password, uses the BASE64 code

3, Always send heartbeat to tell the server online

4, the related data processing and interaction

First step: Create a vcl-forms application (create a standard VCL program)

Step two: Write a common unit for communicating with the server, because the client typically consists of multiple forms, so a public connection class is required, cannot be placed into the form, or the form closes and the connection is closed

The following unit is a unit of egg, I simplified, reserved part for learning, this part is mainly to send the request to the server

Unit clientiocpoper;

Interface

Uses
Classes,

diocp_coder_tcpclient,//with the server to communicate the necessary units, of course, you use Indy to write the client will not need this unit

simplemsgpack,//Package Processing Unit

UDIOCPSTREAMCODER;//Encoder and Decoder unit

<summary>
Request Login
</summary>
<param name= "Auserid" > Account </param>
<param name= "Apaw" > Password </param>
Procedure Cmd_login (const Auserid, apaw:string);

<summary>

Initializing objects used by clients
</summary>
Procedure Iniclientobject;

<summary>
Destroying objects created by clients
</summary>
Procedure Uniniclientobject;

Var
curuserid:string;//Global Variables
codertcpclient:tdiocpcodertcpclient;//Global Communication Client
diocpcontext:tiocpcoderremotecontext;//Global Communication Diocpcontext
Implementation

Uses sysutils;

Var
Cmdobject:tsimplemsgpack;
Cmdstream:tmemorystream;

Procedure Sendcmdobject (Pvcmdobject:tsimplemsgpack);
Var
Lvcmdstream:tmemorystream;
Begin
Lvcmdstream: = tmemorystream.create;
Try
Pvcmdobject.encodetostream (Lvcmdstream); Encrypt messages
Diocpcontext.writeobject (Lvcmdstream);
Finally
Lvcmdstream.free;
End
End

Procedure Cmd_login (const Auserid, apaw:string);
Begin
Connection
Codertcpclient.open;
If Diocpcontext.active then Exit;
Diocpcontext.connect;
Sflogger.logmessage (' Connect with server successfully, please login ');
Online
If Diocpcontext.active then//connection successful, request login
Begin
Cmdobject.clear;
Cmdobject.forcepathobject (' Cmdindex '). Asinteger: = 11;
Cmdobject.forcepathobject (' RequestID '). Asstring: = ' login ';
Cmdobject.forcepathobject (' User.ID '). Asstring: = Auserid;
Cmdobject.forcepathobject (' User.paw '). Asstring: = Apaw;
Sendcmdobject (Cmdobject);
End
End

Procedure cmd_keepalive;
Begin
Cmdobject.clear;
Cmdobject.forcepathobject (' Cmdindex '). Asinteger: = 0;
Sendcmdobject (Cmdobject);
End

Procedure Iniclientobject;
Begin
Codertcpclient: = Tdiocpcodertcpclient.create (nil);

Diocpcontext: = Tiocpcoderremotecontext (Codertcpclient.add);
Diocpcontext.registercoderclass (Tiocpstreamdecoder, Tiocpstreamencoder);

Cmdobject: = tsimplemsgpack.create;
Cmdstream: = tmemorystream.create;
End

Procedure Uniniclientobject;
Begin
Freeandnil (Cmdobject);
Codertcpclient.disconnectall;
Codertcpclient.free;
Cmdstream.free;
End

Initialization
Iniclientobject;

Finalization
Uniniclientobject;

End.

Fourth step: Write the components that receive server data and update the Client interface

This section is to establish an associated event with TcpClient receiving data

Procedure Oncontextactionex (Aobject:tobject);

In the form creat event,

Diocpcontext.oncontextaction: = Oncontextactionex; Client Context Events
Diocpcontext.ondisconnectedevent: = ondisconnected;

Then you can process the data from the server in Oncontextactionex, and notice the "Create UI synchronization Critical section object", that is, the data update interface, anti-suspended animation

DIOCP writing the first application (iii)

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.