WriteShort() public method

public WriteShort ( ushort v ) : void
v ushort
return void
Beispiel #1
0
        /// <summary>
        /// Spawns "other" to "target"
        /// <para>This is soley used for the first spawn of a player</para>
        /// </summary>
        public static void SpawnEntity(Entity target, Entity other, bool mutual = true)
        {
            if (mutual)
            {
                SpawnEntity(other, target, false);
            }
            if (target.VisibleIDs.Contains(other.EntityID))
            {
                return;
            }
            if (target is Player)
            {
                ByteBuffer buffer = new ByteBuffer(Opcodes.SpawnPlayer.length);
                buffer.WriteByte(Opcodes.SpawnPlayer.id);
                buffer.WriteByte(other.EntityID);
                buffer.WriteString(other.HoverName, Encoding.ASCII);
                buffer.WriteShort(other.Position.X);
                buffer.WriteShort(other.Position.Y);
                buffer.WriteShort(other.Position.Z);
                buffer.WriteByte(other.Rotation.X);
                buffer.WriteByte(other.Rotation.Y);
                ((Player)target).SendRaw(buffer.Data);
            }

            target.VisibleIDs.Add(other.EntityID);
            if (mutual)
            {
                SpawnEntity(other, target, false);
            }
        }
Beispiel #2
0
    public void SendMessage(short protocal, short messageID, byte[] buf, string key)
    {
        //if (protocal != Protocal.Heartbeat)
        //{
        //    Util.Log("发送大厅消息,编号=========== " + protocal);
        //}
        // 加密
        ByteBuffer buffer = new ByteBuffer();

        buffer.WriteShort(protocal);
        buffer.WriteShort(messageID);
        if (buf != null)
        {
            //buffer.WriteBytes(SecurityManager.Encrypt(protocal, buf, key));   //加密
            buffer.WriteBytes(buf);
        }
        else
        {
            buffer.WriteInt(0);
        }

        byte[] bytes = buffer.ToBytes();
        WriteMessage(bytes, messageID, protocal);
        buffer.Close();
    }
Beispiel #3
0
    private void Start()
    {
        pb.PlayerSnapShootMsg msg = new pb.PlayerSnapShootMsg();
        msg.username = "******";
        msg.playerId = 9000001;
        byte[] data = ProtobufSerializer.Serialize(msg);
        Debug.LogError("data len " + data.Length);
        ByteBuffer buffer = ByteBuffer.Allocate(1024);

        buffer.WriteShort(1001);
        buffer.WriteShort(1002);
        buffer.WriteShort(1003);
        buffer.WriteLong(10055555);
        buffer.WriteBytes(data);

        short s1 = buffer.ReadShort();
        short s2 = buffer.ReadShort();
        short s3 = buffer.ReadShort();
        long  l1 = buffer.ReadLong();

        byte[] readData = new byte[buffer.ReadableBytes()];
        buffer.ReadBytes(readData, 0, buffer.ReadableBytes());
        Debug.LogError("readData len " + readData.Length);
        pb.PlayerSnapShootMsg dmsg = ProtobufSerializer.DeSerialize <pb.PlayerSnapShootMsg>(readData);
        Debug.LogError("user name " + dmsg.username);

        //socket receive bytes
        //
    }
Beispiel #4
0
        public byte[] ToByteArray()
        {
            using (ByteBuffer oPacket = new ByteBuffer())
            {
                oPacket.WriteShort((short)this.Started.Count);

                foreach (KeyValuePair <ushort, Dictionary <int, short> > quest in this.Started)
                {
                    oPacket.WriteUShort(quest.Key);

                    string kills = string.Empty;

                    foreach (int kill in quest.Value.Values)
                    {
                        kills += kill.ToString().PadLeft(3, '\u0030');
                    }

                    oPacket.WriteString(kills);
                }

                oPacket.WriteShort((short)this.Completed.Count);

                foreach (KeyValuePair <ushort, DateTime> quest in this.Completed)
                {
                    oPacket
                    .WriteUShort(quest.Key)
                    .WriteDateTime(quest.Value);
                }

                oPacket.Flip();
                return(oPacket.GetContent());
            }
        }
Beispiel #5
0
    private void AcceptClient(IAsyncResult ir)
    {
        TcpListener listener  = ir.AsyncState as TcpListener;
        TcpClient   tcpClient = listener.EndAcceptTcpClient(ir);
        //4.创建一个流用来收发数据
        NetworkStream stream = tcpClient.GetStream();

        //读入,也就是接受一个数据
        byte[]          data = new byte[1024];
        Login.RespLogin rsp  = new Login.RespLogin();
        rsp.isWin = true;
        rsp.level = 5;
        MemoryStream ms   = new MemoryStream();
        ByteBuffer   buff = new ByteBuffer();

        ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(ms, rsp);
        data = ms.ToArray();
        buff.WriteShort((ushort)ms.Length);
        buff.WriteShort((ushort)1002);
        buff.WriteBytes(data);
        stream.Write(buff.ToBytes(), 0, buff.ToBytes().Length);
        ms.Close();
        //5.关闭相应的流和监听器
        stream.Close();
        listener.BeginAcceptTcpClient(AcceptClient, listener);
    }
Beispiel #6
0
        public byte[] ToByteArray()
        {
            using (ByteBuffer oPacket = new ByteBuffer())
            {
                oPacket
                .WriteInt(this.MapleID)
                .WriteInt(this.PurchasePrice)
                .WriteInt()     // NOTE: Perfect Pitch.
                .WriteInt()     // NOTE: Time limit.
                .WriteInt();    // NOTE: Unknown.

                if (this.IsRecharageable)
                {
                    oPacket
                    .WriteShort()
                    .WriteInt()
                    .WriteShort((short)(BitConverter.DoubleToInt64Bits(this.UnitPrice) >> 48))
                    .WriteShort(this.MaxPerStack);
                }
                else
                {
                    oPacket
                    .WriteShort(this.Quantity)
                    .WriteShort(this.MaxPerStack);
                }

                oPacket.Flip();
                return(oPacket.GetContent());
            }
        }
Beispiel #7
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteShort(SizeX);
     buf.WriteShort(SizeZ);
     buf.WriteShort(SizeY);
 }
Beispiel #8
0
        public byte[] ToByteArray()
        {
            using (ByteBuffer oPacket = new ByteBuffer())
            {
                oPacket.WriteShort((short)this.Count);

                List <Skill> cooldownSkills = new List <Skill>();

                foreach (Skill loopSkill in this)
                {
                    oPacket.WriteBytes(loopSkill.ToByteArray());

                    if (loopSkill.IsCoolingDown)
                    {
                        cooldownSkills.Add(loopSkill);
                    }
                }

                oPacket.WriteShort((short)cooldownSkills.Count);

                foreach (Skill loopCooldown in cooldownSkills)
                {
                    oPacket
                    .WriteInt(loopCooldown.MapleID)
                    .WriteShort((short)loopCooldown.RemainingCooldownSeconds);
                }

                oPacket.Flip();
                return(oPacket.GetContent());
            }
        }
Beispiel #9
0
    public ByteBuffer encode(IMessage obj)
    {
        ByteBuffer buff    = new ByteBuffer();
        int        protoId = GetProtoIdByType(obj.GetType());

        byte[] result;
        using (MemoryStream ms = new MemoryStream())
        {
            obj.WriteTo(ms);
            result = ms.ToArray();
        }

        UInt16 lengh = (UInt16)(result.Length + 2);

        Debug.Log("lengh" + lengh + ",protoId" + protoId);
        buff.WriteShort((UInt16)lengh);
        PrintBytes(buff.ToBytes());

        buff.WriteShort((UInt16)protoId);
        PrintBytes(buff.ToBytes());

        buff.WriteBytes(result);

        PrintBytes(buff.ToBytes());

        PrintBytes(result);

        return(buff);
    }
Beispiel #10
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteShort(X);
     buf.WriteShort(Z);
     buf.WriteShort(Y);
     buf.WriteByte(Block);
 }
Beispiel #11
0
        public void ShouldWriteShort()
        {
            byteBuffer.WriteShort(inputShort);
            byte[] shortByteLength = new byte[sizeof(short)];
            int    bufferLength    = byteBuffer.Length();

            Assert.Equal(bufferLength, shortByteLength.Length);
        }
Beispiel #12
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteByte(ColorType);
     buf.WriteShort(Red);
     buf.WriteShort(Green);
     buf.WriteShort(Blue);
 }
        /// <summary>
        /// Signals the client that the level has finished loading
        /// </summary>
        public void SendLevelFinalize(Level level)
        {
            ByteBuffer buffer = new ByteBuffer(Opcodes.LevelFinalize);

            buffer.WriteShort((short)level.Width);
            buffer.WriteShort((short)level.Height);
            buffer.WriteShort((short)level.Depth);
            SendRaw(buffer.Data);
        }
Beispiel #14
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteString(TexturePackUrl);
     buf.WriteByte(SideBlock);
     buf.WriteByte(EdgeBlock);
     buf.WriteShort(SideLevel);
     buf.WriteShort(CloudLevel);
     buf.WriteShort(ViewDistance);
 }
Beispiel #15
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteByte((byte)PlayerId);
     buf.WriteShort(ChangeX);
     buf.WriteShort(ChangeZ);
     buf.WriteShort(ChangeY);
     buf.WriteByte(Yaw);
     buf.WriteByte(Pitch);
 }
Beispiel #16
0
        public override byte[] Serialize()
        {
            ByteBuffer buffer = new ByteBuffer(Size);

            buffer.WriteByte(TypeID);
            buffer.WriteShort((short)Name.Length);
            buffer.WriteString(Name, Encoding.ASCII, Name.Length);
            buffer.WriteShort(short.Parse(Value.ToString()));
            return(buffer.Data);
        }
Beispiel #17
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteByte((byte)PlayerId);
     buf.WriteShort(Location.X);
     buf.WriteShort(Location.Z);
     buf.WriteShort(Location.Y);
     buf.WriteByte(Yaw);
     buf.WriteByte(Pitch);
 }
Beispiel #18
0
 public void Write(ByteBuffer buf) {
     buf.WriteByte(Id);
     buf.WriteByte((byte)PlayerId);
     buf.WriteString(PlayerName);
     buf.WriteShort(Location.Location.X);
     buf.WriteShort(Location.Location.Z);
     buf.WriteShort(Location.Location.Y);
     buf.WriteByte(Location.Rotation);
     buf.WriteByte(Location.Look);
     buf.Purge();
 }
        /// <summary>
        /// Sends a blockchange to the client
        /// </summary>
        public void SendBlockchange(short X, short Y, short Z, byte Tile)
        {
            Tile = Level.BlockDefinitions.GetFallback(Tile, UseCustomBlocks, UseBlockDefinitions);
            ByteBuffer buffer = new ByteBuffer(Opcodes.SetBlock);

            buffer.WriteShort(X);
            buffer.WriteShort(Y);
            buffer.WriteShort(Z);
            buffer.WriteByte(Tile);
            SendRaw(buffer.Data);
        }
Beispiel #20
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteByte((byte)PlayerId);
     buf.WriteString(PlayerName);
     buf.WriteShort(X);
     buf.WriteShort(Z);
     buf.WriteShort(Y);
     buf.WriteByte(Yaw);
     buf.WriteByte(Pitch);
 }
Beispiel #21
0
 public void Write(ByteBuffer buf)
 {
     buf.WriteByte(Id);
     buf.WriteByte(EntityId);
     buf.WriteString(InGameName);
     buf.WriteString(SkinName);
     buf.WriteShort(Spawn.X);
     buf.WriteShort(Spawn.Z);
     buf.WriteShort(Spawn.Y);
     buf.WriteByte(SpawnYaw);
     buf.WriteByte(SpawnPitch);
 }
Beispiel #22
0
        public override byte[] Serialize()
        {
            string     val    = (string)Value;
            ByteBuffer buffer = new ByteBuffer(Size);

            buffer.WriteByte(TypeID);
            buffer.WriteShort((short)Name.Length);
            buffer.WriteString(Name, Encoding.ASCII, Name.Length);
            buffer.WriteShort((short)val.Length);
            buffer.WriteString(val, Encoding.ASCII, val.Length);
            return(buffer.Data);
        }
Beispiel #23
0
    /// <summary>
    /// 交给Command,这里不想关心发给谁。
    /// </summary>
    void Update()
    {
        if (sEvents.Count > 0)
        {
            lock (sEvents)
            {
                while (sEvents.Count > 0)
                {
                    ServerEvent _event = sEvents.Dequeue();
                    if (_event == null)
                    {
                        Util.LogError("ServerType is null");
                        continue;
                    }
                    Distribute(_event._type, _event.Key, _event.Value);
                }
            }
        }
        lock (timeOutList)
        {
            if (timeOutList.Count > 0)
            {
                float curTime = Time.unscaledTime;

                for (int i = timeOutList.Count - 1; i >= 0; i--)
                {
                    if (isStopUpdate)
                    {
                        isStopUpdate = false;
                        break;
                    }
                    MessageTimeOut mto = timeOutList[i];
                    if (curTime - mto.beginTime >= AppConst.SendMessageTimeOut)
                    {     //超时了
                        if (IsHandleTimeOut(mto.protocal))
                        { //超时处理
                            Util.LogError("server->" + mto.server + "--protocal-->" + mto.protocal + "--messageID-->" + mto.messageID + "--timeout");

                            ByteBuffer buffer = new ByteBuffer();
                            buffer.WriteShort((short)mto.server);
                            buffer.WriteShort(mto.protocal);

                            ByteBuffer _buffer = new ByteBuffer(buffer.ToBytes());
                            buffer.Close();
                            AddEvent(ServerType.Logic, ClientProtocal.TimeOut, _buffer);
                            //EventManager.instance.NotifyEvent(NetEventType.TimeOut, mto.protocal);
                        }
                        timeOutList.Remove(mto);
                    }
                }
            }
        }
    }
        /// <summary>
        /// Sends the player to the level spawn
        /// </summary>
        public void SpawnSelf()
        {
            ByteBuffer buffer = new ByteBuffer(Opcodes.SpawnPlayer);

            buffer.WriteByte(255);
            buffer.WriteString(HoverName, Encoding.ASCII);
            buffer.WriteShort(Level.Spawn.X);
            buffer.WriteShort(Level.Spawn.Y);
            buffer.WriteShort(Level.Spawn.Z);
            buffer.WriteByte(Level.SpawnRot.X);
            buffer.WriteByte(Level.SpawnRot.Y);
            SendRaw(buffer.Data);
        }
Beispiel #25
0
        public bool SendPb(ProtoBuf.IExtensible proto)
        {
            ByteBuffer buff = new ByteBuffer();

            byte[] contents = ProtoUtil.Serialize(proto);

            buff.WriteShort((ushort)(contents.Length + 2));
            buff.WriteShort((ushort)ProtoUtil.GetProtoType(proto));
            buff.WriteBytes(contents);

            this.SendBytes(buff.ToBytes());
            return(true);
        }
Beispiel #26
0
    public byte[] ToBytes()
    {
        ByteBuffer byteBuffer = new ByteBuffer();

        byteBuffer.WriteByte((Byte)PrefabName.Length);
        byteBuffer.WriteString(PrefabName);

        byteBuffer.WriteInt((int)(X * 100f));
        byteBuffer.WriteInt((int)(Y * 100f));
        byteBuffer.WriteInt((int)(Z * 100f));

        byteBuffer.WriteShort((short)(RotationX * 100f));
        byteBuffer.WriteShort((short)(RotationX * 100f));
        byteBuffer.WriteShort((short)(RotationX * 100f));


        byteBuffer.WriteShort((short)(ScaleX * 100f));
        byteBuffer.WriteShort((short)(ScaleY * 100f));
        byteBuffer.WriteShort((short)(ScaleZ * 100f));

        byteBuffer.WriteByte((Byte)(type));
        if (type != GameObjectTypes.Block)
        {
            return(byteBuffer.ToBytes());
        }
        byteBuffer.WriteByte((Byte)(ColliderType));
        byteBuffer.WriteShort((Byte)(Radius * 100f));
        if (ColliderType == ColliderTypes.CapsuleCollider)
        {
            byteBuffer.WriteShort((Byte)(Height * 100f));
        }

        return(byteBuffer.ToBytes());
    }
        public override byte[] ToByteArray()
        {
            using (ByteBuffer writer = new ByteBuffer())
            {
                writer.WriteShort(this.Position.X);
                writer.WriteShort(this.Position.Y);
                writer.WriteShort(this.Wobble.X);
                writer.WriteShort(this.Wobble.Y);
                writer.WriteByte(this.NewStance);

                writer.Flip();
                return(writer.GetContent());
            }
        }
        public override byte[] ToByteArray()
        {
            using (ByteBuffer writer = new ByteBuffer())
            {
                writer.WriteByte(this.Type);
                writer.WriteShort(this.Delta.X);
                writer.WriteShort(this.Delta.Y);
                writer.WriteByte(this.NewStance);
                writer.WriteShort(this.Duration);

                writer.Flip();
                return(writer.GetContent());
            }
        }
Beispiel #29
0
    private ByteBuffer createByteBuffer(ushort msgId, MemoryStream stream)
    {
        ByteBuffer buffer = new ByteBuffer();

        buffer.setMsgId(msgId);
        buffer.WriteInt(stream != null ? (int)stream.Length : 0);
        buffer.WriteShort(msgId);
        buffer.WriteShort(0);
        if (stream != null)
        {
            buffer.WriteStream(stream);
        }
        return(buffer);
    }
Beispiel #30
0
        public override byte[] GetDataToSend(LightingConsole console)
        {
            ByteBuffer buf = new ByteBuffer().Write(console.BdstNo).Write(SUB_CMD).Write((short)Dst);

            if (sks != null)
            {
                buf.WriteShort(1);
                sks.Sort((n1, n2) =>
                {
                    if (n1.Number > n2.Number)
                    {
                        return(-1);
                    }
                    return(n2.Number > n1.Number ? 1 : 0);
                });

                // assume they are all on the same line
                buf.WriteShort(1);
                int ptr = 0;
                for (int i = 0; i < 512; i++)
                {
                    if (sks.Count > ptr && sks[ptr].Number == i + 1)
                    {
                        buf.Write(sks[ptr].Intensity);
                        ptr++;
                    }
                    else
                    {
                        buf.Write((byte)0);
                    }
                }
            }
            else if (data != null)
            {
                buf.Write(data.Count);

                for (int i = 0; i < data.Count; i++)
                {
                    buf.WriteShort((short)i);
                    buf.Write(data[i]);
                }
            }
            else
            {
                buf.WriteShort(0);
            }

            return(buf.ToArray());
        }
Beispiel #31
0
    /// <summary>
    /// 丢失链接
    /// </summary>
    void OnDisconnected(DisType dis, string msg)
    {
        Close();   //关掉客户端链接
        int protocal = dis == DisType.Exception ?
        Protocal.Exception : Protocal.Disconnect;

        ByteBuffer buffer = new ByteBuffer();
        buffer.WriteShort((ushort)protocal);
        
		//NetworkManager.AddEvent(protocal, buffer);
		//TODO: notify Disconnected
        Debug.LogError("Connection was closed by the server:>" + msg + " Distype:>" + dis);
    }
    /// <summary>
    /// 丢失链接
    /// </summary>
    void OnDisconnected(DisType dis, string msg) {
        sockteClientState = netState.DisConnected;
        Close();   //关掉客户端链接
        int protocal = dis == DisType.Exception ?
        Protocal.Exception : Protocal.Disconnect;

        ByteBuffer buffer = new ByteBuffer();
        buffer.WriteShort((ushort)protocal);
        lock (NetworkManager.sEvents)
        {
            NetworkManager.AddEvent(protocal, buffer);
        }
        //Debug.LogError("Connection was closed by the server:>" + msg + " Distype:>" + dis);
    }