TcpServerChannelSender.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. namespace Taobao.Top.Link.Channel.TCP
  6. {
  7. /// <summary>the channel that can send message to client via raw tcp
  8. /// </summary>
  9. public class TcpServerChannelSender : IServerChannelSender
  10. {
  11. private IDictionary<object, object> _context;
  12. TcpClient _tcpClient;
  13. public bool IsOpen
  14. {
  15. get { return this._tcpClient.Connected; }
  16. }
  17. public TcpServerChannelSender(TcpClient tcpClient)
  18. {
  19. this._tcpClient = tcpClient;
  20. this._context = new Dictionary<object, object>();
  21. }
  22. public object GetContext(object key)
  23. {
  24. object val;
  25. return this._context.TryGetValue(key, out val) ? val : null;
  26. }
  27. public void SetContext(object key, object value)
  28. {
  29. this._context[key] = value;
  30. }
  31. public void Send(byte[] data)
  32. {
  33. this._tcpClient.GetStream().Write(data, 0, data.Length);
  34. }
  35. public void Close(string reason)
  36. {
  37. this._tcpClient.Close();
  38. }
  39. }
  40. }