/// <summary> /// Creates a new session. /// </summary> /// <param name="owner">Owner end point.</param> /// <param name="socket">Session socket.</param> /// <param name="id">Session identifier.</param> public ActiveSession(ActiveEndPoint <T> owner, Socket socket, int id) { Owner = owner; Id = id; Socket = socket; Stream = new NetworkStreamEx(socket, Owner.Host.AuthenticationData); Stream.Authenticate(); // TODO: make authentication optional for StartTLS support if (typeof(T) == typeof(byte[])) { Transceiver = new BinaryTransceiver() as ITransceiver <T>; } else if (typeof(T) == typeof(BinaryPacket)) { Transceiver = new FastBinaryTransceiver() as ITransceiver <T>; } else if (typeof(T) == typeof(X690.Message)) { Transceiver = new X690Transceiver() as ITransceiver <T>; } else if (typeof(T) == typeof(string)) { Transceiver = new StringTransceiver() as ITransceiver <T>; } else { throw new InvalidDataException("Unsupported message type."); } }
/// <summary> /// Receives a X.690 message from the network stream. /// </summary> /// <param name="stream">Readable network stream.</param> /// <returns>Pair of <see cref="ReceiveStatus"/> and received message.</returns> public KeyValuePair <ReceiveStatus, X690.Message> Receive(NetworkStreamEx stream) { if (InputBuffer == null) { InputBuffer = new byte[NetworkStreamEx.ReceiveBufferLength]; } if (MessageCompleted != null) { MessageCompleted.ReadBufferedContinue(stream, InputBuffer); if (!MessageCompleted.IsIncomplete) { try { return(new KeyValuePair <ReceiveStatus, X690.Message>(ReceiveStatus.OverAndOut, MessageCompleted)); } finally { MessageCompleted = null; } } return(new KeyValuePair <ReceiveStatus, X690.Message>(ReceiveStatus.Over, MessageCompleted)); } var message = X690.Message.ReadBuffered(stream, InputBuffer); if (message == null) { return(new KeyValuePair <ReceiveStatus, X690.Message>(ReceiveStatus.Fail, null)); } if (message.IsIncomplete) { MessageCompleted = message; return(new KeyValuePair <ReceiveStatus, X690.Message>(ReceiveStatus.Over, MessageCompleted)); } return(new KeyValuePair <ReceiveStatus, X690.Message>(ReceiveStatus.OverAndOut, message)); }
public KeyValuePair <ReceiveStatus, string> Receive(NetworkStreamEx stream) { var received = B.Receive(stream); if (received.Key == ReceiveStatus.Fail) { return(new KeyValuePair <ReceiveStatus, string>(ReceiveStatus.Fail, null)); } return(new KeyValuePair <ReceiveStatus, string>(received.Key, Encoding.GetString(received.Value))); }
/// <summary> /// Sends a X.690 message to the stream. /// </summary> /// <param name="stream">Writeable stream.</param> /// <param name="message">X.690 message to send.</param> public void Transmit(NetworkStreamEx stream, X690.Message message) { if (message == null) { return; } if (OutputBuffer == null || OutputBuffer.Length < message.Data.Header.MessageLength) { OutputBuffer = new byte[message.Data.Header.MessageLength]; } message.WriteBuffered(stream, OutputBuffer); }
/// <summary> /// Receives a message of binary data from the stream. /// </summary> /// <param name="stream">Readable network stream.</param> /// <returns>Pair of <see cref="ReceiveStatus"/> and received packet.</returns> public KeyValuePair <ReceiveStatus, BinaryPacket> Receive(NetworkStreamEx stream) { if (InputBuffer == null) { InputBuffer = new byte[NetworkStreamEx.ReceiveBufferLength]; } var length = stream.Read(InputBuffer, 0, NetworkStreamEx.ReceiveBufferLength); if (length < 1) { return(new KeyValuePair <ReceiveStatus, BinaryPacket>(ReceiveStatus.Fail, null)); } return(new KeyValuePair <ReceiveStatus, BinaryPacket>(ReceiveStatus.OverAndOut, new BinaryPacket(InputBuffer, length))); }
/// <summary> /// Receives a message of binary data from the stream. /// </summary> /// <param name="stream">Readable network stream.</param> /// <returns>Pair of <see cref="ReceiveStatus"/> and received packet.</returns> public KeyValuePair <ReceiveStatus, byte[]> Receive(NetworkStreamEx stream) { if (InputBuffer == null) { InputBuffer = new byte[NetworkStreamEx.ReceiveBufferLength]; } var length = stream.Read(InputBuffer, 0, NetworkStreamEx.ReceiveBufferLength); if (length < 1) { return(new KeyValuePair <ReceiveStatus, byte[]>(ReceiveStatus.Fail, null)); } var packet = new byte[length]; Buffer.BlockCopy(InputBuffer, 0, packet, 0, length); return(new KeyValuePair <ReceiveStatus, byte[]>(ReceiveStatus.OverAndOut, packet)); }
/// <summary> /// Sends a binary message to the stream. /// </summary> /// <param name="stream">Writeable network stream.</param> /// <param name="packet">Binary packet to send.</param> public void Transmit(NetworkStreamEx stream, byte[] packet) => stream.Write(packet, 0, packet.Length);
public void Transmit(NetworkStreamEx stream, string packet) => B.Transmit(stream, Encoding.GetBytes(packet));
/// <summary> /// Sends a binary message to the stream. /// </summary> /// <param name="stream">Writeable network stream.</param> /// <param name="packet">Binary packet to send.</param> public void Transmit(NetworkStreamEx stream, BinaryPacket packet) => stream.Write(packet, 0, packet.Length);