您好,欢迎访问一九零五行业门户网

XMPP(2)agsXMPP实现细节浅析

using system; using system.io; using system.text; using system.threading; using system.net; using system.net.sockets; using agsxmpp.protocol; using agsxmpp.protocol.iq; using agsxmpp.protocol.iq.auth; using agsxmpp.protocol.iq.roster; usin
using system;
using system.io;
using system.text;
using system.threading;
using system.net;
using system.net.sockets;
using agsxmpp.protocol;
using agsxmpp.protocol.iq;
using agsxmpp.protocol.iq.auth;
using agsxmpp.protocol.iq.roster;
using agsxmpp.protocol.client;
using agsxmpp.xml;
using agsxmpp.xml.dom;
namespace agsxmpp
{
     ///
     /// zusammenfassung f黵xmppseverconnection.
     ///
     public class xmppseverconnection
     {
         #region >
         public xmppseverconnection()
         {       
              streamparser = new streamparser();
              streamparser.onstreamstart       += new streamhandler(streamparser_onstreamstart);
              streamparser.onstreamend         += new streamhandler(streamparser_onstreamend);
              streamparser.onstreamelement     += new streamhandler(streamparser_onstreamelement);
}
public xmppseverconnection(socket sock) : this()
         {   
              m_sock = sock;
              m_sock.beginreceive(buffer, 0, buffersize, 0, new asynccallback(readcallback), null);       
         }
         #endregion
        private streamparser              streamparser;
         private socket                       m_sock;
        private const int buffersize = 1024;
        private byte[] buffer = new byte[buffersize];
public void readcallback(iasyncresult ar)
         {       
              // retrieve the state object and the handler socket
              // from the asynchronous state object
// read data from the client socket.
              int bytesread = m_sock.endreceive(ar);
if (bytesread > 0)
              {                 
                   streamparser.push(buffer, 0, bytesread);
// not all data received. get more.
                   m_sock.beginreceive(buffer, 0, buffersize, 0, new asynccallback(readcallback), null);
              }
              else
              {
                   m_sock.shutdown(socketshutdown.both);
                   m_sock.close();
              }
         }
private void send(string data)
         {
              // convert the string data to byte data using ascii encoding.
              byte[] bytedata = encoding.utf8.getbytes(data);
// begin sending the data to the remote device.
              m_sock.beginsend(bytedata, 0, bytedata.length, 0, new asynccallback(sendcallback), null);
         }
private void sendcallback(iasyncresult ar)
         {
              try
              {
                   // complete sending the data to the remote device.
                   int bytessent = m_sock.endsend(ar);
                   console.writeline(sent {0} bytes to client., bytessent);
}
              catch (exception e)
              {
                   console.writeline(e.tostring());
              }
         }
public void stop()
         {
              send();
//            client.close();
//            _tcpserver.stop();
m_sock.shutdown(socketshutdown.both);
              m_sock.close();
         }
#region >
//       private int            m_port             = 5222;      
         private string         m_sessionid        = null;
public string sessionid
         {
              get
              {
                   return m_sessionid;
              }
              set
              {
                   m_sessionid = value;
              }
         }
         #endregion
private void streamparser_onstreamstart(object sender, node e)
         {
              sendopenstream();
         }
private void streamparser_onstreamend(object sender, node e)
         {
}
private void streamparser_onstreamelement(object sender, node e)
         {
            console.writeline(onstreamelement: + e.tostring());
              if (e.gettype() == typeof(presence))
              {
                   // route presences here and handle all subscription stuff
              }
              else if (e.gettype() == typeof(message))
              {
                   // route the messages here
}
              else if (e.gettype() == typeof(iq))
              {
                   processiq(e as iq);
              }
         }
private void processiq(iq iq)
         {
              if(iq.query.gettype() == typeof(auth))
              {
                   auth auth = iq.query as auth;
                   switch(iq.type)
                   {
                       case iqtype.get:
                            iq.switchdirection();
                            iq.type = iqtype.result;
                            auth.addchild(new element(password));
                            auth.addchild(new element(digest));
                            send(iq);
                            break;
                       case iqtype.set:
                            // here we should verify the authentication credentials
                            iq.switchdirection();
                            iq.type = iqtype.result;
                            iq.query = null;
                            send(iq);
                            break;
                   }
}
              else if(iq.query.gettype() == typeof(roster))
              {
                   processrosteriq(iq);
}
}
private void processrosteriq(iq iq)
         {
              if (iq.type == iqtype.get)
              {
                   // send the roster
                   // we send a dummy roster here, you should retrieve it from a
                   // database or some kind of directory (ldap, ad etc...)
                   iq.switchdirection();
                   iq.type = iqtype.result;
                   for (int i=1; i
                   {
                       rosteritem ri = new rosteritem();
                       ri.name = item + i.tostring();
                       ri.subscription = subscriptiontype.both;
                       ri.jid = new jid(item + i.tostring() + @localhost);
                       ri.addgroup(localhost);
                       iq.query.addchild(ri);
                   }
                   for (int i=1; i
                   {
                       rosteritem ri = new rosteritem();
                       ri.name = item jo + i.tostring();
                       ri.subscription = subscriptiontype.both;
                       ri.jid = new jid(item + i.tostring() + @jabber.org);
                       ri.addgroup(jo);
                       iq.query.addchild(ri);
                   }
                   send(iq);
              }
         }
private void sendopenstream()
         {
// recv:
// send the opening strem to the client
              string serverdomain = localhost;
this.sessionid = agsxmpp.sessionid.createnewid();
stringbuilder sb = new stringbuilder();
sb.append( );
send( sb.tostring() );
         }
private void send(element el)
         {
              send(el.tostring());
         }
}
}
其它类似信息

推荐信息