void SendAckPackets() { // LogFile.WriteLine("Checking Last ack " + ((int)DateTime.Now.Subtract( lastackpacketsent ).TotalMilliseconds).ToString() ); if ((int)DateTime.Now.Subtract(lastackpacketsent).TotalMilliseconds > AckPacketIntervalSeconds * 1000) { lastackpacketsent = DateTime.Now; byte[] ackpacketdata = null; lock ( receivedpacketsnotacked ) { if (receivedpacketsnotacked.Count == 0) { return; } //LogFile.WriteLine("Creating ack packet..." ); int numpacketstoack = receivedpacketsnotacked.Count; ackpacketdata = new byte[numpacketstoack * 2]; int nextposition = 0; for (int i = 0; i < numpacketstoack; i++) { short packettoack = (short)receivedpacketsnotacked.Dequeue(); binarypacker.WriteValueToBuffer(ackpacketdata, ref nextposition, packettoack); // LogFile.WriteLine(" ... acking " + packettoack.ToString() ); } } //LogFile.WriteLine("Sending ack packet " + Encoding.ASCII.GetString( ackpacketdata, 0, ackpacketdata.Length ) ); parent.SendNonAckable('A', ackpacketdata); } }
//public void _NetworkChangeState( object source, NetworkChangeStateArgs e ) //{ // isserver = e.IsServer; //} // rpc format: // (classname)(methodname)(arg1)(arg2)(arg3)... // SendRpc( connection, "OSMP.Testing.ITestInterface", "SayHello", new object[]{ } ) public void SendRpc(IPEndPoint connection, string typename, string methodname, object[] args) { // Test.WriteOut( args ); LogFile.WriteLine("SendRpc " + typename + " " + methodname); //for( int i = 0; i < args.GetUpperBound(0) + 1; i++ ) //{ // LogFile.WriteLine(" arg: " + args[i].ToString() ); //} byte[] packet = new byte[1400]; // note to self: make this a little more dynamic... int nextposition = 0; //binarypacker.WriteValueToBuffer(packet, ref nextposition, RpcType); binarypacker.WriteValueToBuffer(packet, ref nextposition, typename); binarypacker.WriteValueToBuffer(packet, ref nextposition, methodname); foreach (object parameter in args) { binarypacker.WriteValueToBuffer(packet, ref nextposition, parameter); } //LogFile.WriteLine("Sending " + Encoding.UTF8.GetString(packet, 0, nextposition)); //LogFile.WriteLine( nextposition + " bytes " + Encoding.ASCII.GetString( packet, 0, nextposition ) ); network.Send(connection, RpcType, packet, 0, nextposition); }
// server sends C packet to client, which contains the shared key, and also the temporary client key the client sent // server knows key now, client will receive it // Packet format: [int32 xxx][short xxx][char 'C'][temporary client key][shared key] // server replies to any R packet, generating the shared key if the connection didnt exist before public void RPacketHandler(object source, PacketHandlerArgs e) { if (isserver) { byte[] packet = e.Data; int tempnextposition = e.NextPosition; int tempclientkey = (int)binarypacker.ReadValueFromBuffer(packet, ref tempnextposition, typeof(int)); LogFile.WriteLine("R packet received, sending C packet, tempclientkey: " + tempclientkey.ToString() + " sharedkey: " + SharedSecretKey.ToString()); byte[] newpacket = new byte[8]; tempnextposition = 0; binarypacker.WriteValueToBuffer(newpacket, ref tempnextposition, tempclientkey); binarypacker.WriteValueToBuffer(newpacket, ref tempnextposition, SharedSecretKey); parent.SendNonAckable('C', newpacket); if (!Validated) { Validated = true; LogFile.WriteLine("Shared key sent; marking connection open"); parent.OnConnectionValidated(); } } }
byte[] GenerateOutgoingPacket(char packettype, byte[] data, int offset, int length) { byte[] packet = new byte[length + 4 + 2 + 1]; int nextposition = offset; short packetreference = packetreferencecontroller.NextReference; binarypacker.WriteValueToBuffer(packet, ref nextposition, sharedsecretexchange.SharedSecretKey); binarypacker.WriteValueToBuffer(packet, ref nextposition, packetreference); binarypacker.WriteValueToBuffer(packet, ref nextposition, packettype); Buffer.BlockCopy(data, offset, packet, nextposition, length); packetreferencecontroller.RegisterSentPacket(packetreference, packet); return(packet); }
public void TestGeneralBinaryPackerFunctionality() { byte[] bytearray = new byte[1024]; int position = 0; TestClass testclass = new TestClass(); testclass.booleanvaluetrue = true; testclass.charvalue = 'C'; testclass.doublevalue = 123.4567890; testclass.intvalue = 1234567890; testclass.Country = "Spain"; testclass.name = "Test class name"; testclass.indexes = new int[] { 5, 1, 4, 2, 3 }; testclass.childclass = new ChildClass(); testclass.childclass.name = "name inside child class"; BinaryPacker bp = new BinaryPacker(); //Write several objects into the buffer bp.WriteValueToBuffer(bytearray, ref position, testclass); bp.WriteValueToBuffer(bytearray, ref position, "The quick brown fox."); bp.WriteValueToBuffer(bytearray, ref position, "Rain in Spain."); bp.PackObjectUsingSpecifiedAttributes(bytearray, ref position, testclass, new Type[] { typeof(AttributePack) }); byte[] bytearraytowrite = Encoding.UTF8.GetBytes("Hello world"); bp.WriteValueToBuffer(bytearray, ref position, bytearraytowrite); FractalSplineBox box = new FractalSplineBox(); box.name = "Test box"; box.pos = new Vector3(1, 123.123, 150.150); bp.PackObjectUsingSpecifiedAttributes(bytearray, ref position, box, new Type[] { typeof(Replicate) }); position = 0; //verify all the items we put in TestClass outputobject = (TestClass) new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(TestClass)); string error = "Trouble unpacking TestClass from BinaryPacker"; Assert.AreEqual(true, outputobject.booleanvaluetrue, error); Assert.AreEqual('C', outputobject.charvalue, error); Assert.AreEqual(123.4567890, outputobject.doublevalue, error); Assert.AreEqual(1234567890, outputobject.intvalue, error); Assert.AreEqual("Spain", outputobject.Country, error); Assert.AreEqual("Test class name", outputobject.name, error); Assert.AreEqual(5, outputobject.indexes.Length, error); Assert.AreEqual(5, outputobject.indexes[0], error); Assert.AreEqual(1, outputobject.indexes[1], error); Assert.AreEqual(4, outputobject.indexes[2], error); Assert.AreEqual(2, outputobject.indexes[3], error); Assert.AreEqual(3, outputobject.indexes[4], error); Assert.AreEqual("name inside child class", outputobject.childclass.name, error); string sout = (string)new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(string)); Assert.AreEqual("The quick brown fox.", sout, "Could not unpack string"); sout = (string)new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(string)); Assert.AreEqual("Rain in Spain.", sout, "Could not unpack second string"); outputobject = new TestClass(); bp.UnpackIntoObjectUsingSpecifiedAttributes(bytearray, ref position, outputobject, new Type[] { typeof(AttributePack) }); Assert.AreEqual(null, outputobject.name, "Error in value of unset value in attribute packing"); // should be blank, because name member has a AttributePack attribute Assert.AreEqual("Spain", outputobject.Country, "Error in value of set value in attribute packing"); //Country does have an AttributePack attribute bytearraytowrite = (byte[])new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(byte[])); Assert.AreEqual("Hello world", Encoding.UTF8.GetString(bytearraytowrite), "Error in unpacking UTF8"); FractalSplineBox boxobject = new FractalSplineBox(); new BinaryPacker().UnpackIntoObjectUsingSpecifiedAttributes(bytearray, ref position, boxobject, new Type[] { typeof(Replicate) }); Assert.AreEqual("Test box", boxobject.name, "Error in unpacking FractelSplineBox name"); Assert.AreEqual(new Vector3(1, 123.123, 150.150), box.pos, "Error in unpacking FractelSplineBox position"); }