Exemplo n.º 1
0
        bool OnDataReceived(NFClientNet client, byte[] bytes, UInt32 bytesCount)
        {
            if (bytes.Length == bytesCount)
            {
                object structType = new MsgHead();
                StructureTransform.ByteArrayToStructureEndian(bytes, ref structType, 0);
                MsgHead head = (MsgHead)structType;

                Int32 nBodyLen = (Int32)bytesCount - (Int32)ConstDefine.NF_PACKET_HEAD_SIZE;
                if (nBodyLen > 0)
                {
                    byte[] body = new byte[nBodyLen];
                    Array.Copy(bytes, ConstDefine.NF_PACKET_HEAD_SIZE, body, 0, nBodyLen);

                    client.net.mxBinMsgEvent.OnMessageEvent(head, body);
                    return(true);
                }
                else
                {
                    //space packet
                }
            }

            return(false);
        }
Exemplo n.º 2
0
    public void SendMsg(NFrame.NFGUID xID, NFMsg.EGameMsgID unMsgID, MemoryStream stream)
    {
        if (NFStart.Instance.bDebugMode)
        {
            return;
        }

        NFMsg.MsgBase xData = new NFMsg.MsgBase();
        xData.player_id = NFToPB(xID);
        xData.msg_data  = stream.ToArray();

        MemoryStream body = new MemoryStream();

        Serializer.Serialize <NFMsg.MsgBase>(body, xData);

        MsgHead head = new MsgHead();

        head.unMsgID   = (UInt16)unMsgID;
        head.unDataLen = (UInt32)body.Length + (UInt32)ConstDefine.NF_PACKET_HEAD_SIZE;

        byte[] bodyByte = body.ToArray();
        byte[] headByte = StructureTransform.StructureToByteArrayEndian(head);


        byte[] sendBytes = new byte[head.unDataLen];
        headByte.CopyTo(sendBytes, 0);
        bodyByte.CopyTo(sendBytes, headByte.Length);

        xNet.mxClient.SendBytes(sendBytes);

        string strTime = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
        string strData = "S***:" + strTime + " MsgID:" + head.unMsgID + " Len:" + head.unDataLen;

        xNet.mxListener.aMsgList.Add(strData);
    }
Exemplo n.º 3
0
    /// <summary>
    /// 接收到消息
    /// </summary>
    void OnReceive(byte[] bytes, int length)
    {
        memStream.Seek(0, SeekOrigin.End);
        memStream.Write(bytes, 0, length);
        //Reset to beginning
        memStream.Seek(0, SeekOrigin.Begin);

        while (RemainingBytes() >= ConstDefine.NF_PACKET_HEAD_SIZE)
        {
            object structType  = new MsgHead();
            byte[] headBytes   = new byte[Marshal.SizeOf(structType)];
            Int32  msgheeadlen = Marshal.SizeOf(structType);
            Array.Copy(reader.ReadBytes(msgheeadlen), 0, headBytes, 0, msgheeadlen);
            StructureTransform.ByteArrayToStructureEndian(headBytes, ref structType, 0);
            MsgHead head    = (MsgHead)structType;
            int     datalen = (int)head.unDataLen - ConstDefine.NF_PACKET_HEAD_SIZE;

            if (RemainingBytes() >= datalen)
            {
                MemoryStream ms     = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(ms);
                writer.Write(datalen);
                writer.Write(reader.ReadBytes(datalen));
                ms.Seek(0, SeekOrigin.Begin);
                OnReceivedMessage(head.unMsgID, ms);
            }
        }

        //Create a new stream with any leftover bytes
        byte[] leftover = reader.ReadBytes((int)RemainingBytes());
        memStream.SetLength(0);     //Clear
        memStream.Write(leftover, 0, leftover.Length);
    }
Exemplo n.º 4
0
    private Transform GetTowerTransform(TeamType team, eBattlePosition posIndex)
    {
        StructureTransform SpawnPos = (team == TeamType.Ally) ? AllyDefenseTowerPosition : EnemyDefenseTowerPosition;

        switch (posIndex)
        {
        case eBattlePosition.Left: return(SpawnPos.tr_Left);

        case eBattlePosition.Right: return(SpawnPos.tr_Right);

        case eBattlePosition.Center: return(SpawnPos.tr_Center);

        default: return(null);
        }
    }
Exemplo n.º 5
0
    public override void SendMsg(MsgHead head, byte[] bodyByte)
    {
        head.unDataLen = (UInt32)bodyByte.Length + (UInt32)ConstDefine.AF_PACKET_HEAD_SIZE;

        byte[] headByte = StructureTransform.StructureToByteArrayEndian(head);


        byte[] sendBytes = new byte[head.unDataLen];
        headByte.CopyTo(sendBytes, 0);
        bodyByte.CopyTo(sendBytes, headByte.Length);

        mxClient.SendBytes(sendBytes);

        string strTime = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
        string strData = "S***:" + strTime + " MsgID:" + head.unMsgID + " Len:" + head.unDataLen;

        mReciveaMsgList.Add(strData);
    }
Exemplo n.º 6
0
        void OnDataReceived()
        {
            if (mnPacketSize >= ConstDefine.NF_PACKET_HEAD_SIZE)
            {
                object structType = new MsgHead();
                byte[] headBytes  = new byte[Marshal.SizeOf(structType)];

                Array.Copy(mPacket, 0, headBytes, 0, Marshal.SizeOf(structType));
                StructureTransform.ByteArrayToStructureEndian(headBytes, ref structType, 0);
                MsgHead head = (MsgHead)structType;

                if (head.unDataLen == mnPacketSize)
                {
                    byte[] body_head = new byte[head.unDataLen];
                    Array.Copy(mPacket, 0, body_head, 0, head.unDataLen);
                    mnPacketSize = 0;

                    if (false == OnDataReceived(client, body_head, head.unDataLen))
                    {
                        OnClientDisconnect(new NFTCPEventParams());
                    }
                }
                else if (mnPacketSize > head.unDataLen)
                {
                    UInt32 nNewLen   = mnPacketSize - head.unDataLen;
                    byte[] newpacket = new byte[ConstDefine.MAX_PACKET_LEN];
                    Array.Copy(mPacket, head.unDataLen, newpacket, 0, nNewLen);

                    byte[] body_head = new byte[head.unDataLen];
                    Array.Copy(mPacket, 0, body_head, 0, head.unDataLen);
                    mnPacketSize = nNewLen;
                    mPacket      = newpacket;

                    if (false == OnDataReceived(client, body_head, head.unDataLen))
                    {
                        OnClientDisconnect(new NFTCPEventParams());
                    }

                    OnDataReceived();
                }
            }
        }
Exemplo n.º 7
0
    public static byte[] StructureToByteArrayEndian(object obj)
    {
        object thisBoxed = obj;   //copy £¬œ« struct ×°Ïä
        Type   test      = thisBoxed.GetType();

        int offset = 0;

        byte[] data = new byte[Marshal.SizeOf(thisBoxed)];

        object   fieldValue;
        TypeCode typeCode;

        byte[] temp;
        // ÁПٜṹÌåµÄÿžö³ÉÔ±£¬²¢Reverse
        foreach (var field in test.GetFields())
        {
            fieldValue = field.GetValue(thisBoxed);            // Get value

            typeCode = Type.GetTypeCode(fieldValue.GetType()); // get type

            switch (typeCode)
            {
            case TypeCode.Single:     // float
            {
                temp = BitConverter.GetBytes((Single)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(Single));
                break;
            }

            case TypeCode.Int32:
            {
                temp = BitConverter.GetBytes((Int32)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(Int32));
                break;
            }

            case TypeCode.UInt32:
            {
                temp = BitConverter.GetBytes((UInt32)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(UInt32));
                break;
            }

            case TypeCode.Int16:
            {
                temp = BitConverter.GetBytes((Int16)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(Int16));
                break;
            }

            case TypeCode.UInt16:
            {
                temp = BitConverter.GetBytes((UInt16)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(UInt16));
                break;
            }

            case TypeCode.Int64:
            {
                temp = BitConverter.GetBytes((Int64)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(Int64));
                break;
            }

            case TypeCode.UInt64:
            {
                temp = BitConverter.GetBytes((UInt64)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(UInt64));
                break;
            }

            case TypeCode.Double:
            {
                temp = BitConverter.GetBytes((Double)fieldValue);
                StructureTransform.Reverse(temp);
                Array.Copy(temp, 0, data, offset, sizeof(Double));
                break;
            }

            case TypeCode.Byte:
            {
                data[offset] = (Byte)fieldValue;
                break;
            }

            default:
            {
                //System.Diagnostics.Debug.Fail("No conversion provided for this type : " + typeCode.ToString());
                break;
            }
            }
            ;  // switch
            if (typeCode == TypeCode.Object)
            {
                int length = ((byte[])fieldValue).Length;
                Array.Copy(((byte[])fieldValue), 0, data, offset, length);
                offset += length;
            }
            else
            {
                offset += Marshal.SizeOf(fieldValue);
            }
        } // foreach

        return(data);
    } // Swap