void HandleDataFromServer(object sender, DataReceivedEventArgs args) { Connection connection = (Connection)sender; Debug.Log("Received " + args.Bytes.Length + " bytes from server at " + connection.EndPoint.ToString()); //connection.SendBytes(args.Bytes, args.SendOption); ReceiveMessage(args.Bytes); args.Recycle(); }
private static void DataReceivedHandler(object sender, DataReceivedEventArgs args) { Connection connection = (Connection)sender; Console.WriteLine("Received (" + string.Join <byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); connection.SendBytes(args.Bytes, args.SendOption); args.Recycle(); }
private void DataReceived(object sender, DataReceivedEventArgs args) { connection = (Connection)sender; HazelReader reader = new HazelReader(args.Bytes); byte header = reader.ReadByte(); if (DataHandlers.ContainsKey(header)) { UnityMainThreadDispatcher.Instance().Enqueue(() => DataHandlers [header] (connection, reader)); } args.Recycle(); }
private void DataReceivedHandler(object sender, DataReceivedEventArgs args) { Connection connection = (Connection)sender; Console.WriteLine("Received (" + string.Join <byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); //connection.SendBytes(args.Bytes, args.SendOption); //Send data received to all client in List foreach (var conn in clients) { //if (conn != connection) if (true) { conn.SendBytes(args.Bytes, args.SendOption); } } args.Recycle(); }
private static void DataReceived(object sender, DataReceivedEventArgs args) { Console.WriteLine("Received (" + string.Join <byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); args.Recycle(); }
/// <summary> /// Datas the received handler. /// </summary> /// <param name="sender">Sender.</param> /// <param name="args">Arguments.</param> private void DataReceivedHandler(object sender, DataReceivedEventArgs args) { Connection connection = (Connection)sender; Console.WriteLine("Received (" + string.Join <byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); Console.WriteLine("SendType: " + args.Bytes.GetValue(0).ToString()); //Console.WriteLine(((byte)SendType.SENDTOALL).ToString()); //https://stackoverflow.com/questions/943398/get-int-value-from-enum-in-c-sharp //https://msdn.microsoft.com/it-it/library/system.enum.getvalues(v=vs.110).aspx //http://csharp.net-informations.com/statements/enum.htm if (((byte)SendType.SENDTOALL).ToString() == args.Bytes.GetValue(0).ToString()) { //BROADCAST (SENDTOALL) Console.WriteLine("BROADCAST (SENDTOALL)"); //Send data received to all client in List foreach (var conn in clients) { if (true) { conn.Value.SendBytes(args.Bytes, args.SendOption); Console.WriteLine("Send to: " + conn.Value.EndPoint.ToString()); } } } else if ((byte)SendType.SENDTOOTHER == (byte)args.Bytes.GetValue(0)) { //BROADCAST (SENDTOOTHER) Console.WriteLine("BROADCAST (SENDTOOTHER)"); //Send data received to all other client in List foreach (var conn in clients) { if (conn.Value != connection) //SENDTOOTHER { conn.Value.SendBytes(args.Bytes, args.SendOption); Console.WriteLine("Send to: " + conn.Value.EndPoint.ToString()); } } } else if ((byte)SendType.SENDTOSERVER == (byte)args.Bytes.GetValue(0)) { //FOR NOW ECHO SERVER (SENDTOSERVER) Console.WriteLine("CLIENT TO SERVER (SENDTOSERVER)"); //Parser Message //Remove first byte (type) //https://stackoverflow.com/questions/31550484/faster-code-to-remove-first-elements-from-byte-array byte STypeBuffer = args.Bytes[0]; byte[] NewBufferReceiver = new byte[args.Bytes.Length - 1]; Array.Copy(args.Bytes, 1, NewBufferReceiver, 0, NewBufferReceiver.Length); ByteBuffer bb = new ByteBuffer(NewBufferReceiver); //Decoder FlatBuffer String UIDBuffer = String.Empty; if (STypeBuffer == 2) { HazelMessage.HMessage HMessageReceived = HazelMessage.HMessage.GetRootAsHMessage(bb); if ((sbyte)CommandType.LOGIN == HMessageReceived.Command) { //Cerca e restituisci il tutto foreach (var conn in clients) { if (conn.Value == connection) //SENDTOSERVER { UIDBuffer = conn.Key; Console.WriteLine("UID: " + UIDBuffer); } } } } //Encode FlatBuffer //Create flatbuffer class FlatBufferBuilder fbb = new FlatBufferBuilder(1); StringOffset SOUIDBuffer = fbb.CreateString(UIDBuffer); HazelMessage.HMessage.StartHMessage(fbb); HazelMessage.HMessage.AddCommand(fbb, (sbyte)CommandType.LOGIN); HazelMessage.HMessage.AddAnswer(fbb, SOUIDBuffer); var offset = HazelMessage.HMessage.EndHMessage(fbb); HazelMessage.HMessage.FinishHMessageBuffer(fbb, offset); //Reply to Client using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset)) { //Add type! //https://stackoverflow.com/questions/5591329/c-sharp-how-to-add-byte-to-byte-array byte[] newArray = new byte[ms.ToArray().Length + 1]; ms.ToArray().CopyTo(newArray, 1); newArray[0] = (byte)SendType.SENDTOSERVER; connection.SendBytes(newArray, args.SendOption); } Console.WriteLine("Send to: " + connection.EndPoint.ToString()); } args.Recycle(); }
/// <summary> /// Datas the received. /// </summary> /// <param name="sender">Sender.</param> /// <param name="args">Arguments.</param> private static void DataReceived(object sender, DataReceivedEventArgs args) { Console.WriteLine("Received (" + string.Join <byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); //Decode parse received data //Remove first byte (type) //https://stackoverflow.com/questions/31550484/faster-code-to-remove-first-elements-from-byte-array byte STypeBuffer = args.Bytes[0]; //This is NOT TypeBuffer ;-) byte[] NewBufferReceiver = new byte[args.Bytes.Length - 1]; Array.Copy(args.Bytes, 1, NewBufferReceiver, 0, NewBufferReceiver.Length); ByteBuffer bb = new ByteBuffer(NewBufferReceiver); if ((STypeBuffer == (byte)SendType.SENDTOALL) || (STypeBuffer == (byte)SendType.SENDTOOTHER)) { HazelTest.Object ObjectReceived = HazelTest.Object.GetRootAsObject(bb); if (DEBUG) { Console.WriteLine("RECEIVED DATA: "); Console.WriteLine("IDObject RECEIVED: " + ObjectReceived.ID); Console.WriteLine("UID RECEIVED; " + ObjectReceived.Owner); Console.WriteLine("isKinematic: " + ObjectReceived.IsKine); Console.WriteLine("POS RECEIVED: " + ObjectReceived.Pos.X + ", " + ObjectReceived.Pos.Y + ", " + ObjectReceived.Pos.Z); Console.WriteLine("ROT RECEIVED: " + ObjectReceived.Rot.X + ", " + ObjectReceived.Rot.Y + ", " + ObjectReceived.Rot.Z + ", " + ObjectReceived.Rot.W); } //var ReceiveMessageFromGameObjectBuffer = new ReceiveMessageFromGameObject(); //NOT USED! sbyte TypeBuffer = ObjectReceived.Type; if ((byte)PacketId.PLAYER_JOIN == ObjectReceived.Type) { Console.WriteLine("Add new Player!"); //Code for new Player //Spawn something? YES //Using Dispatcher? NO //PlayerSpawn SendMessage(SendType.SENDTOOTHER, PacketId.PLAYER_SPAWN, 0, UID + ";" + AvatarName, true, lastPosition, lastRotation); //TO DO: Using Reliable UDP?? } else if ((byte)PacketId.OBJECT_MOVE == ObjectReceived.Type) { Console.WriteLine("OBJECT MOVE"); } else if ((byte)PacketId.PLAYER_MOVE == ObjectReceived.Type) { Console.WriteLine("PLAYER MOVE"); } else if ((byte)PacketId.PLAYER_SPAWN == ObjectReceived.Type) { Console.WriteLine("PLAYER SPAWN"); } else if ((byte)PacketId.OBJECT_SPAWN == ObjectReceived.Type) { Console.WriteLine("OBJECT SPAWN"); //Rez Object Recieved RezObject(ObjectReceived.Owner.Split(';')[1], ObjectReceived.Owner.Split(';')[0], false); } else if ((byte)PacketId.OBJECT_UNSPAWN == ObjectReceived.Type) { Console.WriteLine("OBJECT UNSPAWN"); //De Rez Object Recieved DeRezObject(ObjectReceived.Owner.Split(';')[1], ObjectReceived.Owner.Split(';')[0], false, ObjectReceived.ID); } } else if (STypeBuffer == (byte)SendType.SENDTOSERVER) { HazelMessage.HMessage HMessageReceived = HazelMessage.HMessage.GetRootAsHMessage(bb); if ((sbyte)CommandType.LOGIN == HMessageReceived.Command) { if (HMessageReceived.Answer != String.Empty) { UID = HMessageReceived.Answer; //Set UID for Your Avatar ME //UnityMainThreadDispatcher.Instance().Enqueue(SetUIDInMainThread(HMessageReceived.Answer)); Console.WriteLine("UID RECEIVED: " + HMessageReceived.Answer); //PLAYER_JOIN MESSAGE (SENDTOOTHER) SendMessage(SendType.SENDTOOTHER, PacketId.PLAYER_JOIN, 0, UID + ";" + AvatarName, true, lastPosition, lastRotation); //TO DO: Using Reliable UDP?? } else { Console.WriteLine("UID RECEIVED is EMPTY (NOT VALID PASSWORD): " + HMessageReceived.Answer); //Disconnect if (connection != null) { Console.WriteLine("DisConnecting from: " + connection.EndPoint.ToString()); connection.Close(); } } } else if ((sbyte)CommandType.DISCONNECTEDCLIENT == HMessageReceived.Command) { //Debug Disconnected UID Console.WriteLine("UID RECEIVED and TO DESTROY: " + HMessageReceived.Answer); } } args.Recycle(); }
/// <summary> /// Datas the received. /// </summary> /// <param name="sender">Sender.</param> /// <param name="args">Arguments.</param> private static void DataReceived(object sender, DataReceivedEventArgs args) { Console.WriteLine("Received (" + string.Join <byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); //Decode parse received data //Remove first byte (type) //https://stackoverflow.com/questions/31550484/faster-code-to-remove-first-elements-from-byte-array byte SendTypeBuffer = args.Bytes[0]; byte[] NewBufferReceiver = new byte[args.Bytes.Length - 1]; Array.Copy(args.Bytes, 1, NewBufferReceiver, 0, NewBufferReceiver.Length); //Check SendType if ((SendTypeBuffer == (byte)SendType.SENDTOALL) || (SendTypeBuffer == (byte)SendType.SENDTOOTHER)) { //Deserialize message using NetStack //Reset bit buffer for further reusing data.Clear(); data.FromArray(NewBufferReceiver, NewBufferReceiver.Length); byte TypeBuffer = data.ReadByte(); string OwnerPlayer = data.ReadString(); bool isKine = data.ReadBool(); uint IDObject = data.ReadUInt(); CompressedVector3 position = new CompressedVector3(data.ReadUInt(), data.ReadUInt(), data.ReadUInt()); CompressedQuaternion rotation = new CompressedQuaternion(data.ReadByte(), data.ReadShort(), data.ReadShort(), data.ReadShort()); //Read Vector3 Compress Velocity //ushort compressedVelocityX = HalfPrecision.Compress(speed); Vector3 VelocityReceived = new Vector3(HalfPrecision.Decompress(data.ReadUShort()), HalfPrecision.Decompress(data.ReadUShort()), HalfPrecision.Decompress(data.ReadUShort())); //Read Float InterpolationTime float InterpolationTime = HalfPrecision.Decompress(data.ReadUShort()); // Check if bit buffer is fully unloaded Console.WriteLine("Bit buffer is empty: " + data.IsFinished); //Decompress Vector and Quaternion //Create a new BoundedRange array for Vector3 position, each entry has bounds and precision BoundedRange[] worldBounds = new BoundedRange[3]; worldBounds[0] = new BoundedRange(-50f, 50f, 0.05f); // X axis worldBounds[1] = new BoundedRange(0f, 25f, 0.05f); // Y axis worldBounds[2] = new BoundedRange(-50f, 50f, 0.05f); // Z axis //Decompress position data Vector3 decompressedPosition = BoundedRange.Decompress(position, worldBounds); // Decompress rotation data Quaternion decompressedRotation = SmallestThree.Decompress(rotation); //Show DATA received if (DEBUG) { Console.WriteLine("ID RECEIVED: " + IDObject.ToString()); Console.WriteLine("TYPE RECEIVED: " + TypeBuffer.ToString()); Console.WriteLine("UID RECEIVED: " + OwnerPlayer); Console.WriteLine("IsKINE RECEIVED: " + isKine.ToString()); Console.WriteLine("POS RECEIVED: " + decompressedPosition.X.ToString() + ", " + decompressedPosition.Y.ToString() + ", " + decompressedPosition.Z.ToString()); Console.WriteLine("ROT RECEIVED: " + decompressedRotation.X.ToString() + ", " + decompressedRotation.Y.ToString() + ", " + decompressedRotation.Z.ToString() + ", " + decompressedRotation.W.ToString()); Console.WriteLine("VEL RECEIVED: " + VelocityReceived.X.ToString() + ", " + VelocityReceived.Y.ToString() + ", " + VelocityReceived.Z.ToString()); Console.WriteLine("INTERPOLATION TIME: " + InterpolationTime.ToString()); } if ((byte)PacketId.PLAYER_JOIN == TypeBuffer) { Console.WriteLine("Add new Player!"); //Code for new Player //Spawn something? YES //Using Dispatcher? NO //PlayerSpawn SendMessage(SendType.SENDTOOTHER, PacketId.PLAYER_SPAWN, 0, UID + ";" + AvatarName, true, lastPosition, lastRotation, VelocityDefaultZero, 0f); //TODO: Using Reliable UDP?? } else if ((byte)PacketId.OBJECT_MOVE == TypeBuffer) { Console.WriteLine("OBJECT MOVE"); } else if ((byte)PacketId.PLAYER_MOVE == TypeBuffer) { Console.WriteLine("PLAYER MOVE"); } else if ((byte)PacketId.PLAYER_SPAWN == TypeBuffer) { Console.WriteLine("PLAYER SPAWN"); } else if ((byte)PacketId.OBJECT_SPAWN == TypeBuffer) { Console.WriteLine("OBJECT SPAWN"); //Rez Object Received //RezObject(OwnerPlayer.Split(';')[1], OwnerPlayer.Split(';')[0], false); } else if ((byte)PacketId.OBJECT_UNSPAWN == TypeBuffer) { Console.WriteLine("OBJECT UNSPAWN"); //De Rez Object Received //DeRezObject(OwnerPlayer.Split(';')[1], OwnerPlayer.Split(';')[0], false, ObjectReceived.ID); } } else if (SendTypeBuffer == (byte)SendType.SENDTOSERVER) { //Deserialize message using NetStack //Reset bit buffer for further reusing data.Clear(); data.FromArray(NewBufferReceiver, NewBufferReceiver.Length); byte CommandTypeBuffer = data.ReadByte(); string Answer = data.ReadString(); // Check if bit buffer is fully unloaded Console.WriteLine("Bit buffer is empty: " + data.IsFinished); if ((byte)CommandType.LOGIN == CommandTypeBuffer) { if (Answer != String.Empty) { UID = Answer; //Set UID for Your Avatar ME //UnityMainThreadDispatcher.Instance().Enqueue(SetUIDInMainThread(HMessageReceived.Answer)); Console.WriteLine("UID RECEIVED: " + Answer); //PLAYER_JOIN MESSAGE (SENDTOOTHER) SendMessage(SendType.SENDTOOTHER, PacketId.PLAYER_JOIN, 0, UID + ";" + AvatarName, true, lastPosition, lastRotation, VelocityDefaultZero, 0f); //TO DO: Using Reliable UDP?? } else { Console.WriteLine("UID RECEIVED is EMPTY (NOT VALID PASSWORD): " + Answer); //Disconnect if (connection != null) { Console.WriteLine("DisConnecting from: " + connection.EndPoint.ToString()); connection.Close(); } } } else if ((byte)CommandType.DISCONNECTEDCLIENT == CommandTypeBuffer) { //Debug Disconnected UID Console.WriteLine("UID RECEIVED and TO DESTROY: " + Answer); } } args.Recycle(); }