Diocp open-source project-diocp3 directly sends objects to help you solve the problem of sticking packets

Source: Internet
Author: User

This demo demonstrates how to directly transfer tstream objects between the client and the server, so that you can focus on data processing logic and ignore the problem of handling inter-network packets.

The server pushes a message tmemorystream object to all clients (this object is string data ).

 

Code explanation:

Procedure tfrmmain. actpushmsgexecute (Sender: tobject); var lvlist: tlist; I: integer; lvstream: tmemorystream; s: ansistring; begin lvlist: = tlist. create; try lvstream: = tmemorystream. create; Try S: = edtmsg. text; lvstream. write (s [1], length (s); // obtain the list of all online clients, ftcpserver. getonlinecontextlist (lvlist); // push to all clients cyclically
For I: = 0 to lvlist. Count-1 do begin // directly push tmemorystream object <internal trial encoder encodes tstream into protocol format, and then sends>
        TIOCPClientContext(lvList[i]).writeObject(lvStream);      end;    finally      lvStream.Free;    end;  finally    lvList.Free;  end;end;

The above code is the push button

 

constructor TfrmMain.Create(AOwner: TComponent);begin  inherited Create(AOwner);  FTcpServer := TIOCPConsole.Create(Self);  FTcpServer.createDataMonitor;  FTcpServer.OnDataObjectReceived := OnRecvObject;  // register decoder and encoder class  FTcpServer.registerCoderClass(TIOCPStreamDecoder, TIOCPStreamEncoder);  TFMMonitor.createAsChild(pnlMonitor, FTcpServer);end;

The above code and decoder are registered to process the conversion of objects when receiving and sending data, and receive object events are assigned a value.

 

Onrecvobject. If an object is decoded successfully, an event is triggered.

procedure TfrmMain.OnRecvObject(pvClientContext: TIocpClientContext;  pvObject: TObject);begin  pvClientContext.writeObject(pvObject);end;

// The above Code directly pushes the object back to the client.

 

 

The client code is similar. You can download socket-coder \ streamcoder to view the complete source code.

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.