-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #20 from webwarrior-ws/websocket-to-grpc-squashed
Replace Websockets with GRPC.
- Loading branch information
Showing
34 changed files
with
600 additions
and
415 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,5 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
</Project> |
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,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<!-- CS8981 is triggered by auto-generated protobuf types --> | ||
<WarningsNotAsErrors>$(WarningsNotAsErrors);CS8981</WarningsNotAsErrors> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Protobuf" /> | ||
<PackageReference Include="Grpc.Net.ClientFactory" /> | ||
<PackageReference Include="Grpc.Tools"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FX.GrpcModels\FX.GrpcModels.fsproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Protobuf Include="..\FX.GrpcService\Protos\fx.proto" GrpcServices="Client"> | ||
<Link>Protos\fx.proto</Link> | ||
</Protobuf> | ||
</ItemGroup> | ||
</Project> |
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,51 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using Grpc.Core; | ||
using Grpc.Net.Client; | ||
|
||
using GrpcService; | ||
|
||
using GrpcModels; | ||
|
||
namespace GrpcClient | ||
{ | ||
public class Instance | ||
{ | ||
private static string serverFqdn = | ||
"localhost"; | ||
|
||
public static readonly int Port = 5178; | ||
public static readonly int HttpsPort = 7178; | ||
|
||
public FXGrpcService.FXGrpcServiceClient Connect() | ||
{ | ||
var channel = GrpcChannel.ForAddress($"https://{serverFqdn}:{HttpsPort}"); | ||
var client = new FXGrpcService.FXGrpcServiceClient(channel); | ||
return client; | ||
} | ||
|
||
public async Task<string> SendMessage(string message) | ||
{ | ||
var client = Connect(); | ||
var reply = await client.GenericMethodAsync( | ||
new GenericInputParam { MsgIn = message } | ||
); | ||
Console.WriteLine($"Got response: {reply.MsgOut}"); | ||
return reply.MsgOut; | ||
} | ||
|
||
public async Task<string> SendMessage<TMessage>(TMessage message) | ||
{ | ||
var text = Marshaller.Serialize(message); | ||
return await SendMessage(text); | ||
} | ||
|
||
public async Task<TResponse> SendMessage<TMessage, TResponse>(TMessage message) | ||
{ | ||
var text = Marshaller.Serialize(message); | ||
var responseText = await SendMessage(text); | ||
return Marshaller.Deserialize<TResponse>(responseText); | ||
} | ||
} | ||
} |
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Models.fs" /> | ||
<Compile Include="ModelSerialization.fs" /> | ||
<Compile Include="Marshalling.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="System.Text.Json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FX.Core\FX.Core.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,74 @@ | ||
namespace GrpcModels | ||
|
||
open System | ||
open System.Reflection | ||
open System.Text.Json | ||
|
||
open ModelSerialization | ||
|
||
module VersionHelper = | ||
let CURRENT_VERSION = | ||
Assembly | ||
.GetExecutingAssembly() | ||
.GetName() | ||
.Version.ToString() | ||
|
||
type IMarshallingWrapper = | ||
abstract member Value: obj | ||
|
||
type MarshallingWrapper<'T> = | ||
{ | ||
Version: string | ||
TypeName: string | ||
Value: 'T | ||
} | ||
|
||
static member New(value: 'T) = | ||
{ | ||
Value = value | ||
Version = VersionHelper.CURRENT_VERSION | ||
TypeName = typeof<'T>.ToString() | ||
} | ||
|
||
interface IMarshallingWrapper with | ||
member this.Value = this.Value :> obj | ||
|
||
module Marshaller = | ||
|
||
let ExtractMetadata(json: string) : Type * Version = | ||
let wrapper = JsonSerializer.Deserialize<MarshallingWrapper<obj>>(json, serializationOptions) | ||
let typ = Type.GetType wrapper.TypeName | ||
let version = Version wrapper.Version | ||
typ, version | ||
|
||
let Serialize<'T>(object: 'T) : string = | ||
let wrapper = MarshallingWrapper.New object | ||
JsonSerializer.Serialize(wrapper, serializationOptions) | ||
|
||
let Deserialize<'T>(json: string) : 'T = | ||
if isNull json then | ||
raise <| ArgumentNullException "json" | ||
|
||
let wrapper = JsonSerializer.Deserialize<MarshallingWrapper<'T>>(json, serializationOptions) | ||
wrapper.Value | ||
|
||
let DeserializeAbstract (json: string) (targetType: Type) : obj = | ||
if isNull json then | ||
raise <| ArgumentNullException "json" | ||
|
||
let wrapperGenericType = typedefof<MarshallingWrapper<_>> | ||
|
||
let wrapperType = | ||
wrapperGenericType.MakeGenericType(Array.singleton targetType) | ||
|
||
let wrapperObj = JsonSerializer.Deserialize(json, wrapperType, serializationOptions) | ||
|
||
if isNull wrapperObj then | ||
failwith "Deserialization failed: result is null" | ||
elif wrapperObj.GetType() <> wrapperType then | ||
failwithf | ||
"Deserialization failed, resulting type: %s" | ||
(wrapperObj.GetType().ToString()) | ||
|
||
let wrapper = wrapperObj :?> IMarshallingWrapper | ||
wrapper.Value |
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,57 @@ | ||
namespace GrpcModels | ||
|
||
open System.Text.Json | ||
open System.Text.Json.Serialization | ||
|
||
open FsharpExchangeDotNetStandard | ||
|
||
module ModelSerialization = | ||
type MatchTypeConverter() = | ||
inherit JsonConverter<Match>() | ||
|
||
override this.Read(reader, _typeToConvert, _options) = | ||
if reader.TokenType <> JsonTokenType.StartObject then | ||
raise <| JsonException() | ||
|
||
// "Type" key | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.PropertyName || reader.GetString() <> "Type" then | ||
raise <| JsonException() | ||
// "Type" value | ||
reader.Read() |> ignore | ||
match reader.GetString() with | ||
| "Full" -> | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.EndObject then | ||
raise <| JsonException() | ||
Match.Full | ||
| "Partial" -> | ||
// "Amount" key | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.PropertyName || reader.GetString() <> "Amount" then | ||
raise <| JsonException() | ||
// "Amount" value | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.Number then | ||
raise <| JsonException() | ||
let amount = reader.GetDecimal() | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.EndObject then | ||
raise <| JsonException() | ||
Match.Partial amount | ||
| typeName -> raise <| JsonException("Unknown Match type: " + typeName) | ||
|
||
override this.Write(writer, value, _options ) = | ||
writer.WriteStartObject() | ||
match value with | ||
| Full -> | ||
writer.WriteString("Type", "Full") | ||
| Partial amount -> | ||
writer.WriteString("Type", "Partial") | ||
writer.WriteNumber("Amount", amount) | ||
writer.WriteEndObject() | ||
|
||
let serializationOptions = | ||
let options = JsonSerializerOptions(Redis.Serialization.serializationOptions) | ||
options.Converters.Add(MatchTypeConverter()) | ||
options |
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,22 @@ | ||
namespace GrpcModels | ||
|
||
open System | ||
|
||
type LimitOrder = | ||
{ | ||
Price: decimal | ||
Side: string | ||
Quantity: decimal | ||
} | ||
|
||
type MarketOrder = | ||
{ | ||
Side: string | ||
Quantity: decimal | ||
} | ||
|
||
type CancelOrderRequest = | ||
{ | ||
OrderId: Guid | ||
// TODO: add Market | ||
} |
Oops, something went wrong.