/// <summary> /// Bytes were read from the socket. /// </summary> /// <param name="sock">The socket that was read from.</param> /// <param name="buf">The bytes that were read.</param> /// <returns>true if RequestRead() should be called automatically again</returns> /// <param name="offset">Offset into the buffer to start at</param> /// <param name="length">Number of bytes to use out of the buffer</param> public virtual bool OnRead(BaseSocket sock, byte[] buf, int offset, int length) { return true; }
/// <summary> /// /// </summary> /// <param name="newSock"></param> public virtual void OnInit(BaseSocket newSock) { m_listener.OnInit(newSock); }
/// <summary> /// /// </summary> /// <param name="sock"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <returns></returns> public virtual bool OnRead(BaseSocket sock, byte[] buf, int offset, int length) { return m_listener.OnRead(sock, buf, offset, length); }
/// <summary> /// /// </summary> /// <param name="newsocket"></param> /// <returns></returns> public virtual bool OnAccept(BaseSocket newsocket) { return m_listener.OnAccept(newsocket); }
/// <summary> /// /// </summary> /// <param name="sock"></param> public virtual void OnConnect(BaseSocket sock) { if (m_ssl) { #if !NO_SSL m_sock.StartTLS(); #else throw new NotImplementedException("SSL not compiled in"); #endif } m_listener.OnConnect(sock); }
bool ISocketEventListener.OnInvalidCertificate(BaseSocket sock, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // TODO: pass up the chain return m_listener.OnInvalidCertificate(null, certificate, chain, sslPolicyErrors); }
void ISocketEventListener.OnWrite(BaseSocket sock, byte[] buf, int offset, int length) { m_listener.OnWrite(null, buf, offset, length); }
void ISocketEventListener.OnConnect(BaseSocket sock) { #if !NO_SSL if ((bool)m_listener[Options.SSL]) { XEP25Socket s = sock as XEP25Socket; m_listener[Options.REMOTE_CERTIFICATE] = s.RemoteCertificate; } #endif m_listener.Connected(); }
void ISocketEventListener.OnError(BaseSocket sock, Exception ex) { m_listener[Options.REMOTE_CERTIFICATE] = null; m_elements = null; m_listener.Errored(ex); }
bool ISocketEventListener.OnAccept(BaseSocket newsocket) { m_sock = newsocket; InitializeStream(); m_listener.Accepted(); // Don't accept any more connections until this one closes // yes, it will look like we're still listening until the old sock is free'd by GC. // don't want OnClose() to fire, though, so we can't close the previous sock. return false; }
void ISocketEventListener.OnClose(BaseSocket sock) { m_listener[Options.REMOTE_CERTIFICATE] = null; m_elements = null; m_listener.Closed(); }
ISocketEventListener ISocketEventListener.GetListener(BaseSocket newSock) { return this; }
/// <summary> /// Connects the outbound socket. /// </summary> public override void Connect() { int port = (int)m_listener[Options.PORT]; Debug.Assert(port > 0); m_sock = CreateSocket(); string to = (string)m_listener[Options.TO]; Debug.Assert(to != null); string host = (string)m_listener[Options.NETWORK_HOST]; if (string.IsNullOrEmpty(host)) host = to; string url = (string)m_listener[Options.POLL_URL]; if (string.IsNullOrEmpty(url)) { url = Address.LookupTXT("_xmppconnect.", to, "_xmpp-client-xbosh"); if (url == null) throw new ArgumentNullException("URL not found in DNS, and not specified", "URL"); } ((IHttpSocket)m_sock).URL = url; //Address addr = new Address(host, port); m_sock.Connect(null, (string)m_listener[Options.SERVER_ID]); }
/// <summary> /// Bytes were written to the socket. /// </summary> /// <param name="sock">The socket that was written to.</param> /// <param name="buf">The bytes that were written.</param> /// <param name="offset">Offset into the buffer to start at</param> /// <param name="length">Number of bytes to use out of the buffer</param> public virtual void OnWrite(BaseSocket sock, byte[] buf, int offset, int length) { }
void ISocketEventListener.OnError(BaseSocket sock, Exception ex) { if (Interlocked.Increment(ref m_errorCount) > m_maxErrors) { m_keepRunning = false; m_listener.OnError(null, ex); } lock (m_lock) { Monitor.Pulse(m_lock); } }
void ISocketEventListener.OnInit(BaseSocket newSock) { }
void ISocketEventListener.OnInit(BaseSocket newSock) { // This is the one listener event with a good socket, but it might not be the one that anyone expects. // The important thing is that if someone wants to set the TCP buffer sizes downstream, it // will work. m_listener.OnInit(newSock); }
bool ISocketEventListener.OnRead(BaseSocket sock, byte[] buf, int offset, int length) { Debug.Assert(m_listener != null); Debug.Assert(m_elements != null); m_listener.BytesRead(buf, offset, length); m_elements.Push(buf, offset, length); return true; }
bool ISocketEventListener.OnRead(BaseSocket sock, byte[] buf, int offset, int length) { Debug.WriteLine("IN HTTP(" + m_name + "): " + ENC.GetString(buf, offset, length)); int i = offset; string header = null; int last = offset + length; while (i < last) { // HTTP/1.1 200 OK // Header: value // Header: value // // Content switch (m_state) { case ParseState.START: if (!ParseAt(buf, ref i, last, HTTP11_SP, 0)) goto ERROR; m_state = ParseState.RESPONSE; break; case ParseState.RESPONSE: string code = ParseTo(buf, ref i, last, SPACE); if (code == null) goto ERROR; if (code != "200") { Debug.WriteLine("Non-OK response from server (" + code + "). STOP!"); goto ERROR; } try { // I know this can never fail. it's here for when we // implement redirects and the like. m_current.Code = int.Parse(code); } catch (Exception) { Debug.WriteLine("invalid response code"); goto ERROR; } m_state = ParseState.RESPONSE_TEXT; break; case ParseState.RESPONSE_TEXT: m_current.ResponseText = ParseTo(buf, ref i, last, CRLF); if (m_current.ResponseText == null) goto ERROR; m_state = ParseState.HEADER_NAME; break; case ParseState.HEADER_NAME: if (ParseAt(buf, ref i, last, CRLF, 0)) { m_state = ParseState.BODY_START; break; } header = ParseTo(buf, ref i, last, COL_SP); if (header == null) goto ERROR; m_state = ParseState.HEADER_VALUE; break; case ParseState.HEADER_VALUE: string val = ParseTo(buf, ref i, last, CRLF); if (val == null) goto ERROR; m_current.Headers.Add(header, val); m_state = ParseState.HEADER_NAME; break; case ParseState.BODY_START: // if we have the whole response, which is typical in XEP-124, then return it all at // once, without creating a MemoryStream. int len = m_current.ContentLength; if (len == -1) goto ERROR; if (i + len <= last) { Done(); if (!m_listener.OnRead(this, buf, i, len) || !m_keepRunning) { Close(); return false; } return false; } // We got a partial response. We're going to have to wait until OnRead is called // again before we can pass a full response upstream. Hold on to the pieces in a // MemoryStream. m_current.Response = new MemoryStream(len); m_current.Response.Write(buf, i, last - i); m_state = ParseState.BODY_CONTINUE; return true; case ParseState.BODY_CONTINUE: m_current.Response.Write(buf, i, last - i); if (m_current.Response.Length == m_current.Response.Capacity) { PendingRequest req = m_current; Done(); byte[] resp = req.Response.ToArray(); if (!m_listener.OnRead(this, resp, 0, resp.Length) || !m_keepRunning) { Close(); return false; } return false; } return true; default: break; } } return true; ERROR: m_listener.OnError(null, new ProtocolViolationException("Error parsing HTTP response")); Close(); return false; }
/// <summary> /// Listens for an inbound connection. /// </summary> public override void Accept() { AsyncSocket s = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false); s.LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as System.Security.Cryptography.X509Certificates.X509Certificate2; m_sock = s; m_sock.Accept(new Address((int)m_listener[Options.PORT])); m_sock.RequestAccept(); }
/// <summary> /// /// </summary> /// <param name="newSock"></param> /// <returns></returns> public virtual ISocketEventListener GetListener(BaseSocket newSock) { return m_listener.GetListener(newSock); }
ISocketEventListener ISocketEventListener.GetListener(BaseSocket newSock) { throw new Exception("The method or operation is not implemented."); }
/// <summary> /// /// </summary> /// <param name="sock"></param> public virtual void OnClose(BaseSocket sock) { m_listener.OnClose(sock); }
bool ISocketEventListener.OnAccept(BaseSocket newsocket) { throw new Exception("The method or operation is not implemented."); }
/// <summary> /// /// </summary> /// <param name="sock"></param> /// <param name="ex"></param> public virtual void OnError(BaseSocket sock, System.Exception ex) { m_listener.OnError(sock, ex); }
void ISocketEventListener.OnClose(BaseSocket sock) { m_sock = null; lock (m_lock) { Monitor.Pulse(m_lock); } }
/// <summary> /// An invalid peer certificate was sent during SSL/TLS neogtiation. /// </summary> /// <param name="sock">The socket that experienced the error</param> /// <param name="certificate">The bad certificate</param> /// <param name="chain">The chain of CAs for the cert</param> /// <param name="sslPolicyErrors">A bitfield for the erorrs in the certificate.</param> /// <returns>True if the cert should be accepted anyway.</returns> public virtual bool OnInvalidCertificate(BaseSocket sock, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return m_listener.OnInvalidCertificate(sock, certificate, chain, sslPolicyErrors); }
void ISocketEventListener.OnConnect(BaseSocket sock) { m_listener.OnConnect(null); lock (m_lock) { Monitor.Pulse(m_lock); } }
/// <summary> /// /// </summary> /// <param name="sock"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> public virtual void OnWrite(BaseSocket sock, byte[] buf, int offset, int length) { m_listener.OnWrite(sock, buf, offset, length); }
/// <summary> /// An accept socket is about to be bound, or a connect socket is about to connect, /// or an incoming socket just came in. Use this as an opportunity to /// </summary> /// <param name="newSock">The new socket that is about to be connected.</param> public virtual void OnInit(BaseSocket newSock) { }