-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Horusiath/feature/akkling-io
Added support for Akka.IO API
- Loading branch information
Showing
9 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#r "../src/Akkling/bin/Debug/Akka.dll" | ||
#r "../src/Akkling/bin/Debug/Wire.dll" | ||
#r "../src/Akkling/bin/Debug/Newtonsoft.Json.dll" | ||
#r "../src/Akkling/bin/Debug/FSharp.PowerPack.dll" | ||
#r "../src/Akkling/bin/Debug/FSharp.PowerPack.Linq.dll" | ||
#r "../src/Akkling/bin/Debug/Akkling.dll" | ||
|
||
open System | ||
open Akkling | ||
open Akkling.IO | ||
open Akkling.IO.Tcp | ||
open System.Net | ||
|
||
let system = System.create "telnet-sys" <| Configuration.defaultConfig() | ||
|
||
let handler connection = fun (ctx: Actor<obj>) -> | ||
monitor ctx connection |> ignore | ||
let rec loop () = actor { | ||
let! msg = ctx.Receive () | ||
match msg with | ||
| Received(data) -> | ||
printfn "%s" (data.DecodeString()) | ||
return! loop () | ||
| Terminated(_, _,_) | ConnectionClosed(_) -> return Stop | ||
| _ -> return Unhandled | ||
} | ||
loop () | ||
|
||
let endpoint = IPEndPoint(IPAddress.Loopback, 5000) | ||
let listener = spawn system "listener" <| fun m -> | ||
IO.Tcp(m) <! TcpMessage.Bind(m.Self, endpoint, 100) | ||
let rec loop () = actor { | ||
let! (msg: obj) = m.Receive () | ||
match msg with | ||
| Connected(remote, local) -> | ||
let conn = m.Sender () | ||
conn <! TcpMessage.Register(spawn m null (handler conn)) | ||
return! loop () | ||
| _ -> return Unhandled | ||
} | ||
loop () | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="IO.fs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com> | ||
// Copyright (C) 2013-2015 Akka.NET project <https://github.com/akkadotnet/akka.net> | ||
// Copyright (C) 2015 Bartosz Sypytkowski <gttps://github.com/Horusiath> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace Akkling | ||
|
||
module IO = | ||
|
||
open Akka.IO | ||
open System.Net | ||
|
||
/// <summary> | ||
/// Gets TCP manager for current actor. | ||
/// </summary> | ||
let Tcp(context: Actor<'Message>) : IActorRef<Akka.IO.Tcp.Command> = | ||
typed (Akka.IO.Tcp.Manager(context.System)) | ||
|
||
/// <summary> | ||
/// Gets UDP manager for current actor. | ||
/// </summary> | ||
let Udp(context: Actor<'Message>) : IActorRef<Akka.IO.Udp.Command> = | ||
typed (Akka.IO.Udp.Manager(context.System)) | ||
|
||
type TcpMessage = Akka.IO.TcpMessage | ||
|
||
module Tcp = | ||
|
||
let (|Received|_|) (msg:obj) : ByteString option = | ||
match msg with | ||
| :? Tcp.Received as r -> Some (r.Data) | ||
| _ -> None | ||
|
||
let (|Connected|_|) (msg:obj) : (EndPoint * EndPoint) option = | ||
match msg with | ||
| :? Tcp.Connected as c -> Some (c.RemoteAddress, c.LocalAddress) | ||
| _ -> None | ||
|
||
let (|CommandFailed|_|) (msg:obj) : #Tcp.Command option = | ||
match msg with | ||
| :? Tcp.CommandFailed as c -> | ||
if c.Cmd :? #Tcp.Command | ||
then Some (c.Cmd :?> #Tcp.Command) | ||
else None | ||
| _ -> None | ||
|
||
let (|ConnectionClosed|_|) (msg:obj) = | ||
match msg with | ||
| :? Tcp.ConnectionClosed as closed -> Some closed | ||
| _ -> None | ||
|
||
let (|Closed|Aborted|ConfirmedClosed|PeerClosed|ErrorClosed|) (msg:Tcp.ConnectionClosed) = | ||
match msg with | ||
| :? Tcp.Closed -> Closed | ||
| :? Tcp.Aborted -> Aborted | ||
| :? Tcp.ConfirmedClosed -> ConfirmedClosed | ||
| :? Tcp.PeerClosed -> PeerClosed | ||
| :? Tcp.ErrorClosed -> ErrorClosed | ||
|
||
module Udp = | ||
|
||
let inline Bind(ref: IActorRef<'t>, localAddress: EndPoint) = | ||
Akka.IO.Udp.Bind(ref, localAddress) :> Udp.Command | ||
|
||
let (|Received|_|) (msg:obj) : ByteString option = | ||
match msg with | ||
| :? Udp.Received as r -> Some (r.Data) | ||
| _ -> None | ||
|
||
let (|CommandFailed|_|) (msg:obj) : 'C option = | ||
match msg with | ||
| :? Udp.CommandFailed as c -> | ||
if c.Cmd :? 'C | ||
then Some (c.Cmd :?> 'C) | ||
else None | ||
| _ -> None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters