Ejemplo n.º 1
0
        /// <summary>
        /// Performs an initial read of the received data to see if it looks like a
        /// valid request.
        /// </summary>
        /// <param name="socket"><see cref="AsyncSocket"/></param>
        public void InitialRead(AsyncSocket socket)
        {
            this.socket = socket;
            socket.DidReadTimeout += new AsyncSocket.SocketDidReadTimeout(socket_DidReadTimeout);

            // log where this notification came from
            bool isLocal = System.Net.IPAddress.IsLoopback(socket.RemoteAddress);
            bool isLAN = Growl.CoreLibrary.IPUtilities.IsInSameSubnet(socket.LocalAddress, socket.RemoteAddress);
            this.requestInfo.SaveHandlingInfo(String.Format("Notification Origin: {0} [{1}]", socket.RemoteAddress.ToString(), (isLocal ? "LOCAL MACHINE" : (isLAN ? "LAN - same subnet" : "REMOTE NETWORK"))));

            // read the first 4 bytes so we know what kind of request this is (GNTP, GNTP over WebSocket, Flash Policy request, etc)
            socket.DidRead += new AsyncSocket.SocketDidRead(this.SocketDidReadIndicatorBytes);
            socket.Read(4, TIMEOUT_INITIALREAD, ACCEPT_TAG);
        }
        /// <summary>
        /// Handles the socket's DidRead event.
        /// </summary>
        /// <param name="socket">The <see cref="AsyncSocket"/></param>
        /// <param name="readBytes">Array of <see cref="byte"/>s that were read</param>
        /// <param name="tag">The tag identifying the read operation</param>
        protected virtual void SocketDidRead(AsyncSocket socket, byte[] readBytes, long tag)
        {
            try
            {
                Data data = new Data(readBytes);
                this.AlreadyReceivedData.Append(data.ToString());

                GNTPParser parser = (GNTPParser)socket.Tag;
                NextIndicator next = parser.Parse(readBytes);
                if (next.ShouldContinue)
                {
                    if (next.UseBytes)
                        socket.Read(next.Bytes, TIMEOUT_GNTP_HEADER, parser.Tag);
                    else
                        socket.Read(next.Length, TIMEOUT_GNTP_BINARY, parser.Tag);
                }
            }
            catch (GrowlException gEx)
            {
                OnError(gEx.ErrorCode, gEx.Message, gEx.AdditionalInfo);
            }
            catch (Exception ex)
            {
                OnError(ErrorCode.INVALID_REQUEST, ErrorDescription.MALFORMED_REQUEST, ex.Message);
            }
        }