BnetServer.Network.BnetSession.Send C# (CSharp) Method

Send() public method

public Send ( IMessage message, BnetServiceHash serviceHash = BnetServiceHash.None, uint methodId ) : Task
message IMessage
serviceHash BnetServiceHash
methodId uint
return Task
        public async Task Send(IMessage message, BnetServiceHash serviceHash = BnetServiceHash.None, uint methodId = 0)
        {
            await tlsSemaphore.WaitAsync();

            try
            {
                var messageData = message.ToByteArray();
                var header = new Header
                {
                    Token = messageToken++,
                    ServiceId = 0xFE,
                    Size = (uint)messageData.Length
                };

                if (serviceHash != BnetServiceHash.None)
                {
                    header.ServiceId = 0;
                    header.ServiceHash = (uint)serviceHash;
                    header.MethodId = methodId;
                }

                var headerData = header.ToByteArray();
                var packetData = new byte[2 + headerData.Length + messageData.Length];

                packetData[0] = (byte)(headerData.Length >> 8);
                packetData[1] = (byte)(headerData.Length & 0xFF);

                Buffer.BlockCopy(headerData, 0, packetData, 2, headerData.Length);
                Buffer.BlockCopy(messageData, 0, packetData, 2 + headerData.Length, messageData.Length);

                await tlsStream.WriteAsync(packetData, 0, packetData.Length);
            }
            finally
            {
                // Always release the semaphore to prevent a permanent wait/lock of it.
                tlsSemaphore.Release();
            }
        }

Usage Example

        public static async void HandleVerifyWebCredentialsRequest(VerifyWebCredentialsRequest verifyWebCredentials, BnetSession session)
        {
            var logonResult = new LogonResult();

            if (verifyWebCredentials.WebCredentials.ToStringUtf8() == session.LoginTicket)
            {
                logonResult.AccountId = new EntityId
                {
                    High = 0x100000000000000,
                    Low = session.Account.Id
                };

                session.Account.GameAccounts.ForEach(ga =>
                {
                    logonResult.GameAccountId.Add(new EntityId
                    {
                        // TODO: Build the right High value.
                        High = 0x200000200576F57,
                        Low = ga.Id
                    });
                });

                logonResult.SessionKey = ByteString.CopyFromUtf8(new byte[0].GenerateRandomKey(64).ToHexString());
            }
            else
                logonResult.ErrorCode = (uint)BnetErrorCode.Denied;

            await session.Send(logonResult, BnetServiceHash.AuthenticationListenerService, 5);
        }
All Usage Examples Of BnetServer.Network.BnetSession::Send