Difference between revisions of "lNet"

From Lazarus wiki
Jump to navigationJump to search
Line 50: Line 50:
 
===Usage===
 
===Usage===
 
Drop a TLTCPComponent or TLUDPComponent on a form.
 
Drop a TLTCPComponent or TLUDPComponent on a form.
Assign OnAccept, OnError, OnRecieve and OnDisconnect event handlers. Note that this is optional but in most
+
Assign OnAccept, OnError, OnReceive and OnDisconnect event handlers. Note that this is optional but in most
 
cases required for functionality. Note also that if you don't assign OnError it will throw an exception in such case when a connection error occurs.
 
cases required for functionality. Note also that if you don't assign OnError it will throw an exception in such case when a connection error occurs.
  
 
Optional properties you may set are:
 
Optional properties you may set are:
  
* Buffersize - this is the maximum size a buffer can hold per-one recieve (note that assigning big number may produce random result. OS buffer is limited to 65k normaly)
+
* Buffersize - this is the maximum size a buffer can hold per-one receive (note that assigning big number may produce random result. OS buffer is limited to 65k normaly)
* MaxMsgs - this is the maximum number of messages a recieve buffer can hold. It's a security limit.
+
* MaxMsgs - this is the maximum number of messages a receive buffer can hold. It's a security limit.
  
 
Client connection is initialized with the Connect call. Arguments are IP address/hostname and port respectivly.
 
Client connection is initialized with the Connect call. Arguments are IP address/hostname and port respectivly.
Server connection is initialized with the Accept call. Argument is port.
+
Server connection is initialized with the Listen* call. Argument is port.
  
 
Note: address is a string representation of IP eg: '127.0.0.1' or a hostname like 'localhost'.
 
Note: address is a string representation of IP eg: '127.0.0.1' or a hostname like 'localhost'.
Line 75: Line 75:
 
* OnDisconnect - this event is fired whenever a connection is lost. If no handler is assigned nothing is done. Data contains the informational message in english. SocketNumber contains the internal number of socket which got disconnected.
 
* OnDisconnect - this event is fired whenever a connection is lost. If no handler is assigned nothing is done. Data contains the informational message in english. SocketNumber contains the internal number of socket which got disconnected.
  
* OnRecieve - this event is fired whenever new data is recieved. If no handler is assigned nothing is done, but a system buffer fill may happen which will in turn fire an OnError event. Data contains the recieved data. SocketNumber contains the internal number of socket at which the data got recieved.
+
* OnReceive - this event is fired whenever new data is received. If no handler is assigned nothing is done, but a system buffer fill may happen which will in turn fire an OnError event. Data contains the recieved data. SocketNumber contains the internal number of socket at which the data got recieved.
  
Spelling note: I know that OnRec'''ie'''ve should be spelled OnRec'''ei'''ve. However I found this only after releasing the components to public. I will fix this typo in sources and then here in wiki too. Sorry for the "inconvinience".
+
'''API CHANGES''':* There have been some API changes from 0.1 to 0.2. These were required and should improve the usability of packages. I know API changes are always a PITA but I'll try to keep them minimal from now on. Version 0.3 will be the first beta from which API will remain unchanged (only adding new stuff will be possible).
 +
 
 +
* OnRecieve() changed to OnReceive()
 +
* Accept() changed to Listen()

Revision as of 19:24, 29 November 2005

About

lNet or LightWeight Networking Library is a collection of classes and components to enable event-driven TCP or UDP networking. lNet is released under a modified LGPL license. (permits static linking)


The package consists of base lNet units library, lThreadNet library for multithreading and lNetComponents libraries for providing visual and non-visual components for networking.

These components were tested in Win32 and Linux. They should however run on any major platform supported by FPC. Homepage of the lNet base libraries is: http://members.chello.sk/ales

As of version 0.2 the package consists of multi-threaded (MT) and single-threaded(ST) versions. I strongly advise installing the ST version as it's less bug prone, doesn't require special hacks(see installation) to work and uses new LCL features to work.

Author

Ales Katona (User: Almindor)

License

Modified LGPL (read LICENSE and LICENSE.ADDON included in package).

Download

Change Log

  • Version 0.2.0 29.11.2005
  • Version 0.1.0 12.11.2005

Roadmap

  • Stress testing
  • Addition of input and output streaming classes/components
  • Addition of Telnet components

Dependencies / System Requirements

  • MT - Lazarus 0.9.10 (FPC 2.0.2)
  • ST - Lazarus 0.9.11 (FPC 2.0.0)

Status: Alpha

Issues: Tested on Windows (WinXP) and Linux (Kubuntu Breezy Badger).

Installation

I STRONGLY ADVISE USING THE ST VERSION

  • In the lazarus/components directory, unzip the files from lnetpackage-<version>.zip file. The lNetpackage-<version> folder will be created.
  • Open lazarus
  • WINDOWS USERS ONLY WITH MT VERSION: There's an inconsistency in FPC about threading on the unix platform. This is the reason for -Facthreads switch which is forced by the lnetpackage.lpk. On windows this unit does not exist. For now there is no other solution but to manually remove this dependency from lnetpackage.lpk. Open lnetpackage-0.2-mt/lnetpackage.lpk, go to options/custom and remove the -Facthreads line. Then save the package. This ugly procedure will be removed when lazarus adds the possibility to add platform specific compiler switches. I strongly avise you to use the ST version which doesn't require this step.
  • Open the package lnetpackage-0.2-<MT/ST>/lnetpackage.lpk with Component/Open package file (.lpk)
  • Click on Compile
  • Open the package lnetpackage-0.2-<MT/ST>/ide/lnetidepackage.lpk with Component/Open package file (.lpk)
  • Click on Install and answer 'Yes' when you are asked about Lazarus rebuilding. A new tab named 'lNet' will be created in the components palette.

Usage

Drop a TLTCPComponent or TLUDPComponent on a form. Assign OnAccept, OnError, OnReceive and OnDisconnect event handlers. Note that this is optional but in most cases required for functionality. Note also that if you don't assign OnError it will throw an exception in such case when a connection error occurs.

Optional properties you may set are:

  • Buffersize - this is the maximum size a buffer can hold per-one receive (note that assigning big number may produce random result. OS buffer is limited to 65k normaly)
  • MaxMsgs - this is the maximum number of messages a receive buffer can hold. It's a security limit.

Client connection is initialized with the Connect call. Arguments are IP address/hostname and port respectivly. Server connection is initialized with the Listen* call. Argument is port.

Note: address is a string representation of IP eg: '127.0.0.1' or a hostname like 'localhost'.

Sending data to the other side is accomplished with the SendMessage method. Arguments are data to be sent in string form.

Note: Currently the components are limited. I will add input and output classes/components for better handling later.

Events of TLNetComponent class.

  • OnError - this event is fired whenever a connection error occurs. If no handler is assigned an excepion with "Data" is raised. Arguments are: Data - contains string and numerical representation of error message formatted in native encoding or UTF8 (depending on ForceUTF8 property). SocketNumber is the internal number of socket at which the error occured, -1 means "not an error on socket".
  • OnAccept - this event is fired whenever a connection is accepted on a server. If no handler is assigned nothing is done. Data contains the informational message in english. SocketNumber contains the internal number of socket which accepted the connection.
  • OnDisconnect - this event is fired whenever a connection is lost. If no handler is assigned nothing is done. Data contains the informational message in english. SocketNumber contains the internal number of socket which got disconnected.
  • OnReceive - this event is fired whenever new data is received. If no handler is assigned nothing is done, but a system buffer fill may happen which will in turn fire an OnError event. Data contains the recieved data. SocketNumber contains the internal number of socket at which the data got recieved.

API CHANGES:* There have been some API changes from 0.1 to 0.2. These were required and should improve the usability of packages. I know API changes are always a PITA but I'll try to keep them minimal from now on. Version 0.3 will be the first beta from which API will remain unchanged (only adding new stuff will be possible).

  • OnRecieve() changed to OnReceive()
  • Accept() changed to Listen()