Skip to content

Commit

Permalink
fix broadcast nullreference #41
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Sep 14, 2017
1 parent 5f9a07e commit 4d1c8af
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 31 deletions.
8 changes: 4 additions & 4 deletions nuget/MagicOnion.HttpGateway.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MagicOnion.HttpGateway</id>
<version>0.5.1</version>
<version>0.5.1.1</version>
<title>MagicOnion.HttpGateway</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand All @@ -13,17 +13,17 @@
<tags>gRPC, HTTP2</tags>
<dependencies>
<group targetFramework=".NETFramework4.6">
<dependency id="MagicOnion" version="0.5.1" />
<dependency id="MagicOnion" version="0.5.1.1" />
<dependency id="Microsoft.AspNetCore.Http.Abstractions" version="1.1.0" />
<dependency id="Newtonsoft.Json" version="10.0.2" />
</group>
<group targetFramework=".NETFramework4.7">
<dependency id="MagicOnion" version="0.5.1" />
<dependency id="MagicOnion" version="0.5.1.1" />
<dependency id="Microsoft.AspNetCore.Http.Abstractions" version="1.1.0" />
<dependency id="Newtonsoft.Json" version="10.0.2" />
</group>
<group targetFramework=".NETStandard1.5">
<dependency id="MagicOnion" version="0.5.1" />
<dependency id="MagicOnion" version="0.5.1.1" />
<dependency id="Microsoft.AspNetCore.Http.Abstractions" version="1.1.0" />
<dependency id="Newtonsoft.Json" version="10.0.2" />
</group>
Expand Down
2 changes: 1 addition & 1 deletion nuget/MagicOnion.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MagicOnion</id>
<version>0.5.1</version>
<version>0.5.1.1</version>
<title>MagicOnion</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand Down
4 changes: 2 additions & 2 deletions nuget/push.bat
@@ -1,2 +1,2 @@
nuget push MagicOnion.0.5.1.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MagicOnion.HttpGateway.0.5.1.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MagicOnion.0.5.1.1.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MagicOnion.HttpGateway.0.5.1.1.nupkg -Source https://www.nuget.org/api/v2/package
31 changes: 26 additions & 5 deletions sandbox/Sandbox.ConsoleClient/Program.cs
Expand Up @@ -37,9 +37,10 @@ static void Main(string[] args)
var channel = new Channel("localhost", 12345, ChannelCredentials.Insecure);
//channel.ConnectAsync().Wait();

var c = MagicOnionClient.Create<IStandard>(channel);
//var c = MagicOnionClient.Create<IStandard>(channel);

RunTest(c).GetAwaiter().GetResult();
//RunTest(c).GetAwaiter().GetResult();
RunChat(new ChannelContext(channel)).GetAwaiter().GetResult();
return;


Expand Down Expand Up @@ -68,12 +69,32 @@ static void Main(string[] args)
}
finally
{
var asm = AssemblyHolder.Save();
Verify(asm);
Console.WriteLine("end");
//var asm = AssemblyHolder.Save();
//Verify(asm);
//Console.WriteLine("end");
}
}

static async Task RunChat(ChannelContext ctx)
{
// create room
var client = ctx.CreateClient<IChatRoomService>();

var room = await client.CreateNewRoom("test", "A");

var waiter = (await client.OnJoin()).ResponseStream.ForEachAsync(xs =>
{
Console.WriteLine(xs);
});

await client.Join(room.Id, "B");

//var result = await await client.SendMessage(room.Id, "foo bar baz");
// Console.WriteLine("Send success:" + result);

await waiter;
}


static async Task RunTest(IStandard c)
{
Expand Down
34 changes: 20 additions & 14 deletions sandbox/Sandbox.ConsoleServer/Services/ChatRoomService.cs
@@ -1,4 +1,6 @@
using MagicOnion;
#pragma warning disable CS1998

using MagicOnion;
using MagicOnion.Server;
using System;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -112,9 +114,11 @@ static void SetMyId(ConnectionContext context, ChatRoom room, string id)
context.Items[$"RoomService{room.Id}.MyId"] = id;
}


// RoomCommand

public Task<UnaryResult<ChatRoomResponse>> CreateNewRoom(string roomName, string nickName)

public async UnaryResult<ChatRoomResponse> CreateNewRoom(string roomName, string nickName)
{
var room = new ChatRoom(Guid.NewGuid().ToString(), roomName);
var member = new RoomMember(Guid.NewGuid().ToString(), nickName);
Expand All @@ -131,32 +135,32 @@ public Task<UnaryResult<ChatRoomResponse>> CreateNewRoom(string roomName, string
LeaveCore(t.Item1, t.Item2).Wait();
}, Tuple.Create(room.Id, member.Id));

return Task.FromResult(UnaryResult(room.ToChatRoomResponse()));
return room.ToChatRoomResponse();
}

public Task<UnaryResult<ChatRoomResponse[]>> GetRooms()
public async UnaryResult<ChatRoomResponse[]> GetRooms()
{
return Task.FromResult(UnaryResult(RoomRepository.Default.GetRooms().Select(x => x.ToChatRoomResponse()).ToArray()));
return RoomRepository.Default.GetRooms().Select(x => x.ToChatRoomResponse()).ToArray();
}

public async Task<UnaryResult<ChatRoomResponse>> Join(string roomId, string nickName)
public async UnaryResult<ChatRoomResponse> Join(string roomId, string nickName)
{
var room = RoomRepository.Default.GetRoom(roomId);
var newMember = new RoomMember(Guid.NewGuid().ToString(), nickName);
room.AddMember(newMember, GetStreamingContextRepository());

await room.BroadcastJoinAsync(newMember);

return UnaryResult(room.ToChatRoomResponse());
return room.ToChatRoomResponse();
}

public async Task<UnaryResult<bool>> Leave(string roomId)
public async UnaryResult<bool> Leave(string roomId)
{
var connectionContext = this.GetConnectionContext();
var room = RoomRepository.Default.GetRoom(roomId);
if (room == null) return UnaryResult(false);
if (room == null) return false;
await LeaveCore(roomId, GetMyId(connectionContext, room));
return UnaryResult(true);
return true;
}

// called from ConnectionStatus.Register so should be static.
Expand All @@ -179,15 +183,15 @@ static async Task LeaveCore(string roomId, string myId)
}
}

public Task<UnaryResult<bool>> SendMessage(string roomId, string message)
public async UnaryResult<bool> SendMessage(string roomId, string message)
{
var room = RoomRepository.Default.GetRoom(roomId);
var myId = GetMyId(this.GetConnectionContext(), room);
var self = room.GetMember(myId);
if (self == null) return Task.FromResult(UnaryResult(false));
if (self == null) return false;

RoomRepository.Default.GetRoom(roomId).BroadcastMessageAsync(self.Value, message);
return Task.FromResult(UnaryResult(true));
return true;
}

// RoomStreaming
Expand All @@ -197,7 +201,7 @@ StreamingContextRepository<IChatRoomStreaming> GetStreamingContextRepository()
var connection = this.GetConnectionContext();
var item = connection.Items.GetOrAdd("RoomStreamingStreamingContextRepository", _ => new Lazy<StreamingContextRepository<IChatRoomStreaming>>(() =>
{
return new StreamingContextRepository<IChatRoomStreaming>(connection);
return new StreamingContextRepository<IChatRoomStreaming>(connection, this);
}));
return (item as Lazy<StreamingContextRepository<IChatRoomStreaming>>).Value;
}
Expand All @@ -218,3 +222,5 @@ public async Task<ServerStreamingResult<ChatMessage>> OnMessageReceived()
}
}
}

#pragma warning restore CS1998
10 changes: 5 additions & 5 deletions sandbox/Sandbox.ConsoleServerDefinition/IChatRoomService.cs
Expand Up @@ -18,15 +18,15 @@ public interface IChatRoomStreaming// : IStreamingService
public interface IChatRoomCommand
{
/// <summary>Create new room.</summary>
Task<UnaryResult<ChatRoomResponse>> CreateNewRoom(string roomName, string nickName);
UnaryResult<ChatRoomResponse> CreateNewRoom(string roomName, string nickName);

Task<UnaryResult<ChatRoomResponse>> Join(string roomId, string nickName);
UnaryResult<ChatRoomResponse> Join(string roomId, string nickName);

Task<UnaryResult<ChatRoomResponse[]>> GetRooms();
UnaryResult<ChatRoomResponse[]> GetRooms();

Task<UnaryResult<bool>> Leave(string roomId);
UnaryResult<bool> Leave(string roomId);

Task<UnaryResult<bool>> SendMessage(string roomId, string message);
UnaryResult<bool> SendMessage(string roomId, string message);
}

public interface IChatRoomService : IService<IChatRoomService>, IChatRoomCommand, IChatRoomStreaming
Expand Down
14 changes: 14 additions & 0 deletions src/MagicOnion/Server/StreamingContextRepository.cs
Expand Up @@ -60,6 +60,7 @@ public class StreamingContextRepository<TService> : IDisposable
public bool IsDisposed => isDisposed;
public ConnectionContext ConnectionContext { get; }

// TODO:Obsolete?
public StreamingContextRepository(ConnectionContext connectionContext)
{
this.ConnectionContext = connectionContext;
Expand All @@ -69,6 +70,19 @@ public StreamingContextRepository(ConnectionContext connectionContext)
});
}

public StreamingContextRepository(ConnectionContext connectionContext, TService self)
{
this.ConnectionContext = connectionContext;
connectionContext.ConnectionStatus.Register(() =>
{
Dispose();
});

// .NET Standard does not support GetUninitializedObject, TService may always has zero argument constructor.
// dummyInstance = (TService)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(self.GetType());
dummyInstance = (TService)Activator.CreateInstance(self.GetType());
}

public StreamingContextInfo<TResponse> RegisterStreamingMethod<TResponse>(TService self, Func<Task<ServerStreamingResult<TResponse>>> methodSelector)
{
if (isDisposed) throw new ObjectDisposedException("StreamingContextRepository", "already disposed(disconnected).");
Expand Down

0 comments on commit 4d1c8af

Please sign in to comment.