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); } }
// 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(); } } }