Functions/Send-TCPMessage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Function Send-TCPMessage
{
    <#
        .SYNOPSIS
        Sends a datagram using the specified TCP writer
 
        .DESCRIPTION
        Internal function.
 
        This function will send a datagram using the specified TCP writer.
 
        .EXAMPLE
        Send-TCPMessage -TcpWriter $Writer -Datagram $Message
        Sends the datagram, or byte array, $Message using the specified TCP writer $writer.
 
        .OUTPUTS
        None
    #>


    [CmdletBinding()]
    [OutputType($null)]
    param
    (
        # TCPWriter object, that is already connected to the TCP server.
        [Parameter(Mandatory   = $true,
                   HelpMessage = 'TCPWriter object, that is already connected to the TCP server')]
        [ValidateNotNullOrEmpty()]
        [System.IO.StreamWriter]
        $TcpWriter,

        # Byte array containing the datagram to be sent.
        [Parameter(Mandatory   = $true,
                   HelpMessage = 'Byte array containing the datagram to be sent')]
        [ValidateNotNullOrEmpty()]
        [byte[]]
        $Datagram
    )

    Write-Verbose -Message ([Text.Encoding]::ASCII.GetString($Datagram))

    $null = $TcpWriter.Write($Datagram, 0, $Datagram.Length)
}