/// <summary> /// Sends a datagram with the specified AckResolver info /// The datagram must be sent reliably, but the AckResolver /// is already in the buffer, so there's no need to add it again. /// </summary> /// <param name="resolver">The AckResolver with the datagram info</param> private void SendDatagram(AckResolver resolver) { byte[] msgBytes; // Append the ack index count to the message, to communicate to the // client what reliable datagram count it represents msgBytes = Encoding.ASCII.GetBytes(Reliable.CreateString(resolver.AckIndex, resolver.Message)); _client.Send(msgBytes, msgBytes.Length); }
/// <summary> /// Generically sends a datagram to the specified end points, either reliable or unreliable. /// </summary> /// <param name="message">The message to send</param> /// <param name="isReliable">Whether the handler should ensure /// the message reaches its intended recipient</param> /// <param name="endPoints">The client(s) to send the datagram to</param> public void SendDatagram(string message, bool isReliable) { byte[] msgBytes; if (isReliable) { if (_resolverBuffer.Count >= 100) { return; } // Append the ack index count to the message, to communicate to the // client what reliable datagram count it represents msgBytes = Encoding.ASCII.GetBytes(Reliable.CreateString(_ackCurrentIndex, message)); // Add a new AckResolver to the resolver buffer, which will // resend the datagram if the timeout is reached // before receiving an ACK AddAckResolver( new AckResolver() { AckIndex = _ackCurrentIndex, TicksStart = DateTime.Now.Ticks, Message = message } ); _ackCurrentIndex += 1; } else { msgBytes = Encoding.ASCII.GetBytes(Unreliable.CreateString(message)); } try { _client.Send(msgBytes, msgBytes.Length); } catch (SocketException) { _displayDisconnection.Display(); } }