PowerTCP Mail for .NET
SegmentEventHandler Delegate
See Also  Example Send comments on this topic.
Dart.PowerTCP.Mail Namespace : SegmentEventHandler Delegate




sender
The source of the event.
e
A SegmentEventArgs object that contains the event data.
Represents the method (event handler) that is raised when data is received or sent.

Syntax

Visual Basic (Declaration) 
Public Delegate Sub SegmentEventHandler( _
   ByVal sender As Object, _
   ByVal e As SegmentEventArgs _
) 
Visual Basic (Usage)Copy Code
Dim instance As New SegmentEventHandler(AddressOf HandlerMethod)
C# 
public delegate void SegmentEventHandler( 
   object sender,
   SegmentEventArgs e
)
Managed Extensions for C++ 
public: __gc __delegate void SegmentEventHandler( 
   Object* sender,
   SegmentEventArgs* e
)
C++/CLI 
public delegate void SegmentEventHandler( 
   Object^ sender,
   SegmentEventArgs^ e
)

Parameters

sender
The source of the event.
e
A SegmentEventArgs object that contains the event data.

Example

The following example demonstrates using the Log event to create a text log of all data sent over the control connection.
 
' The Trace Event will fire whenever data is sent/received over the control connection. 
Private Sub Ftp1_Log(ByVal sender As Object, ByVal e As Dart.PowerTCP.Ftp.SegmentEventArgs) Handles Ftp1.Log
   ' Create FileStream to write to log file
   Dim stream1 As New System.IO.FileStream("c:\FtpTest\mylog.log", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)

   ' Create text to precede sent/received data.
   Dim recv() As Byte = System.Text.Encoding.ASCII.GetBytes(vbCrLf + "Received <--" + vbCrLf)
   Dim sent() As Byte = System.Text.Encoding.ASCII.GetBytes(vbCrLf + "Sent -->" + vbCrLf)

   ' Set the stream position to append.
   stream1.Position = stream1.Length

   ' Check if data is incoming or outgoing and write appropriate data
   If e.Segment.Sent = True Then
      stream1.Write(sent, 0, sent.Length)
   Else
      stream1.Write(recv, 0, recv.Length)
   End If

   ' Write the data
   stream1.Write(e.Segment.Buffer, e.Segment.Offset, e.Segment.Count)

   ' Close the stream
   stream1.Close()

End Sub    
				
// The Trace Event will fire whenever data is sent/received. 

private void Ftp1_Trace(object sender, Dart.PowerTCP.Ftp.SegmentEventArgs e)
{

    // Create FileStream to write to log file
    System.IO.FileStream stream1 = new System.IO.FileStream("c:\\FtpTest\\log.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);

    // Create text to precede sent/received data.
    byte[] recv = System.Text.ASCIIEncoding.ASCII.GetBytes("\r\nReceived <-- \r\n");
    byte[] sent = System.Text.ASCIIEncoding.ASCII.GetBytes("\r\nSent --> \r\n");

    // Set the stream position to append.
    stream1.Position = stream1.Length;

    // Check if data is incoming or outgoing and write appropriate data
    if(e.Segment.Sent == true)
        stream1.Write(sent, 0, sent.Length);
    else
        stream1.Write(recv, 0, recv.Length);

    // Write the data
    stream1.Write(e.Segment.Buffer, e.Segment.Offset, e.Segment.Count);

    // Close the stream
    stream1.Close();
				
The following example demonstrates using the Log event to create a text log of all data sent over the control connection.
Visual BasicCopy Code
' The Trace Event will fire whenever data is sent/received over the control connection. 
Private Sub Ftp1_Log(ByVal sender As Object, ByVal e As Dart.PowerTCP.Ftp.SegmentEventArgs) Handles Ftp1.Log
   ' Create FileStream to write to log file
   Dim stream1 As New System.IO.FileStream("c:\FtpTest\mylog.log", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)

   ' Create text to precede sent/received data.
   Dim recv() As Byte = System.Text.Encoding.ASCII.GetBytes(vbCrLf + "Received <--" + vbCrLf)
   Dim sent() As Byte = System.Text.Encoding.ASCII.GetBytes(vbCrLf + "Sent -->" + vbCrLf)

   ' Set the stream position to append.
   stream1.Position = stream1.Length

   ' Check if data is incoming or outgoing and write appropriate data
   If e.Segment.Sent = True Then
      stream1.Write(sent, 0, sent.Length)
   Else
      stream1.Write(recv, 0, recv.Length)
   End If

   ' Write the data
   stream1.Write(e.Segment.Buffer, e.Segment.Offset, e.Segment.Count)

   ' Close the stream
   stream1.Close()

End Sub
C#Copy Code
// The Trace Event will fire whenever data is sent/received. 

private void Ftp1_Trace(object sender, Dart.PowerTCP.Ftp.SegmentEventArgs e)
{

    // Create FileStream to write to log file
    System.IO.FileStream stream1 = new System.IO.FileStream("c:\\FtpTest\\log.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);

    // Create text to precede sent/received data.
    byte[] recv = System.Text.ASCIIEncoding.ASCII.GetBytes("\r\nReceived <-- \r\n");
    byte[] sent = System.Text.ASCIIEncoding.ASCII.GetBytes("\r\nSent --> \r\n");

    // Set the stream position to append.
    stream1.Position = stream1.Length;

    // Check if data is incoming or outgoing and write appropriate data
    if(e.Segment.Sent == true)
        stream1.Write(sent, 0, sent.Length);
    else
        stream1.Write(recv, 0, recv.Length);

    // Write the data
    stream1.Write(e.Segment.Buffer, e.Segment.Offset, e.Segment.Count);

    // Close the stream
    stream1.Close();

Remarks

As Microsoft describes in their MSDN documentation, the event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A class that holds the event data. This class must derive from the base class EventArgs.
  • A delegate that points to a method that provides the response to the event.
When sending or receiving data, the class that holds the event data is the SegmentEventArgs class. A method must be created with the same signature as the delegate. In this example that would mean a method would have to be defined to accept two arguments (an object and a SegmentEventArgs) and return void. Once this has been done, the delegate must be "connected" to the handling event. This is done by adding an instance of the delegate to the event.

For more information about event handler delegates, see the Using Events in PowerTCP topic.

If your code causes an exception, it would be returned to the handling event without you seeing it. To preclude such a condition, you should ALWAYS use a try/ catch block around your event-handling code.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.