NCindy

.net平台上的高性能网络程序开发框架

使用NCindy连接需要TLS的服务器

/**
Gtalk的XMPP协议需要使用TLS来传输,下面的代码演示了如何使用NCindy的SSLFilter来给连接加上SSL处理
**/

  1
using System;
  2using System.Text;
  3using NCindy.Session.AIO;
  4using System.Net;
  5using NCindy.Packet;
  6using NCindy.Filter;
  7using System.Threading;
  8
  9namespace NCindy.Test.SSL
 10{
 11    class GtalkTest
 12    {
 13        static ManualResetEvent startSSLEvent = new ManualResetEvent(false);
 14
 15        static int recvPacketCount = 0;
 16        static void Main(string[] args)
 17        {
 18            try
 19            {
 20                //获取gtalk服务器的IP地址
 21                IPAddress addr = Dns.GetHostAddresses("talk.google.com")[0];
 22                
 23                IPEndPoint gtalkEndpoint = new IPEndPoint(addr, 5222);
 24                //创建一个Session
 25                ISession session = new AsyncTcpSession();
 26
 27                //设置远端地址
 28                session.RemoteEndPoint = gtalkEndpoint;
 29
 30                //设置数据包的编码器和解码器
 31                session.PacketEncoder = new StringEncoder();
 32                session.PacketDecoder = new StringDecoder();
 33
 34                
 35                //设置事件处理函数
 36                session.Started += new EventHandler<NCindy.SessionEventArgs>(Session_Started);
 37                session.ObjectReceived += new EventHandler<NCindy.SessionEventArgs>(Session_ObjectReceived);
 38                session.ObjectSent += new EventHandler<NCindy.SessionEventArgs>(Session_ObjectSent);
 39                session.ExceptionCaught += new EventHandler<NCindy.ExceptionEventArgs>(Session_ExceptionCaught);
 40
 41
 42                //创建并配置SSLFilter
 43                SSLFilter sslFilter = CreateGtalkSSLFilter(session);
 44
 45                //为Session增加SSLFilter
 46                session.AddSessionFilter(sslFilter);
 47
 48                session.Start();
 49
 50                //等待接收到3个数据包之后,启动SSLFilter
 51                startSSLEvent.WaitOne();
 52
 53                Console.WriteLine("Start SSLFilter.");
 54
 55                sslFilter.Start();
 56
 57                //SSLFilter认证通过,再次发送数据。
 58                DoSendData(session);
 59
 60
 61                Console.ReadLine();
 62
 63            }

 64            catch (Exception e)
 65            {
 66                Console.WriteLine(e);
 67            }

 68        }

 69
 70        private static SSLFilter CreateGtalkSSLFilter(ISession session)
 71        {
 72            //设置Filter所属Session和Filter的加密方式:TLS
 73            SSLFilter sslFilter = new SSLFilter(session, System.Security.Authentication.SslProtocols.Tls);
 74
 75            sslFilter.TargetHost = "gmail.com";
 76
 77            //作为客户端进行SSL认证
 78            sslFilter.IsClientMode = true;
 79            sslFilter.IsNeedClientAuth = true;
 80            return sslFilter;
 81        }

 82
 83        static void Session_ExceptionCaught(object sender, NCindy.ExceptionEventArgs e)
 84        {
 85            Console.WriteLine(e.Cause);
 86        }

 87
 88        static void Session_ObjectSent(object sender, NCindy.SessionEventArgs e)
 89        {
 90            Console.WriteLine("Object sent: " + e.Obj);
 91        }

 92
 93        static void Session_ObjectReceived(object sender, NCindy.SessionEventArgs e)
 94        {
 95            if (Interlocked.Increment(ref recvPacketCount) > 2 && startSSLEvent != null)
 96            {
 97                startSSLEvent.Set();
 98                startSSLEvent = null;
 99            }

100
101
102            Console.WriteLine("Object recv: " + e.Obj);
103        }

104
105        static void Session_Started(object sender, NCindy.SessionEventArgs e)
106        {
107            Console.WriteLine("Session started.");
108            //当Session启动,开始发送
109            DoSendData(e.Session);
110        }

111
112        private static void DoSendData(NCindy.ISession session)
113        {
114            Console.WriteLine("Send some data.");
115            string s = "<stream:stream to=\"gmail.com\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">";
116            string s1 = "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
117
118            session.Send(s);
119            session.Send(s1);
120        }

121    }

122
123
124    public class StringEncoder : NCindy.IPacketEncoder
125    {
126
127        IPacketEncoder Members
138    }

139
140    public class StringDecoder : NCindy.IPacketDecoder
141    {
142        IPacketDecoder Members
154    }

155}

156

posted on 2006-10-27 11:49  iceboundrock  阅读(1632)  评论(0编辑  收藏  举报

导航