Exemple #1
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count   = 0;
        bool   success = true;

        Span <byte> s = new Span <byte>(segment.Array, segment.Offset, segment.Count);

        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)PacketID.C_PlayerInfoReq);
        count   += sizeof(ushort);
        segment.Array[segment.Offset + count] = (byte)this.testByte;
        count   += sizeof(byte);
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), this.playerId);
        count   += sizeof(long);
        ushort nameLen = (ushort)Encoding.Unicode.GetBytes(this.name, 0, this.name.Length, segment.Array, segment.Offset + count + sizeof(ushort));

        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), nameLen);
        count   += sizeof(ushort);
        count   += nameLen;
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)this.skills.Count);
        count   += sizeof(ushort);
        foreach (Skill skill in this.skills)
        {
            success = skill.Write(s, ref count);
        }

        success &= BitConverter.TryWriteBytes(s, count);
        if (success == false)
        {
            return(null);
        }
        return(SendBufferHelper.Close(count));
    }
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort pos = 0;


        pos += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.S_BroadcastEnterGame), 0, segment.Array, segment.Offset + pos, sizeof(ushort));
        pos += sizeof(ushort);

        Array.Copy(BitConverter.GetBytes(playerId), 0, segment.Array, segment.Offset + pos, sizeof(int));
        pos += sizeof(int);


        Array.Copy(BitConverter.GetBytes(posX), 0, segment.Array, segment.Offset + pos, sizeof(float));
        pos += sizeof(float);


        Array.Copy(BitConverter.GetBytes(posY), 0, segment.Array, segment.Offset + pos, sizeof(float));
        pos += sizeof(float);


        Array.Copy(BitConverter.GetBytes(posZ), 0, segment.Array, segment.Offset + pos, sizeof(float));
        pos += sizeof(float);

        Array.Copy(BitConverter.GetBytes(pos), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(pos));
    }
    //Serializer
    public override ArraySegment <byte> Ser()
    {
        ArraySegment <byte> s = SendBufferHelper.Open(128);

        ushort _size = 0;


        ToBytes(s.Array, s.Offset + _size, p_size);
        _size += sizeof(short);
        ToBytes(s.Array, s.Offset + _size, p_id);
        _size += sizeof(short);
        ToBytes(s.Array, s.Offset + _size, player_id);
        _size += sizeof(long);



        //String Message
        ToBytes(s.Array, s.Offset + _size, message.Length * 2);
        _size += sizeof(short);
        ushort nameLen = (ushort)Encoding.Unicode.GetBytes(this.message, 0, this.message.Length, s.Array, s.Offset + _size);

        _size += sizeof(long);
        _size += nameLen;
        return(SendBufferHelper.Close(_size));
    }
Exemple #4
0
        public override void OnConnected(EndPoint endPoint)
        {
            Console.WriteLine($"{endPoint}에 연결되었습니다.");

            // * 수정된 부분
            // 패킷을 전송한다. Send Buffer때 패킷을 전송하던 방식을 이용한다.
            Packet packet = new Packet()
            {
                packetSize = 4, packetId = 7
            };

            for (int i = 0; i < 5; i++)
            {
                byte[] buffer1 = BitConverter.GetBytes(packet.packetSize);
                byte[] buffer2 = BitConverter.GetBytes(packet.packetId);

                ArraySegment <byte> openSegment = SendBufferHelper.Open(4096);
                Array.Copy(buffer1, 0, openSegment.Array, openSegment.Offset, buffer1.Length);
                Array.Copy(buffer2, 0, openSegment.Array, openSegment.Offset + buffer1.Length, buffer2.Length);

                ArraySegment <byte> sendBuff = SendBufferHelper.Close(packet.packetSize);

                Send(sendBuff);
            }
        }
Exemple #5
0
    public ArraySegment <byte> Write()
    {
        ushort count   = 0;
        bool   success = true;

        ArraySegment <byte> sg   = SendBufferHelper.Open(4096);
        Span <byte>         span = new Span <byte>(sg.Array, sg.Offset, sg.Count); // span을 만든 이유는 어차피 spawn을 인자로 써야하기때문에

        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), (ushort)PacketID.S_Chat); // 시작범위부터 남은공간까지 오른쪽에 인자 값을 넣어라
        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), this.playerId);
        count   += sizeof(int);
        ushort chatLen = (ushort)Encoding.Unicode.GetBytes(this.chat, 0, this.chat.Length, sg.Array, sg.Offset + count + sizeof(ushort));

        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), chatLen);
        count   += sizeof(ushort);
        count   += chatLen;
        success &= BitConverter.TryWriteBytes(span, count);
        if (success == false)
        {
            return(null);
        }
        return(SendBufferHelper.Close(count));
    }
Exemple #6
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);

        ushort count = 0;

        count += sizeof(ushort); // 패킷의 사이즈는 나중에 정함
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.S_Chat), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes(this.playerId), 0, segment.Array, segment.Offset + count, sizeof(int));
        count += sizeof(int);
        ushort chatLen = (ushort)Encoding.Unicode.GetBytes(chat, 0, chat.Length, segment.Array, segment.Offset + count + sizeof(ushort));

        Array.Copy(BitConverter.GetBytes(chatLen), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        count += chatLen;

        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        // 패킷사이즈



        return(SendBufferHelper.Close(count));
    }
Exemple #7
0
        public override void OnConnected(EndPoint endPoint)
        {
            Console.WriteLine($"{endPoint}가 접속했습니다.");
            // * 수정된 부분
            // (기존) Send("Welcome to Jiho's Server.");
            //----------------------------------------------------------------------
            // 이제 Send Buffer를 받아서 그쪽에 보낼 내용을 입력한다.
            byte[] buffer1 = Encoding.UTF8.GetBytes("Welcome to Jiho's Server.\n Your ID : ");
            byte[] buffer2 = BitConverter.GetBytes(7777);

            // 보낼 내용이 작성될 버퍼. 넉넉한 크기로 요청한다.
            ArraySegment <byte> openSegment = SendBufferHelper.Open(4096);

            // 버퍼에 내용을 작성한다.
            Array.Copy(buffer1, 0, openSegment.Array, openSegment.Offset, buffer1.Length);
            Array.Copy(buffer2, 0, openSegment.Array, openSegment.Offset + buffer1.Length, buffer2.Length);

            // 작성한 내용의 크기를 입력하고 버퍼를 닫은 후 작성한 내용이 있는 부분의 ArraySegment를 반환받는다.
            ArraySegment <byte> sendBuff = SendBufferHelper.Close(buffer1.Length + buffer2.Length);

            // 해당 내용을 전송한다.
            Send(sendBuff);
            //----------------------------------------------------------------------
            Thread.Sleep(1000);

            Disconnect();
        }
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count   = 0;
        bool   success = true;

        Span <byte> s = new Span <byte>(segment.Array, segment.Offset, segment.Count);

        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)PacketID.S_Chat);
        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), this.playerId);
        count   += sizeof(int);
        ushort chatLen = (ushort)Encoding.Unicode.GetBytes(this.chat, 0, this.chat.Length, segment.Array, segment.Offset + count + sizeof(ushort));

        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), chatLen);
        count   += sizeof(ushort);
        count   += chatLen;
        success &= BitConverter.TryWriteBytes(s, count);
        if (success == false)
        {
            return(null);
        }
        return(SendBufferHelper.Close(count));
    }
        public override ArraySegment <byte> Serialize()
        {
            ArraySegment <byte> arr = SendBufferHelper.Open(4096);

            ushort count   = 0;
            bool   success = true;

            // * 수정된 부분
            // Span.Slice 를 이용하여, 직렬화할 영역을 지정할 때 Span을 부분적으로 잘라서 사용한다.
            // Span은 받아온 버퍼의 메모리 영역을 가리키도록 초기화하여 한 번만 선언한다.
            Span <byte> span = new Span <byte>(arr.Array, arr.Offset, arr.Count);

            // Span에서 데이터가 입력된 양(count)만큼 뺀 공간을 지정한다. 첫 2바이트(데이터 크기)는 비워둔다.
            // Span.Slice : 해당 Span의 일부를 가리키는 Span을 반환한다. 매개변수로 입력된 Span은 변하지 않는다.
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), packetId);
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), playerId);
            count   += sizeof(long);

            // + 추가된 부분
            // string 타입 직렬화하기
            //-----------------------------------------------------------------------------
            // 크기가 가변적이므로, 데이터 크기(ushort) + 데이터 형태로 저장한다.

            // 1. string 타입에 입력된 값의 바이트 수를 저장해둔다. (Unicode는 UTF-16을 의미)
            ushort nameLen = (ushort)Encoding.Unicode.GetByteCount(playerName);

            // 2. string 데이터 크기(ushort 타입)를 먼저 직렬화한다.
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), nameLen);
            count   += sizeof(ushort);

            // 3. 버퍼에 string 크기만큼의 영역을 지정해서, string 데이터를 인코딩하여 저장한다.
            Array.Copy(Encoding.Unicode.GetBytes(playerName), 0, arr.Array, arr.Offset + count, nameLen);
            count += nameLen;
            //-----------------------------------------------------------------------------
            // 좀 더 효율적인 방식으로, GetBytes에 오버로딩된 버전을 이용하면 지정한 메모리에 인코딩된 내용을 바로 넣을 수 있다.

            // 1. 메모리 주소를 지정하여 string을 변환해서 넣는다. 작성된 바이트 수가 반환된다. string의 크기를 입력하기 위해 2바이트는 비워둔다.
            // 매개 변수: (인코딩할 문자열, 문자열의 시작점, 문자열 길이(글자수), 작성할 배열, 작성할 배열의 시작점 인덱스)
            nameLen = (ushort)Encoding.Unicode.GetBytes(nickName, 0, nickName.Length, arr.Array, arr.Offset + count + sizeof(ushort));

            // 2. 비워둔 2바이트에 위에서 반환된 문자열 길이를 작성하고, '문자열 길이 + 2' 바이트만큼 count를 추가한다.
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), nameLen);
            count   += sizeof(ushort);
            count   += nameLen;
            //-----------------------------------------------------------------------------

            // 첫 2바이트에는 직렬화한 데이터 크기가 들어가므로 span의 시작부분에 최종 count를 입력해준다.
            success &= BitConverter.TryWriteBytes(span, count);

            if (!success)
            {
                return(null);
            }

            return(SendBufferHelper.Close(count));
        }
        public ArraySegment <byte> Write()
        {
            ArraySegment <byte> segment = SendBufferHelper.Open(4096);
            ushort      count           = 0;
            bool        success         = true;
            Span <byte> s = new Span <byte>(segment.Array, segment.Offset, segment.Count);

            //success &= BitConverter.TryWriteBytes(new Span<byte>(s.Array, s.Offset, s.Count), packet.size);
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)PacketID.PlayerInfoReq);
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), this.playerId);
            count   += sizeof(long);
            //string
            //ushort nameLen = (ushort)Encoding.Unicode.GetByteCount(this.name);
            //success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), nameLen); // nameLen 값을 쓴다
            //count += sizeof(ushort);
            //Array.Copy(Encoding.Unicode.GetBytes(this.name), 0, segment.Array, count, nameLen); //nameLen 값 만큼 쓴다
            //count += nameLen;

            ushort nameLen = (ushort)Encoding.Unicode.GetBytes(this.name, 0, this.name.Length, segment.Array, segment.Offset + count + sizeof(ushort));

            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), nameLen);
            count   += sizeof(ushort);
            count   += nameLen;

            //skill list
            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)skills.Count);
            count   += sizeof(ushort);
            foreach (SkillInfo skill in skills)
            {
                success &= skill.Write(s, ref count);
            }

            success &= BitConverter.TryWriteBytes(s, count);
            //byte[] size = BitConverter.GetBytes(packet.size);
            //byte[] packetId = BitConverter.GetBytes(packet.packetId);
            //byte[] playerId = BitConverter.GetBytes(packet.playerId);

            //Array.Copy(size, 0, s.Array, s.Offset+count, 2);
            //count += 2;
            //Array.Copy(packetId, 0, s.Array, s.Offset + count, 2);
            //count += 2;
            //Array.Copy(playerId, 0, s.Array, s.Offset+count, 8);
            //count += 8;
            if (success == false)
            {
                return(null);
            }
            return(SendBufferHelper.Close(count));
            //궁금점? : 왜 큰 버퍼를 두고 잘라 사용해야하는가? 길이를 안다면 그만큼만 보내면 되는 것 아닌가?
            // -> 가변적 + 메모리 낭비 줄이기
        }
Exemple #11
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count = 0;

        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.LeaveGame), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);


        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
Exemple #12
0
        public override ArraySegment <byte> Write()
        {
            ArraySegment <byte> segment = SendBufferHelper.Open(4096);

            ushort count   = 0;
            bool   success = true;

            Span <byte> s = new Span <byte>(segment.Array, segment.Offset, segment.Count);

            // success &= BitConverter.TryWriteBytes(new Span<byte>(s.Array, s.Offset + count, s.Count - count), packet.size);  // 공간이 모자르면 실패
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), this.packetId);  // 공간이 모자르면 실패
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), this.playerId);  // 공간이 모자르면 실패
            count   += sizeof(long);

            // string - 2단계로 나눠서보내기
            // string len[2]
            // byte []
            //ushort nameLen = (ushort)Encoding.Unicode.GetByteCount(this.name);
            //success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), nameLen);
            //count += sizeof(ushort);
            //Array.Copy(Encoding.Unicode.GetBytes(this.name), 0, segment.Array, count, nameLen);
            //count += nameLen;

            // string 직렬화하는 또다른 방법
            ushort nameLen = (ushort)Encoding.Unicode.GetBytes(this.name, 0, this.name.Length, segment.Array, segment.Offset + count + sizeof(ushort));

            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), nameLen);
            count   += sizeof(ushort);
            count   += nameLen;

            // skill list
            success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)skills.Count);
            count   += sizeof(ushort);

            foreach (SkillInfo skill in skills)
            {
                success &= skill.Write(s, ref count);
            }

            success &= BitConverter.TryWriteBytes(s, count);  // 패킷 사이즈는 마지막에 결정됨

            if (success == false)
            {
                return(null);
            }

            return(SendBufferHelper.Close(count));
        }
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort pos = 0;


        pos += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.C_LeaveGame), 0, segment.Array, segment.Offset + pos, sizeof(ushort));
        pos += sizeof(ushort);

        Array.Copy(BitConverter.GetBytes(pos), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(pos));
    }
    //Serializer
    public override ArraySegment <byte> Ser()
    {
        ArraySegment <byte> s = SendBufferHelper.Open(4096);

        ushort _size = 0;

        ToBytes(s.Array, s.Offset + _size, p_size);
        _size += sizeof(ushort);
        ToBytes(s.Array, s.Offset + _size, p_id);
        _size += sizeof(ushort);
        ToBytes(s.Array, s.Offset + _size, player_id);
        _size += sizeof(ulong);

        return(SendBufferHelper.Close(_size));
    }
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count   = 0;
        bool   success = true;

        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.S_BroadcastLeaveGame), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);

        Array.Copy(BitConverter.GetBytes(this.playerId), 0, segment.Array, segment.Offset + count, sizeof(int));
        count += sizeof(int);

        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count = 0;

        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.C_Chat), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        ushort chatLength = (ushort)Encoding.Unicode.GetBytes(this.chat, 0, this.chat.Length, segment.Array, segment.Offset + count + sizeof(ushort)); // nameLength가 들어갈 공간만큼 뒤로 밀어준다.

        Array.Copy(BitConverter.GetBytes(chatLength), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        count += chatLength;

        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
Exemple #17
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count = 0;

        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.PlayerList), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)this.players.Count), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        foreach (Player player in this.players)
        {
            player.Write(segment, ref count);
        }

        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
        public override ArraySegment <byte> Serialize()
        {
            ArraySegment <byte> arr = SendBufferHelper.Open(4096);

            ushort count   = 0;
            bool   success = true;

            Span <byte> span = new Span <byte>(arr.Array, arr.Offset, arr.Count);

            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), packetId);
            count   += sizeof(ushort);
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), playerId);
            count   += sizeof(long);

            // string
            ushort nameLen = (ushort)Encoding.Unicode.GetBytes(playerName, 0, playerName.Length, arr.Array, arr.Offset + count + sizeof(ushort));

            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), nameLen);
            count   += sizeof(ushort);
            count   += nameLen;

            // * 추가된 부분
            //-------------------------------------------------------------------------------
            // 리스트는 기본적으로 string처럼 요소 개수를 먼저 계산하고, 컨테이너를 순회하며 직렬화를 반복하면 된다.
            success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), (ushort)skills.Count);
            count   += sizeof(ushort);
            // count는 참조형(ref)으로 받으므로 따로 여기서 제어할 필요는 없다.
            foreach (SkillInfo skill in skills)
            {
                success &= skill.Serialize(span, ref count);
            }
            //-------------------------------------------------------------------------------

            success &= BitConverter.TryWriteBytes(span, count);

            if (!success)
            {
                return(null);
            }

            return(SendBufferHelper.Close(count));
        }
Exemple #19
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count = 0;

        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.BroadcastEnterGame), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes(playerId), 0, segment.Array, segment.Offset + count, sizeof(int));
        count += sizeof(int);
        if (position == null)
        {
            position = new Position();
        }
        position.Write(segment, ref count);

        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
Exemple #20
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count   = 0;
        bool   success = true;

        Span <byte> s = new Span <byte>(segment.Array, segment.Offset, segment.Count);

        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), (ushort)PacketID.S_Test);
        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(s.Slice(count, s.Length - count), this.testInt);
        count   += sizeof(int);
        success &= BitConverter.TryWriteBytes(s, count);
        if (success == false)
        {
            return(null);
        }
        return(SendBufferHelper.Close(count));
    }
        public override ArraySegment <byte> Write()
        {
            // Serialize
            ArraySegment <byte> segment = SendBufferHelper.Open(4096);
            ushort usedByteCount        = 0;
            bool   success = true;

            Span <byte> span = new Span <byte>(segment.Array, segment.Offset, segment.Count);

            usedByteCount += sizeof(ushort);
            success       &= BitConverter.TryWriteBytes(span.Slice(usedByteCount, span.Length - usedByteCount), packetID);
            usedByteCount += sizeof(ushort);
            success       &= BitConverter.TryWriteBytes(span.Slice(usedByteCount, span.Length - usedByteCount), playerID);
            usedByteCount += sizeof(long);

            // string length, string
            ushort nameLength = (ushort)Encoding.Unicode.GetBytes(this.name, 0, this.name.Length, segment.Array, segment.Offset + usedByteCount + sizeof(ushort));

            success       &= BitConverter.TryWriteBytes(span.Slice(usedByteCount, span.Length - usedByteCount), nameLength);
            usedByteCount += sizeof(ushort);
            usedByteCount += nameLength;

            // Skill List
            success       &= BitConverter.TryWriteBytes(span.Slice(usedByteCount, span.Length - usedByteCount), (ushort)skills.Count);
            usedByteCount += sizeof(ushort);

            foreach (var skill in skills)
            {
                // TODO
                success &= skill.Write(span, ref usedByteCount);
            }

            // size
            success &= BitConverter.TryWriteBytes(span, usedByteCount);

            if (!success)
            {
                return(null);
            }
            return(SendBufferHelper.Close(usedByteCount));
        }
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> segment = SendBufferHelper.Open(4096);
        ushort count   = 0;
        bool   success = true;

        count += sizeof(ushort);
        Array.Copy(BitConverter.GetBytes((ushort)PacketID.C_Move), 0, segment.Array, segment.Offset + count, sizeof(ushort));
        count += sizeof(ushort);

        Array.Copy(BitConverter.GetBytes(this.posX), 0, segment.Array, segment.Offset + count, sizeof(float));
        count += sizeof(float);
        Array.Copy(BitConverter.GetBytes(this.posY), 0, segment.Array, segment.Offset + count, sizeof(float));
        count += sizeof(float);
        Array.Copy(BitConverter.GetBytes(this.posZ), 0, segment.Array, segment.Offset + count, sizeof(float));
        count += sizeof(float);

        Array.Copy(BitConverter.GetBytes(count), 0, segment.Array, segment.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
        public override ArraySegment <byte> Serialize()
        {
            ArraySegment <byte> arr = SendBufferHelper.Open(4096);

            ushort count   = 0;
            bool   success = true;

            count   += 2;
            success &= BitConverter.TryWriteBytes(new Span <byte>(arr.Array, arr.Offset + count, arr.Count - count), packetId);
            count   += 2;
            success &= BitConverter.TryWriteBytes(new Span <byte>(arr.Array, arr.Offset + count, arr.Count - count), playerId);
            count   += 8;

            success &= BitConverter.TryWriteBytes(new Span <byte>(arr.Array, arr.Offset, arr.Count), count);

            if (!success)
            {
                return(null);
            }

            return(SendBufferHelper.Close(count));
        }
Exemple #24
0
        // 참고: unsafe를 이용하여 C++의 포인터 조작과 유사한 동작을 실행할 수 있다.
        //		 이는 추후 다룰 예정.

        /*
         * static unsafe void ToBytes(byte[] array, int offset, ulong value)
         * {
         *      fixed (byte* ptr = &array[offset])
         *(ulong*)ptr = value;
         * }
         */

        public override void OnConnected(EndPoint endPoint)
        {
            Console.WriteLine($"{endPoint}에 연결되었습니다.");

            PlayerInfoReq packet = new PlayerInfoReq()
            {
                packetId = (ushort)PacketID.PlayerInfoReq,
                playerId = 1001
            };

            for (int i = 0; i < 5; i++)
            {
                ArraySegment <byte> arr = SendBufferHelper.Open(4096);

                // * 수정된 부분
                // TryWriteBytes를 이용하여, GetByte로 변환 후 복사하는 과정을 한 번에 실행한다.
                // success에 비트 연산을 통해 한 번이라도 실패했다면 false로 바뀌게 한다.
                ushort count   = 0;
                bool   success = true;

                // 패킷 크기가 들어갈 부분은 2바이트 비워준다.
                count   += 2;
                success &= BitConverter.TryWriteBytes(new Span <byte>(arr.Array, arr.Offset + count, arr.Count - count), packet.packetId);
                count   += 2;
                success &= BitConverter.TryWriteBytes(new Span <byte>(arr.Array, arr.Offset + count, arr.Count - count), packet.playerId);
                count   += 8;

                // 패킷의 크기는 미리 패킷에 정의해두지 않고, 보낼 때 크기를 센 다음 마지막에 count로 입력해준다.
                success &= BitConverter.TryWriteBytes(new Span <byte>(arr.Array, arr.Offset, arr.Count), count);

                ArraySegment <byte> sendBuff = SendBufferHelper.Close(count);

                if (success)
                {
                    Send(sendBuff);
                }
            }
        }
Exemple #25
0
    public ArraySegment <byte> Write()
    {
        ArraySegment <byte> seg = SendBufferHelper.Open(4096);
        ushort count            = 0;

        count += sizeof(ushort);

        Array.Copy(BitConverter.GetBytes((ushort)PacketId.S_BroadcastEnterGame), 0, seg.Array, seg.Offset + count, sizeof(ushort));
        count += sizeof(ushort);

        Array.Copy(BitConverter.GetBytes(this.playerId), 0, seg.Array, seg.Offset + count, sizeof(int));
        count += sizeof(int);
        Array.Copy(BitConverter.GetBytes(this.posX), 0, seg.Array, seg.Offset + count, sizeof(float));
        count += sizeof(float);
        Array.Copy(BitConverter.GetBytes(this.posY), 0, seg.Array, seg.Offset + count, sizeof(float));
        count += sizeof(float);
        Array.Copy(BitConverter.GetBytes(this.posZ), 0, seg.Array, seg.Offset + count, sizeof(float));
        count += sizeof(float);

        Array.Copy(BitConverter.GetBytes(count), 0, seg.Array, seg.Offset, sizeof(ushort));

        return(SendBufferHelper.Close(count));
    }
    public ArraySegment <byte> Serialize()
    {
        ArraySegment <byte> arr  = SendBufferHelper.Open(4096);
        Span <byte>         span = new Span <byte>(arr.Array, arr.Offset, arr.Count);

        ushort count   = 0;
        bool   success = true;

        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), (ushort)PacketID.C_PlayerInfoReq);
        count   += sizeof(ushort);

        arr.Array[arr.Offset + count] = (byte)this.testByte;
        count   += sizeof(byte);
        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), playerId);
        count   += sizeof(long);
        ushort playerNameLen = (ushort)Encoding.Unicode.GetBytes(playerName, 0, playerName.Length, arr.Array, arr.Offset + count + sizeof(ushort));

        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), playerNameLen);
        count   += sizeof(ushort);
        count   += playerNameLen;
        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), (ushort)skills.Count);
        count   += sizeof(ushort);
        foreach (Skill skill in skills)
        {
            success &= skill.Serialize(span, ref count);
        }

        success &= BitConverter.TryWriteBytes(span, count);

        if (!success)
        {
            return(null);
        }

        return(SendBufferHelper.Close(count));
    }
    public ArraySegment <byte> Serialize()
    {
        ArraySegment <byte> arr  = SendBufferHelper.Open(4096);
        Span <byte>         span = new Span <byte>(arr.Array, arr.Offset, arr.Count);

        ushort count   = 0;
        bool   success = true;

        count   += sizeof(ushort);
        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), (ushort)PacketID.S_Test);
        count   += sizeof(ushort);

        success &= BitConverter.TryWriteBytes(span.Slice(count, span.Length - count), testInt);
        count   += sizeof(int);

        success &= BitConverter.TryWriteBytes(span, count);

        if (!success)
        {
            return(null);
        }

        return(SendBufferHelper.Close(count));
    }