public bool TryReceive(ref Frame frame) { if (m_handshake.State != HandshakeStateMachine.HandshakeState.Ready) return false; return m_receiveStateMachine.TryReceive(ref frame); }
protected override bool TryReceiveInternal(ref Frame frame) { if (m_endpoint == null) return false; return m_endpoint.TryReceive(ref frame); }
public bool TrySend(ref Frame frame) { if (m_handshake.State != HandshakeStateMachine.HandshakeState.Ready) return false; return m_sendStateMachine.TrySend(ref frame); }
public static void Send(this Socket socket, string text) { Frame frame = new Frame(); frame.Init(Encoding.UTF8.GetBytes(text)); socket.TrySend(ref frame, Timeout.InfiniteTimeSpan); frame.Close(); }
public static string ReceiveString(this Socket socket) { Frame frame = new Frame(); socket.TryReceive(ref frame, Timeout.InfiniteTimeSpan); var text = Encoding.UTF8.GetString(frame.Data, 0, frame.Size); frame.Close(); return text; }
public bool TrySend(ref Frame frame, TimeSpan timeout) { m_context.Enter(); try { bool isMessageSent = TrySendInternal(ref frame); if (isMessageSent) return true; Stopwatch stopwatch = Stopwatch.StartNew(); bool infinite = timeout == Timeout.InfiniteTimeSpan; while (infinite || stopwatch.Elapsed < timeout) { TimeSpan actualTimeout = timeout - stopwatch.Elapsed; if (actualTimeout.Ticks < 0) actualTimeout = TimeSpan.Zero; bool signalled; if (infinite) { m_context.Wait(); signalled = true; } else signalled = m_context.Wait(actualTimeout); if (signalled) { isMessageSent = TrySendInternal(ref frame); if (isMessageSent) return true; } } return false; } finally { m_context.Exit(); } }
protected abstract bool TryReceiveInternal(ref Frame frame);
protected abstract bool TrySendInternal(ref Frame frame);