Esempio n. 1
0
        public void Add(Client client, ulong objectId)
        {
            client.Channel = this;
            var builder = bnet.protocol.channel.AddNotification.CreateBuilder();
            var identity = bnet.protocol.Identity.CreateBuilder();
            //identity.SetAccountId(client.Account.BnetAccountID);
            //identity.SetGameAccountId(client.Account.BnetGameAccountID);
            if (client.Account.Toons.Count > 0) identity.SetToonId(client.Account.Toons.First().Value.BnetEntityID);

            var state = bnet.protocol.channel.MemberState.CreateBuilder().AddRole(2).SetPrivileges(64511);
            var selfBuilder = bnet.protocol.channel.Member.CreateBuilder().SetIdentity(identity.Build()).SetState(state.Build());
            var self = selfBuilder.Build();
            builder.SetSelf(self);

            if (client.Account.Toons.Count > 0)
            {
                var memberIdentity = bnet.protocol.Identity.CreateBuilder();
                memberIdentity.SetToonId(client.Account.Toons.First().Value.BnetEntityID);

                var memberState = bnet.protocol.channel.MemberState.CreateBuilder().AddRole(2).SetPrivileges(64511);

                var member = bnet.protocol.channel.Member.CreateBuilder().SetIdentity(memberIdentity).SetState(memberState.Build());
                builder.AddMember(self);
            }

            builder.SetChannelState(this.State);

            client.CallMethod(bnet.protocol.channel.ChannelSubscriber.Descriptor.FindMethodByName("NotifyAdd"), null, builder.Build(), null, r => { }, objectId);
        }
Esempio n. 2
0
        public void ListenForGame(Client client)
        {
            var connectionInfo =
                bnet.protocol.game_master.ConnectInfo.CreateBuilder().SetToonId(client.CurrentToon.BnetEntityID).SetHost
                    ("127.0.0.1").SetPort((int)this.ID + 16000).SetToken(ByteString.CopyFrom(new byte[] {0x07, 0x34, 0x02, 0x60, 0x91, 0x93, 0x76, 0x46, 0x28, 0x84}))
                    .AddAttribute(bnet.protocol.attribute.Attribute.CreateBuilder().SetName("SGameId").SetValue(bnet.protocol.attribute.Variant.CreateBuilder().SetIntValue(2014314530).Build())).Build();
                 
            var builder = bnet.protocol.game_master.GameFoundNotification.CreateBuilder();
            builder.AddConnectInfo(connectionInfo);
            builder.SetRequestId(this.RequestID);
            builder.SetGameHandle(this.GameHandle);

            Logger.Trace("Game spawned: {0}:{1}", connectionInfo.Host, connectionInfo.Port);
            client.CallMethod(bnet.protocol.game_master.GameFactorySubscriber.Descriptor.FindMethodByName("NotifyGameFound"), builder.Build(), this.ID);
        }
Esempio n. 3
0
        private void AcceptCallback(IAsyncResult result)
        {
            try
            {
                var socket = Listener.EndAccept(result); // Finish accepting the incoming client.
                var connection = new Client(this, socket); // Add the new connection to the dictionary.

                lock (ConnectionLock) Connections[socket] = connection; // add the client to list.

                OnClientConnection(new ClientEventArgs(connection)); // Raise the ClientConnected event.

                connection.BeginReceive(ReceiveCallback, connection); // Begin receiving on the new client connection.
                Listener.BeginAccept(AcceptCallback, null); // Continue receiving other incoming clients asynchronously.
            }
            catch (Exception e)
            {
                Logger.DebugException(e, "AcceptCallback");
            }
        }
Esempio n. 4
0
        public void Add(Client client)
        {
            var identity = client.GetIdentity(false, false, true);
            var user = bnet.protocol.channel.Member.CreateBuilder()
                .SetIdentity(identity)
                .SetState(bnet.protocol.channel.MemberState.CreateBuilder()
                    .AddRole(2)
                    .SetPrivileges(0xFBFF) // 64511
                    .Build())
                .Build();
            this.Members.Add(user);

            var builder = bnet.protocol.channel.AddNotification.CreateBuilder()
                .SetChannelState(this.State)
                .SetSelf(user);
            
            // Cap includes the user that was added
            foreach (var m in this.Members)
            {
                builder.AddMember(m);
            }
            client.CallMethod(bnet.protocol.channel.ChannelSubscriber.Descriptor.FindMethodByName("NotifyAdd"), builder.Build(), this.ID);
        }
Esempio n. 5
0
        public void NotifyChannelState(Client client)
        {
            var field1 =
                bnet.protocol.presence.Field.CreateBuilder().SetKey(
                    bnet.protocol.presence.FieldKey.CreateBuilder().SetProgram(16974).SetGroup(3).SetField(3).SetIndex(0)
                        .Build()).SetValue(bnet.protocol.attribute.Variant.CreateBuilder().SetBoolValue(true).Build()).Build();

            var field2 =
                bnet.protocol.presence.Field.CreateBuilder().SetKey(
                    bnet.protocol.presence.FieldKey.CreateBuilder().SetProgram(16974).SetGroup(3).SetField(10).SetIndex(0)
                        .Build()).SetValue(bnet.protocol.attribute.Variant.CreateBuilder().SetIntValue(1315530390868296).Build()).Build();

            var field3 =
                bnet.protocol.presence.Field.CreateBuilder().SetKey(
                    bnet.protocol.presence.FieldKey.CreateBuilder().SetProgram(16974).SetGroup(3).SetField(11).SetIndex(0)
                        .Build()).SetValue(bnet.protocol.attribute.Variant.CreateBuilder().SetMessageValue(
                                ByteString.CopyFrom(new byte[]
                                                        {
                                                            0x9, 0x46, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4,
                                                            0x11, 0xdd, 0xb4, 0x63, 0xe7, 0x82, 0x44, 0x68, 0x4e
                                                        })).Build()).Build();


            var fieldOperation1 = bnet.protocol.presence.FieldOperation.CreateBuilder().SetField(field1).Build();
            var fieldOperation2 = bnet.protocol.presence.FieldOperation.CreateBuilder().SetField(field2).Build();
            var fieldOperation3 = bnet.protocol.presence.FieldOperation.CreateBuilder().SetField(field3).Build();

            var state =
                bnet.protocol.presence.ChannelState.CreateBuilder().SetEntityId(this.BnetEntityID).AddFieldOperation(
                    fieldOperation1).AddFieldOperation(fieldOperation2).AddFieldOperation(fieldOperation3).Build();


            var channelState = bnet.protocol.channel.ChannelState.CreateBuilder().SetExtension(bnet.protocol.presence.ChannelState.Presence, state);
            var builder = bnet.protocol.channel.UpdateChannelStateNotification.CreateBuilder().SetStateChange(channelState).Build();

            client.CallMethod(bnet.protocol.channel.ChannelSubscriber.Descriptor.FindMethodByName("NotifyUpdateChannelState"), null, builder, null, r => { });
        }
Esempio n. 6
0
 public bool HasUser(Client client)
 {
     return this.Members.Any(m => m.Identity == client.GetIdentity(false, false, true));
 }
Esempio n. 7
0
 /*public void Close()
 {
     RemoveAllUsers();
 }
 
 public void RemoveAllUsers()
 {
     // Need a way to iterate clients on the server to send a NotifyRemove
     // and then call RemoveUser on them
     this.Members.Clear();
 }*/
 
 public void RemoveUser(Client client)
 {
     var identity = client.GetIdentity(false, false, true);
     var builder = bnet.protocol.channel.RemoveNotification.CreateBuilder()
         .SetMemberId(identity.ToonId);
     this.Members.RemoveAll(m => identity == m.Identity);
     client.CurrentChannel = null;
     client.CallMethod(bnet.protocol.channel.ChannelSubscriber.Descriptor.FindMethodByName("NotifyRemove"), builder.Build(), this.ID);
 }
Esempio n. 8
0
 private void RemoveConnection(Client connection, bool raiseEvent)
 {
     // Remove the connection from the dictionary and raise the OnDisconnection event.
     lock (ConnectionLock) 
         if (Connections.Remove(connection.Socket) && raiseEvent)
             OnClientDisconnect(new ClientEventArgs(connection));
 }
Esempio n. 9
0
        public virtual void Disconnect(Client connection)
        {
            if (connection == null) throw new ArgumentNullException("connection");
            if (!connection.IsConnected) return;

            connection.Socket.Disconnect(false);
            RemoveConnection(connection, true);
        }             
Esempio n. 10
0
        public virtual int Send(Client connection, byte[] buffer, int start, int count, SocketFlags flags)
        {
            if (connection == null) throw new ArgumentNullException("connection");
            if (buffer == null) throw new ArgumentNullException("buffer");

            var totalBytesSent = 0;
            var bytesRemaining = buffer.Length;

            try
            {
                while (bytesRemaining > 0) // Ensure we send every byte.
                {
                    var bytesSent = connection.Socket.Send(buffer, totalBytesSent, bytesRemaining, flags); // Send the remaining data.                    
                    if (bytesSent > 0)
                        OnDataSent(new ClientDataEventArgs(connection, buffer.Enumerate(totalBytesSent, bytesSent))); // Raise the Data Sent event.

                    // Decrement bytes remaining and increment bytes sent.
                    bytesRemaining -= bytesSent;
                    totalBytesSent += bytesSent;
                }
            }
            catch (SocketException)
            {
                RemoveConnection(connection, true); // An error occured while sending, client has disconnected.
            }
            catch (Exception e)
            {
                Logger.DebugException(e, "Send");
            }

            return totalBytesSent;
        }
Esempio n. 11
0
        public virtual int Send(Client connection, IEnumerable<byte> data, SocketFlags flags)
        {
            if (connection == null) throw new ArgumentNullException("connection");
            if (data == null) throw new ArgumentNullException("data");

            var buffer = data.ToArray();
            return Send(connection, buffer, 0, buffer.Length, SocketFlags.None);
        }