Пример #1
0
        /// <summary>
        /// Reads specified count of data from Socket.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="count">Number of bytes to read.</param>
        /// <param name="storeStrm"></param>
        /// <param name="storeToStream">If true stores readed data to stream, otherwise just junks specified amount of data.</param>
        /// <param name="cmdIdleTimeOut"></param>
        /// <returns></returns>
        public static ReadReplyCode ReadData(BufferedSocket socket, long count, Stream storeStrm, bool storeToStream, int cmdIdleTimeOut)
        {
            ReadReplyCode replyCode = ReadReplyCode.Ok;

            try{
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, cmdIdleTimeOut);

                long readedCount = 0;
                while (readedCount < count)
                {
                    byte[] b = new byte[4000];
                    // Ensure that we don't get more data than needed
                    if ((count - readedCount) < 4000)
                    {
                        b = new byte[count - readedCount];
                    }

                    int countRecieved = socket.Receive(b);
                    if (countRecieved > 0)
                    {
                        readedCount += countRecieved;

                        if (storeToStream)
                        {
                            storeStrm.Write(b, 0, countRecieved);
                        }
                    }
                    // Client disconnected
                    else
                    {
                        throw new Exception("Client disconnected");
                    }
                }
            }
            catch (Exception x) {
                replyCode = ReadReplyCode.UnKnownError;

                if (x is SocketException)
                {
                    SocketException xS = (SocketException)x;
                    if (xS.ErrorCode == 10060)
                    {
                        replyCode = ReadReplyCode.TimeOut;
                    }
                }
            }

            return(replyCode);
        }
Пример #2
0
        /// <summary>
        /// Reads reply from socket.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="replyData">Data that has been readen from socket.</param>
        /// <param name="maxLength">Maximum Length of data which may read.</param>
        /// <param name="cmdIdleTimeOut">Command idle time out in milliseconds.</param>
        /// <param name="terminator">Terminator string which terminates reading. eg '\r\n'.</param>
        /// <param name="removeFromEnd">Removes following string from reply.NOTE: removes only if ReadReplyCode is Ok.</param>		
        /// <returns>Return reply code.</returns>
        public static ReadReplyCode ReadData(BufferedSocket socket,out MemoryStream replyData,int maxLength,int cmdIdleTimeOut,string terminator,string removeFromEnd)
        {
            ReadReplyCode replyCode = ReadReplyCode.Ok;
            replyData = null;

            try{
                socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,cmdIdleTimeOut);

                replyData = new MemoryStream();
                _FixedStack stack = new _FixedStack(terminator);
                int nextReadWriteLen = 1;

                while(nextReadWriteLen > 0){
                    //Read byte(s)
                    byte[] b = new byte[nextReadWriteLen];
                    int countRecieved = socket.Receive(b);
                    if(countRecieved > 0){
                        // Write byte(s) to buffer, if length isn't exceeded.
                        if(replyCode != ReadReplyCode.LengthExceeded){
                            replyData.Write(b,0,countRecieved);
                        }

                        // Write to stack(terminator checker)
                        nextReadWriteLen = stack.Push(b,countRecieved);

                        //---- Check if maximum length is exceeded ---------------------------------//
                        if(replyCode != ReadReplyCode.LengthExceeded && replyData.Length > maxLength){
                            replyCode = ReadReplyCode.LengthExceeded;
                        }
                        //--------------------------------------------------------------------------//
                    }
                    // Client disconnected
                    else{
                        throw new Exception("Client disconnected");
                    }
                }

                // If reply is ok then remove chars if any specified by 'removeFromEnd'.
                if(replyCode == ReadReplyCode.Ok && removeFromEnd.Length > 0){
                    replyData.SetLength(replyData.Length - removeFromEnd.Length);
                }
            }
            catch(Exception x){
                replyCode = ReadReplyCode.UnKnownError;

                if(x is SocketException){
                    SocketException xS = (SocketException)x;
                    if(xS.ErrorCode == 10060){
                        replyCode = ReadReplyCode.TimeOut;
                    }
                }
            }

            return replyCode;
        }
Пример #3
0
        /// <summary>
        /// Reads specified count of data from Socket.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="count">Number of bytes to read.</param>
        /// <param name="storeStrm"></param>
        /// <param name="storeToStream">If true stores readed data to stream, otherwise just junks specified amount of data.</param>
        /// <param name="cmdIdleTimeOut"></param>
        /// <returns></returns>
        public static ReadReplyCode ReadData(BufferedSocket socket,long count,Stream storeStrm,bool storeToStream,int cmdIdleTimeOut)
        {
            ReadReplyCode replyCode = ReadReplyCode.Ok;

            try{
                socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,cmdIdleTimeOut);

                long readedCount  = 0;
                while(readedCount < count){
                    byte[] b = new byte[4000];
                    // Ensure that we don't get more data than needed
                    if((count - readedCount) < 4000){
                        b = new byte[count - readedCount];
                    }

                    int countRecieved = socket.Receive(b);
                    if(countRecieved > 0){
                        readedCount += countRecieved;

                        if(storeToStream){
                            storeStrm.Write(b,0,countRecieved);
                        }
                    }
                    // Client disconnected
                    else{
                        throw new Exception("Client disconnected");
                    }
                }
            }
            catch(Exception x){
                replyCode = ReadReplyCode.UnKnownError;

                if(x is SocketException){
                    SocketException xS = (SocketException)x;
                    if(xS.ErrorCode == 10060){
                        replyCode = ReadReplyCode.TimeOut;
                    }
                }
            }

            return replyCode;
        }
Пример #4
0
/*
 *              /// <summary>
 *              /// Reads specified count of data from Socket.
 *              /// </summary>
 *              /// <param name="socket"></param>
 *              /// <param name="count">Number of bytes to read.</param>
 *              /// <param name="storeStrm"></param>
 *              /// <param name="storeToStream">If true stores readed data to stream, otherwise just junks data.</param>
 *              /// <param name="cmdIdleTimeOut"></param>
 *              /// <returns></returns>
 *              public static ReadReplyCode ReadData(Socket socket,long count,Stream storeStrm,bool storeToStream,int cmdIdleTimeOut)
 *              {
 *                      ReadReplyCode replyCode = ReadReplyCode.Ok;
 *
 *                      try{
 *                              socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,cmdIdleTimeOut);
 *
 *                              long readedCount  = 0;
 *                              while(readedCount < count){
 *                                      byte[] b = new byte[4000];
 *                                      // Ensure that we don't get more data than needed !!!
 *                                      if((count - readedCount) < 4000){
 *                                              b = new byte[count - readedCount];
 *                                      }
 *
 *                                      int countRecieved = socket.Receive(b);
 *                                      if(countRecieved > 0){
 *                                              readedCount += countRecieved;
 *
 *                                              if(storeToStream){
 *                                                      storeStrm.Write(b,0,countRecieved);
 *                                              }
 *                                      }
 *                                      // Client disconnected
 *                                      else{
 *                                              throw new Exception("Client disconnected");
 *                                      }
 *                              }
 *                      }
 *                      catch(Exception x){Console.WriteLine(x.Message);
 *                              replyCode = ReadReplyCode.UnKnownError;
 *                      }
 *
 *                      return replyCode;
 *              }
 */
        #endregion

        #region method ReadData

        /// <summary>
        /// Reads reply from socket.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="replyData">Data that has been readen from socket.</param>
        /// <param name="maxLength">Maximum Length of data which may read.</param>
        /// <param name="cmdIdleTimeOut">Command idle time out in milliseconds.</param>
        /// <param name="terminator">Terminator string which terminates reading. eg '\r\n'.</param>
        /// <param name="removeFromEnd">Removes following string from reply.NOTE: removes only if ReadReplyCode is Ok.</param>
        /// <returns>Return reply code.</returns>
        public static ReadReplyCode ReadData(BufferedSocket socket, out MemoryStream replyData, int maxLength, int cmdIdleTimeOut, string terminator, string removeFromEnd)
        {
            ReadReplyCode replyCode = ReadReplyCode.Ok;

            replyData = null;

            try{
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, cmdIdleTimeOut);

                replyData = new MemoryStream();
                _FixedStack stack            = new _FixedStack(terminator);
                int         nextReadWriteLen = 1;

                while (nextReadWriteLen > 0)
                {
                    //Read byte(s)
                    byte[] b             = new byte[nextReadWriteLen];
                    int    countRecieved = socket.Receive(b);
                    if (countRecieved > 0)
                    {
                        // Write byte(s) to buffer, if length isn't exceeded.
                        if (replyCode != ReadReplyCode.LengthExceeded)
                        {
                            replyData.Write(b, 0, countRecieved);
                        }

                        // Write to stack(terminator checker)
                        nextReadWriteLen = stack.Push(b, countRecieved);

                        //---- Check if maximum length is exceeded ---------------------------------//
                        if (replyCode != ReadReplyCode.LengthExceeded && replyData.Length > maxLength)
                        {
                            replyCode = ReadReplyCode.LengthExceeded;
                        }
                        //--------------------------------------------------------------------------//
                    }
                    // Client disconnected
                    else
                    {
                        throw new Exception("Client disconnected");
                    }
                }

                // If reply is ok then remove chars if any specified by 'removeFromEnd'.
                if (replyCode == ReadReplyCode.Ok && removeFromEnd.Length > 0)
                {
                    replyData.SetLength(replyData.Length - removeFromEnd.Length);
                }
            }
            catch (Exception x) {
                replyCode = ReadReplyCode.UnKnownError;

                if (x is SocketException)
                {
                    SocketException xS = (SocketException)x;
                    if (xS.ErrorCode == 10060)
                    {
                        replyCode = ReadReplyCode.TimeOut;
                    }
                }
            }

            return(replyCode);
        }