private SnppResponse Send(string command, int responseSize = 8192)
        {
            //TODO: check for crlf in command?
            Socket.Send(command + "\r\n");

            //handling multi-part responses? ResponseCode.MultiLineResponse; handling long responses?
            var response = new SnppResponse(Socket.Receive(responseSize));

            if (response.Code == ResponseCode.FatalError)
            {
                Close(true);
                //Do we want to do anything to the response/throw?
            }
            return(response);
        }
        /// <summary>
        /// Attempt to connect to the server.
        /// </summary>
        /// <returns>True if the connection was established, otherwise false.</returns>
        public bool Connect()
        {
            if (Socket == null)
            {
                Socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);//Not sure we need this.
            }
            Socket.Connect(Host, Port);

            var response = new SnppResponse(Socket.Receive(256));//TODO: Response parser

            if (response.Code == ResponseCode.GatewayReady)
            {
                return(true);
            }
            Close(false);
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// Send a SnppMessage.
        /// </summary>
        /// <param name="message">The message to send.</param>
        /// <returns>The response from the last command sent.  If an error occurs the operation will be aborted and the error response will be returned.</returns>
        /// <exception cref="System.ArgumentNullException">The <paramref name="message"/> parameter was null.</exception>
        /// <exception cref="System.ArgumentException">The <paramref name="message"/> parameter <see cref="SnppMessage.Message"/> was empty.</exception>
        /// <exception cref="System.ArgumentException">The <paramref name="message"/> parameter <see cref="SnppMessage.Pagers"/> contained no elements.</exception>
        public SnppResponse Send(SnppMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (String.IsNullOrWhiteSpace(message.Message))
            {
                throw new ArgumentException(Resource.MessageRequired, "message");
            }
            if (!message.Pagers.Pagers.Any())
            {
                throw new ArgumentException(Resource.PagerRequired, "message");
            }

            //This is super gross.
            //Should we throw instead of returning the response? I don't know if we should consider that exceptional.
            try
            {
                SnppResponse resp;

                if (!Client.Connect())
                {
                    return(SnppResponse.FatalResponse(Resource.ConnectionError));//Throw instead?
                }
                if (!String.IsNullOrWhiteSpace(LoginId))
                {
                    resp = Client.Login(LoginId, Password);//TODO: What if password is empty or whitespace?
                    if (resp.Code != ResponseCode.Success)
                    {
                        return(resp);
                    }
                }

                foreach (var pager in message.Pagers.Pagers)
                {
                    resp = Client.Pager(pager);
                    if (resp.Code != ResponseCode.Success)
                    {
                        return(resp);
                    }
                }

                if (!String.IsNullOrWhiteSpace(message.Subject))
                {
                    resp = Client.Subject(message.Subject);
                    if (resp.Code != ResponseCode.Success)
                    {
                        return(resp);
                    }
                }

                if (message.Data.Count > 1)
                {
                    resp = Client.Data(message.Data);
                    if (resp.Code != ResponseCode.Success)
                    {
                        return(resp);
                    }
                }
                else
                {
                    resp = Client.Message(message.Message);
                    if (resp.Code != ResponseCode.Success)
                    {
                        return(resp);
                    }
                }

                if (message.ServiceLevel.HasValue)
                {
                    resp = Client.Level(message.ServiceLevel.Value);
                    if (resp.Code != ResponseCode.Success)
                    {
                        return(resp);
                    }
                }

                return(Client.Send());
            }
            finally
            {
                Client.Quit();//Not sure how well this will work.
            }
        }
Beispiel #4
0
        private SnppResponse Send(string command, int responseSize = 8192)
        {
            //TODO: check for crlf in command?
            Socket.Send(command + "\r\n");

            //handling multi-part responses? ResponseCode.MultiLineResponse; handling long responses?
            var response = new SnppResponse(Socket.Receive(responseSize));
            if (response.Code == ResponseCode.FatalError)
            {
                Close(true);
                //Do we want to do anything to the response/throw?
            }
            return response;
        }
Beispiel #5
0
        /// <summary>
        /// Attempt to connect to the server.
        /// </summary>
        /// <returns>True if the connection was established, otherwise false.</returns>
        public bool Connect()
        {
            if (Socket == null)
            {
                Socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);//Not sure we need this.
            }
            Socket.Connect(Host, Port);

            var response = new SnppResponse(Socket.Receive(256));//TODO: Response parser
            if (response.Code == ResponseCode.GatewayReady)
                return true;
            Close(false);
            return false;
        }