예제 #1
0
    void Update()
    {
        NetIncomingMessage message;

        while ((message = peer.ReadMessage()) != null)
        {
            switch (message.MessageType)
            {
            case NetIncomingMessageType.Data:
                Debug.Log(message.MessageType);
                NetDataType type = (NetDataType)message.ReadByte();
                OnDataType(type, message);
                break;

            case NetIncomingMessageType.StatusChanged:
                NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
                Debug.Log("status " + status);
                break;

            case NetIncomingMessageType.DebugMessage:
                break;

            default:
                print("unhandled message with type: " + message.MessageType);
                break;
            }
        }
    }
예제 #2
0
 public NetData(float f)
 {
     netDataType = NetDataType.nFloat;
     numBytes = sizeof(float);
     byte[] toAdd = BitConverter.GetBytes(f);
     if (!BitConverter.IsLittleEndian)
         Array.Reverse(toAdd);
     content = toAdd;
 }
예제 #3
0
 public NetData(int i)
 {
     netDataType = NetDataType.nInt;
     numBytes = sizeof(Int32);
     byte[] toAdd = BitConverter.GetBytes(i);
     if (!BitConverter.IsLittleEndian)
         Array.Reverse(toAdd);
     content = toAdd;
 }
예제 #4
0
 static public void send(NetDataType type)
 {
     byte[] send_buffer = new byte[8];
     byte[] ib;
     ib = BitConverter.GetBytes(0);
     Buffer.BlockCopy(ib, 0, send_buffer, 0, 4);
     ib = BitConverter.GetBytes((int)type);
     Buffer.BlockCopy(ib, 0, send_buffer, 4, 4);
     send(send_buffer);
 }
예제 #5
0
 public NetData(byte[] data)
 {
     netDataType = (NetDataType)data[0];
     numBytes = (int)data[1];
     content = new byte[numBytes];
     for (int i = 2; i < data.Length; i++)
     {
         content[i - 2] = data[i]; 
     }
 }
예제 #6
0
    public NetData(string s)
    {
        if (s.Length > 252) return;

        netDataType = NetDataType.nString;
        numBytes = s.Length;
        content = new byte[s.Length];

        content = Encoding.ASCII.GetBytes(s);
    }
예제 #7
0
 void OnDataType(NetDataType type, NetIncomingMessage message)
 {
     switch (type)
     {
     case NetDataType.SPAWN_PLAYER:
         Vector3    position = message.ReadVector3();
         Quaternion rotation = message.ReadQuaternion();
         MyNetwork.SpawnPlayer(position, rotation);
         break;
     }
 }
예제 #8
0
 static public void send(NetDataType type, float[] data)
 {
     byte[] send_buffer = new byte[data.Length * 4];
     for (int i = 0; i < data.Length; i++)
     {
         float  f = data[i];
         byte[] ib;
         ib = BitConverter.GetBytes(f);
         Buffer.BlockCopy(ib, 0, send_buffer, 4 * i, 4);
     }
     send(type, send_buffer);
 }
예제 #9
0
 static public void send(NetDataType type, string data)
 {
     byte[] buffer      = Encoding.UTF8.GetBytes(data);
     byte[] send_buffer = new byte[buffer.Length + 8];
     byte[] ib;
     ib = BitConverter.GetBytes(buffer.Length);
     Buffer.BlockCopy(ib, 0, send_buffer, 0, 4);
     ib = BitConverter.GetBytes((int)type);
     Buffer.BlockCopy(ib, 0, send_buffer, 4, 4);
     Buffer.BlockCopy(buffer, 0, send_buffer, 8, buffer.Length);
     send(send_buffer);
 }
예제 #10
0
        /// <summary>
        /// 获取NetData所占用的字节数
        /// </summary>
        /// <param name="ndt">Key</param>
        /// <returns>value</returns>
        public static int Query(NetDataType ndt)
        {
            int result;

            if (dic.TryGetValue(ndt, out result))
            {
                return(result);
            }
            else
            {
                throw new Exception("网络可传输的数据类型出错!无该类型!");
            }
        }
예제 #11
0
 //成功解析出一个完整的包
 void get(NetDataType type, byte[] buffer)
 {
     //Debug.Log("get:" + type.ToString() + " buffer_length:" + buffer.Length);
     //Debug.Log(System.Text.Encoding.UTF8.GetString(buffer));
     if (type == NetDataType.get)
     {
         ReceiveData(type, System.Text.Encoding.UTF8.GetString(buffer));
     }
     else
     {
         string log = "";
         for (int i = 0; i < buffer.Length; i++)
         {
             log += ((int)buffer[i]).ToString() + " ";
         }
         Debug.Log("Error,Net can't parse type:" + type.ToString() + " data:" + log);
     }
 }
예제 #12
0
 public NetDataObject(NetDataType netDataType)
 {
     _netDataType = netDataType;
 }
예제 #13
0
 public NetData(char c)
 {
     netDataType = NetDataType.nChar;
     numBytes = sizeof(char);
     content = BitConverter.GetBytes(c);
 }
예제 #14
0
 public NetData(bool b)
 {
     netDataType = NetDataType.nBool;
     numBytes = sizeof(bool);
     content = BitConverter.GetBytes(b);
 }
예제 #15
0
        //Incoming messages
        //=================
        private void handleDataMessage(NetIncomingMessage inMsg, NetDataType msgDataType)
        {
            switch (msgDataType)
            {
                case (NetDataType.FrameUpdate):
                    readFrameUpdateData(inMsg);
                    break;

                case (NetDataType.PlayerConnect):
                    readPlayerData(inMsg);
                    break;

                case (NetDataType.PlayerDisconnect):
                    long playerUID = inMsg.ReadInt64();
                    PlayerData dcPlayer = clientWorld.getPlayerByUID(playerUID);
                    handlePlayerDisconnect(dcPlayer);
                    break;

                case (NetDataType.StartGame):
                    startGame();
                    break;

                case (NetDataType.PlayerIndexUpdate):
                    readPlayerIndexUpdate(inMsg);
                    break;

                default:
                    dwLog.info("Unknown data packet of size " + inMsg.LengthBytes + " bytes");
                    if (onNetDataReceived != null)
                        onNetDataReceived(inMsg);
                    break;
            }
        }