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; } } }
public NetData(float f) { netDataType = NetDataType.nFloat; numBytes = sizeof(float); byte[] toAdd = BitConverter.GetBytes(f); if (!BitConverter.IsLittleEndian) Array.Reverse(toAdd); content = toAdd; }
public NetData(int i) { netDataType = NetDataType.nInt; numBytes = sizeof(Int32); byte[] toAdd = BitConverter.GetBytes(i); if (!BitConverter.IsLittleEndian) Array.Reverse(toAdd); content = toAdd; }
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); }
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]; } }
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); }
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; } }
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); }
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); }
/// <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("网络可传输的数据类型出错!无该类型!"); } }
//成功解析出一个完整的包 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); } }
public NetDataObject(NetDataType netDataType) { _netDataType = netDataType; }
public NetData(char c) { netDataType = NetDataType.nChar; numBytes = sizeof(char); content = BitConverter.GetBytes(c); }
public NetData(bool b) { netDataType = NetDataType.nBool; numBytes = sizeof(bool); content = BitConverter.GetBytes(b); }
//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; } }