IceTcpClientNetSession.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using CommonNetwork_ICE.Util;
  7. using CommonLang.Protocol;
  8. using CommonNetwork_ICE.Common;
  9. using Slice;
  10. using CommonNetwork_ICE.Client;
  11. using CommonNetwork.Net;
  12. using System.Collections;
  13. using CommonLang.IO;
  14. using CommonLang.Log;
  15. using CommonLang.Net;
  16. namespace CommonNetwork_ICE.Session
  17. {
  18. /// <summary>
  19. /// 网络会话对象实现,通过会话对象可以发送消息,存储会话级变量
  20. /// </summary>
  21. public class IceTcpClientNetSession : IceClientNetSession
  22. {
  23. public override bool Open(string url, INetPackageCodec codec, INetSessionListener listener)
  24. {
  25. if(IsConnected)
  26. {
  27. return true;
  28. }
  29. bool ret = false;
  30. try
  31. {
  32. lock (this)
  33. {
  34. this.mListener = listener;
  35. this.mCodec = codec;
  36. string[] url_kv = url.Split(':');
  37. this.mHost = url_kv[0];
  38. this.mPort = int.Parse(url_kv[1]);
  39. int localListenPort = 0;// TCP不侦听端口
  40. this.mRemoteAddress = IPUtil.ToEndPoint(url_kv[0], int.Parse(url_kv[1]));
  41. IceClientMessageCodecImpl iceCodec = new IceClientMessageCodecImpl(codec, this);
  42. iceConnector = new IceTcpClient(this, url_kv[0], this.mPort);
  43. iceConnector.SetMessageCodec(iceCodec);
  44. iceConnector.Open(localListenPort);
  45. ret = true;
  46. onOpen();
  47. }
  48. }
  49. catch (Exception err)
  50. {
  51. onException(new NetException("Open: " + url + " Exception " + err.Message));
  52. }
  53. return ret;
  54. }
  55. }
  56. }