Skip to content

Commit

Permalink
#902: fixes from testing, and docs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Apr 16, 2022
1 parent 4983176 commit 7113d3c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 28 deletions.
13 changes: 10 additions & 3 deletions docs/Servers/TCP.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Pode has a generic TCP server type inbuilt. Unlike the other server types, the T

Where a web server using Routes, a TCP server uses Verbs - think of them like "commands" or "phrases". Requests sent will be matched to a Verb, and that Verb's logic invoked (more info below).

!!! important
This server type is still in the early days, so if you find any bugs or have any suggestions, please feel free to raise them over on [GitHub](https://github.com/badgerati/pode) or [Discord](https://discord.gg/fRqeGcbF6h)! 😄

## Usage

To create a TCP server you need to create an appropriate [Endpoint](../../Tutorials/Endpoints/Basics) with the TCP protocol, plus some [Verbs](#verbs).
Expand All @@ -12,7 +15,7 @@ The following example will create a TCP endpoint listening on `localhost:9000`,

```powershell
Start-PodeServer {
Add-PodeEndpoint -Address localhost -Port 9000 -Protocol Tcp -CRLFMessageEnd
Add-PodeEndpoint -Address localhost -Port 9000 -Protocol Tcp -CRLFMessageEnd -Acknowledge 'Welcome!'
Add-PodeVerb -Verb 'HELLO' -ScriptBlock {
Write-PodeTcpClient -Message 'HI'
Expand All @@ -27,12 +30,16 @@ Start the above server, and then in a different terminal start telnet and send "

```powershell
$> telnet localhost 9000
S> Welcome!
C> HELLO
S> HI
```

When you send "HELLO", the server will respond with "HI". If you can't use telnet, then there's a quick [test script](#test-send) below to use.

!!! important
If you're using telnet to send data, backspaces will be recorded as sent data.

### Acknowledge

If you'd like to send an initial message to clients as soon as they connect, you can set an `-Acknowledge` message on the Endpoint:
Expand Down Expand Up @@ -93,7 +100,7 @@ Start-PodeServer {
}
```

Running the above server and navigating to http://localhost:9000 will greet you with a message. The raw data sent by the browser will be display on the terminal; this could be parsed to do different things.
Running the above server and navigating to `http://localhost:9000` will greet you with a message. The raw data sent by the browser will be display on the terminal; this could be parsed to do different things.

### SSL Upgrade

Expand Down Expand Up @@ -137,7 +144,7 @@ In the above examples you've seen [`Write-PodeTcpClient`](../../Functions/Respon
```powershell
Add-PodeVerb -Verb 'HELLO' -ScriptBlock {
Write-PodeTcpClient -Message "Hi! What's your name?"
$name = Read-PodeTcpClient
$name = Read-PodeTcpClient -CRLFMessageEnd
Write-PodeTcpClient -Message "Hi, $($name)!"
}
```
Expand Down
9 changes: 4 additions & 5 deletions examples/tcp-server-http.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop
# or just:
# Import-Module Pode

# create a server, and start listening on port 8999
# create a server, and start listening on port 9000
Start-PodeServer -Threads 2 {

# add two endpoints
Add-PodeEndpoint -Address * -Port 8999 -Protocol Tcp
Add-PodeEndpoint -Address * -Port 9000 -Protocol Tcp

# enable logging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging

# catch-all for http
Add-PodeVerb -Verb '*' -ScriptBlock {
Add-PodeVerb -Verb '*' -Close -ScriptBlock {
$TcpEvent.Request.Body | Out-Default
Write-PodeTcpClient -Message "HTTP/1.1 200 `r`nConnection: close`r`n`r`n<b>Hello, there</b>"
Close-PodeTcpClient
# navigate to "http://localhost:8999"
# navigate to "http://localhost:9000"
}

}
37 changes: 37 additions & 0 deletions examples/tcp-server-multi-endpoint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop

# or just:
# Import-Module Pode

# create a server, and start listening on port 9000
Start-PodeServer -Threads 2 {

# add two endpoints
Add-PodeEndpoint -Address * -Port 9000 -Protocol Tcp -Name 'EP1' -Acknowledge 'Hello there!' -CRLFMessageEnd
Add-PodeEndpoint -Address '127.0.0.2' -Hostname 'foo.pode.com' -Port 9000 -Protocol Tcp -Name 'EP2' -Acknowledge 'Hello there!' -CRLFMessageEnd

# enable logging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging

# hello verb for endpoint1
Add-PodeVerb -Verb 'HELLO :forename :surname' -EndpointName EP1 -ScriptBlock {
Write-PodeTcpClient -Message "HI 1, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)"
"HI 1, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)" | Out-Default
}

# hello verb for endpoint2
Add-PodeVerb -Verb 'HELLO :forename :surname' -EndpointName EP2 -ScriptBlock {
Write-PodeTcpClient -Message "HI 2, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)"
"HI 2, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)" | Out-Default
}

# catch-all verb for both endpoints
Add-PodeVerb -Verb '*' -ScriptBlock {
Write-PodeTcpClient -Message "Unrecognised verb sent"
}

# quit verb for both endpoints
Add-PodeVerb -Verb 'Quit' -Close

}
49 changes: 33 additions & 16 deletions examples/tcp-server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,51 @@ Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop
# or just:
# Import-Module Pode

# create a server, and start listening on port 8999
# create a server, and start listening on port 9000
Start-PodeServer -Threads 2 {

# add two endpoints
Add-PodeEndpoint -Address * -Port 8999 -Protocol Tcp -Name 'EP1' -Acknowledge 'Hello there!' -CRLFMessageEnd
Add-PodeEndpoint -Address '127.0.0.2' -Hostname 'foo.pode.com' -Port 8999 -Protocol Tcp -Name 'EP2' -Acknowledge 'Hello there!' -CRLFMessageEnd
Add-PodeEndpoint -Address * -Port 9000 -Protocol Tcp -CRLFMessageEnd #-Acknowledge 'Welcome!'
# Add-PodeEndpoint -Address * -Port 9000 -Protocol Tcps -SelfSigned -CRLFMessageEnd -TlsMode Explicit -Acknowledge 'Welcome!'
# Add-PodeEndpoint -Address * -Port 9000 -Protocol Tcps -SelfSigned -CRLFMessageEnd -TlsMode Implicit -Acknowledge 'Welcome!'

# enable logging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging

# hello verb for endpoint1
Add-PodeVerb -Verb 'HELLO :forename :surname' -EndpointName EP1 -ScriptBlock {
Write-PodeTcpClient -Message "HI 1, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)"
"HI 1, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)" | Out-Default
Add-PodeVerb -Verb 'HELLO' -ScriptBlock {
Write-PodeTcpClient -Message "HI"
}

# hello verb for endpoint2
Add-PodeVerb -Verb 'HELLO :forename :surname' -EndpointName EP2 -ScriptBlock {
Write-PodeTcpClient -Message "HI 2, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)"
"HI 2, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)" | Out-Default
Add-PodeVerb -Verb 'HELLO2 :username' -ScriptBlock {
Write-PodeTcpClient -Message "HI2, $($TcpEvent.Parameters.username)"
}

# catch-all verb for both endpoints
Add-PodeVerb -Verb '*' -ScriptBlock {
Write-PodeTcpClient -Message "Unrecognised verb sent"
Add-PodeVerb -Verb * -ScriptBlock {
Write-PodeTcpClient -Message 'Unrecognised verb sent'
}

# quit verb for both endpoints
Add-PodeVerb -Verb 'Quit' -Close
# Add-PodeVerb -Verb * -Close -ScriptBlock {
# $TcpEvent.Request.Body | Out-Default
# Write-PodeTcpClient -Message "HTTP/1.1 200 `r`nConnection: close`r`n`r`n<b>Hello, there</b>"
# }

# Add-PodeVerb -Verb 'STARTTLS' -UpgradeToSsl

# Add-PodeVerb -Verb 'STARTTLS' -ScriptBlock {
# Write-PodeTcpClient -Message 'TLS GO AHEAD'
# $TcpEvent.Request.UpgradeToSSL()
# }

# Add-PodeVerb -Verb 'QUIT' -Close

Add-PodeVerb -Verb 'QUIT' -ScriptBlock {
Write-PodeTcpClient -Message 'Bye!'
Close-PodeTcpClient
}

Add-PodeVerb -Verb 'HELLO3' -ScriptBlock {
Write-PodeTcpClient -Message "Hi! What's your name?"
$name = Read-PodeTcpClient -CRLFMessageEnd
Write-PodeTcpClient -Message "Hi, $($name)!"
}
}
6 changes: 4 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ repo_url: https://github.com/Badgerati/Pode

extra:
social:
- icon: fontawesome/brands/github-alt
link: https://github.com/badgerati
- icon: fontawesome/brands/discord
link: https://discord.gg/fRqeGcbF6h
- icon: fontawesome/brands/github
link: https://github.com/badgerati/pode
- icon: fontawesome/brands/twitter
link: https://twitter.com/badgerati
4 changes: 2 additions & 2 deletions src/Listener/PodeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public async Task<string> Read(byte[] checkBytes, CancellationToken cancellation
cancellationToken.ThrowIfCancellationRequested();
bufferStream.Write(buffer, 0, read);

if (Socket.Available > 0 || !ValidateInputInternal(BufferStream.ToArray(), checkBytes))
if (Socket.Available > 0 || !ValidateInputInternal(bufferStream.ToArray(), checkBytes))
{
continue;
}
Expand All @@ -201,7 +201,7 @@ public async Task<string> Read(byte[] checkBytes, CancellationToken cancellation
}

cancellationToken.ThrowIfCancellationRequested();
return Encoding.GetString(bufferStream.ToArray());
return Encoding.GetString(bufferStream.ToArray()).Trim();
}
finally
{
Expand Down

0 comments on commit 7113d3c

Please sign in to comment.