Exemple #1
0
    public override async Task Stream(IAsyncStreamReader <Package.Types.Frame> requestStream, IServerStreamWriter <Package.Types.Frame> responseStream, ServerCallContext context)
    {
        if (GetMetadata(context, out uint userID, out string token) == false)
        {
            Logger.Error("Invalid connection");
            return;
        }
        baseServer.Connect(userID, token);

        try
        {
            while (await requestStream.MoveNext(CancellationToken.None))
            {
                Package.Types.Frame p = (Package.Types.Frame)requestStream.Current;
                var responseTask      = Task.Run(async() =>
                {
                    Package.Types.Frame rep = null;
                    if (p.CmdId == Gvalue.CmdHeartbeat)
                    {
                        rep = p;
                    }
                    else
                    {
                        if (p.CmdId == (UInt32)HotelGsCmdID.HotelCreateConnect ||
                            p.CmdId == (UInt32)HotelGsCmdID.HotelBroadcastCmdid ||
                            p.CmdId == (UInt32)HotelGsCmdID.HotelCloseConnet)
                        {
                            if (p.CmdId == (UInt32)HotelGsCmdID.HotelCreateConnect)
                            {
                                Connect connectInfo = Connect.Parser.ParseFrom(p.Message);
                                hotelMap.Add(connectInfo.RoomID, responseStream);
                            }
                        }
                        else
                        {
                            if (this.responseStream == null)    //MVS消息只用一个channel返回
                            {
                                this.responseStream = responseStream;
                            }
                        }
                        rep = baseServer.DealMsg(p);
                    }

                    if (rep != null)
                    {
                        await responseStream.WriteAsync(rep);
                    }
                });

                await responseTask;
            }
            Logger.Info("Rcv package over!");
        }
        catch (RpcException e)
        {
            Logger.Info("e:", e);
        }

        Logger.Info("requestTask over!");
        baseServer.Disconnect(userID, token);
    }