ConnectionState.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // both server and client need a connection state object.
  2. // -> server needs it to keep track of multiple connections
  3. // -> client needs it to safely create a new connection state on every new
  4. // connect in order to avoid data races where a dieing thread might still
  5. // modify the current state. can't happen if we create a new state each time!
  6. // (fixes all the flaky tests)
  7. //
  8. // ... besides, it also allows us to share code!
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. namespace Telepathy
  12. {
  13. public class ConnectionState
  14. {
  15. public TcpClient client;
  16. // thread safe pipe to send messages from main thread to send thread
  17. public readonly MagnificentSendPipe sendPipe;
  18. // ManualResetEvent to wake up the send thread. better than Thread.Sleep
  19. // -> call Set() if everything was sent
  20. // -> call Reset() if there is something to send again
  21. // -> call WaitOne() to block until Reset was called
  22. public ManualResetEvent sendPending = new ManualResetEvent(false);
  23. public ConnectionState(TcpClient client, int MaxMessageSize)
  24. {
  25. this.client = client;
  26. // create send pipe with max message size for pooling
  27. sendPipe = new MagnificentSendPipe(MaxMessageSize);
  28. }
  29. }
  30. }