conSock: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: conSock
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Contents | |
| 2 | + | ||
| 3 | + | * 1 About | |
| 4 | + | * 2 Usage | |
| 5 | + | * 2.1 Frame structure | |
| 6 | + | * 2.2 Units | |
| 7 | + | * 2.3 Example | |
| 8 | + | * 3 Download | |
| 9 | + | ||
| 10 | + | ## About | |
| 11 | + | ||
| 12 | + | conSock is a Lazarus library that implements TCP/IP connections, in Client-Server mode, using sockets. It is based on [[Synapse]]. Tested only in Windows. | |
| 13 | + | ||
| 14 | + | Author: Tito Hinostroza | |
| 15 | + | ||
| 16 | + | License: GPL v3 | |
| 17 | + | ||
| 18 | + | ## Usage | |
| 19 | + | ||
| 20 | + | Using the library is easy because it presents objects that communicate using events. At the low level the objects are implemented using blocking sockets and threads. | |
| 21 | + | ||
| 22 | + | The connection model is similar to a Client-Server architecture, with the following rules: | |
| 23 | + | ||
| 24 | + | * To implement a communication, there must be one Client and one Server part. | |
| 25 | + | * Client part must be set with the IP address of the Server part. Server doesn't need to be set. | |
| 26 | + | * Server part can connect only to one client. If there are several clients connecting to the same server, only the first will be accepted. | |
| 27 | + | * Once the connection is established, only client can start the communication. The server can respond only to a client request. | |
| 28 | + | * All communications are implemented using the same Frame structure. | |
| 29 | + | ||
| 30 | + | Default IP for Client is 127.0.0.1. Default port for Client and Server is 80. | |
| 31 | + | ||
| 32 | + | ### Frame structure | |
| 33 | + | ||
| 34 | + | Library implements a Frame including: | |
| 35 | + | ||
| 36 | + | * Header part: 9 bytes size | |
| 37 | + | * Data part: Optional. Can be as big as 16777215 bytes | |
| 38 | + | ||
| 39 | + | Header part is small (fixed size 9 bytes) and has the following structure: | |
| 40 | + | ||
| 41 | + | * 1 byte: Header ID. Header ID is always the hexadecimal value $0F, and is a way to identify the start of a frame. | |
| 42 | + | * 3 bytes: Data part size. The "Data part size" indicates the size of the "Data part". | |
| 43 | + | * 1 byte: Command. User can send any command in the frame field "Command". But the values $03 and $83 are reserved to internal dialog of the protocol. | |
| 44 | + | * 2 bytes: X parameter. Parameters X and Y can be used to send additional information for a command. | |
| 45 | + | * 2 bytes: Y parameter. | |
| 46 | + | ||
| 47 | + | ### Units | |
| 48 | + | ||
| 49 | + | The library contains 3 units: | |
| 50 | + | ||
| 51 | + | * conSockFrames.pas - Defines the protocol and the routines to code/decode the Frame packet. | |
| 52 | + | * conSockClient.pas - Defines the base client object TConSockClient. | |
| 53 | + | * conSockServer.pas - Defines the base server object TConSockServer. | |
| 54 | + | ||
| 55 | + | To implement a basic connection, it's needed to create a client and server object. | |
| 56 | + | ||
| 57 | + | * Client Part - Modelled with the object TConSockClient. | |
| 58 | + | * Server Part - Modelled with the object TConSockServer. | |
| 59 | + | ||
| 60 | + | ### Example | |
| 61 | + | ||
| 62 | + | To implement a simple connection, it's needed to create a client part and a server part: | |
| 63 | + | ``` | |
| 64 | + | //Set client | |
| 65 | + | client := TConSockClient.Create('127.0.0.1'); | |
| 66 | + | client.OnFrameReady := @clientFrameReady; | |
| 67 | + | client.Connect; | |
| 68 | + | ||
| 69 | + | //Set server | |
| 70 | + | Server := TConSockServer.Create; | |
| 71 | + | Server.OnFrameReady := @ServerFrameReady; | |
| 72 | + | Server.Connect; | |
| 73 | + | ``` | |
| 74 | + | ||
| 75 | + | Event OnFrameReady is triggered every time a Frame is received. | |
| 76 | + | ||
| 77 | + | The state of the connection can be read in the field client.state and in server.State. This state can be: | |
| 78 | + | ||
| 79 | + | * cecCONNECTING | |
| 80 | + | * cecCONNECTED | |
| 81 | + | * cecSTOPPED | |
| 82 | + | * cecDEAD | |
| 83 | + | ||
| 84 | + | To send data from the client, we can use the following instruction: | |
| 85 | + | ``` | |
| 86 | + | client.SendCommand($FF, 0, 0, sometext); | |
| 87 | + | ``` | |
| 88 | + | ||
| 89 | + | Where $FF represents some command, and "sometext" represents the data part of the command. We can use different commands number, except the values $03 and $83 that are reserved for the library. | |
| 90 | + | ||
| 91 | + | For a complete code, check the sample projects. | |
| 92 | + | ||
| 93 | + | ## Download | |
| 94 | + | ||
| 95 | + | GitHub: <https://github.com/t-edson/conSock> | |
| 96 | + | ||
| 97 | + | --- | |
| 98 | + | *From the Free Pascal wiki, page "conSock", as the Internet Archive kept it on 2023-09-24. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
