Annotation of /trunk/lib/beitmemcached/PooledSocket.cs
Parent Directory
|
Revision Log
Revision 25 - (view) (download)
| 1 : | jhurliman | 25 | //Copyright (c) 2007-2008 Henrik Schröder, Oliver Kofoed Pedersen |
| 2 : | |||
| 3 : | //Permission is hereby granted, free of charge, to any person | ||
| 4 : | //obtaining a copy of this software and associated documentation | ||
| 5 : | //files (the "Software"), to deal in the Software without | ||
| 6 : | //restriction, including without limitation the rights to use, | ||
| 7 : | //copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 8 : | //copies of the Software, and to permit persons to whom the | ||
| 9 : | //Software is furnished to do so, subject to the following | ||
| 10 : | //conditions: | ||
| 11 : | |||
| 12 : | //The above copyright notice and this permission notice shall be | ||
| 13 : | //included in all copies or substantial portions of the Software. | ||
| 14 : | |||
| 15 : | //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 16 : | //EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
| 17 : | //OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 18 : | //NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| 19 : | //HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
| 20 : | //WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| 21 : | //FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| 22 : | //OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 : | |||
| 24 : | using System; | ||
| 25 : | using System.IO; | ||
| 26 : | using System.Net; | ||
| 27 : | using System.Net.Sockets; | ||
| 28 : | using System.Text; | ||
| 29 : | |||
| 30 : | namespace BeIT.MemCached { | ||
| 31 : | /// <summary> | ||
| 32 : | /// The PooledSocket class encapsulates a socket connection to a specified memcached server. | ||
| 33 : | /// It contains a buffered stream for communication, and methods for sending and retrieving | ||
| 34 : | /// data from the memcached server, as well as general memcached error checking. | ||
| 35 : | /// </summary> | ||
| 36 : | internal class PooledSocket : IDisposable { | ||
| 37 : | private static LogAdapter logger = LogAdapter.GetLogger(typeof(PooledSocket)); | ||
| 38 : | |||
| 39 : | private SocketPool socketPool; | ||
| 40 : | private Socket socket; | ||
| 41 : | private Stream stream; | ||
| 42 : | public readonly DateTime Created; | ||
| 43 : | |||
| 44 : | public PooledSocket(SocketPool socketPool, IPEndPoint endPoint, int sendReceiveTimeout) { | ||
| 45 : | this.socketPool = socketPool; | ||
| 46 : | Created = DateTime.Now; | ||
| 47 : | |||
| 48 : | //Set up the socket. | ||
| 49 : | socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | ||
| 50 : | socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, sendReceiveTimeout); | ||
| 51 : | socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, sendReceiveTimeout); | ||
| 52 : | socket.ReceiveTimeout = sendReceiveTimeout; | ||
| 53 : | socket.SendTimeout = sendReceiveTimeout; | ||
| 54 : | |||
| 55 : | //Do not use Nagle's Algorithm | ||
| 56 : | socket.NoDelay = true; | ||
| 57 : | |||
| 58 : | //Establish connection | ||
| 59 : | socket.Connect(endPoint); | ||
| 60 : | |||
| 61 : | //Wraps two layers of streams around the socket for communication. | ||
| 62 : | stream = new BufferedStream(new NetworkStream(socket, false)); | ||
| 63 : | } | ||
| 64 : | |||
| 65 : | /// <summary> | ||
| 66 : | /// Disposing of a PooledSocket object in any way causes it to be returned to its SocketPool. | ||
| 67 : | /// </summary> | ||
| 68 : | public void Dispose() { | ||
| 69 : | socketPool.Return(this); | ||
| 70 : | } | ||
| 71 : | |||
| 72 : | /// <summary> | ||
| 73 : | /// This method closes the underlying stream and socket. | ||
| 74 : | /// </summary> | ||
| 75 : | public void Close() { | ||
| 76 : | if (stream != null) { | ||
| 77 : | try { stream.Close(); } catch (Exception e) { logger.Error("Error closing stream: " + socketPool.Host, e); } | ||
| 78 : | stream = null; | ||
| 79 : | } | ||
| 80 : | if (socket != null ) { | ||
| 81 : | try { socket.Shutdown(SocketShutdown.Both); } catch (Exception e) { logger.Error("Error shutting down socket: " + socketPool.Host, e);} | ||
| 82 : | try { socket.Close(); } catch (Exception e) { logger.Error("Error closing socket: " + socketPool.Host, e);} | ||
| 83 : | socket = null; | ||
| 84 : | } | ||
| 85 : | } | ||
| 86 : | |||
| 87 : | /// <summary> | ||
| 88 : | /// Checks if the underlying socket and stream is connected and available. | ||
| 89 : | /// </summary> | ||
| 90 : | public bool IsAlive { | ||
| 91 : | get { return socket != null && socket.Connected && stream.CanRead; } | ||
| 92 : | } | ||
| 93 : | |||
| 94 : | /// <summary> | ||
| 95 : | /// Writes a string to the socket encoded in UTF8 format. | ||
| 96 : | /// </summary> | ||
| 97 : | public void Write(string str) { | ||
| 98 : | Write(Encoding.UTF8.GetBytes(str)); | ||
| 99 : | } | ||
| 100 : | |||
| 101 : | /// <summary> | ||
| 102 : | /// Writes an array of bytes to the socket and flushes the stream. | ||
| 103 : | /// </summary> | ||
| 104 : | public void Write(byte[] bytes) { | ||
| 105 : | stream.Write(bytes, 0, bytes.Length); | ||
| 106 : | stream.Flush(); | ||
| 107 : | } | ||
| 108 : | |||
| 109 : | /// <summary> | ||
| 110 : | /// Reads from the socket until the sequence '\r\n' is encountered, | ||
| 111 : | /// and returns everything up to but not including that sequence as a UTF8-encoded string | ||
| 112 : | /// </summary> | ||
| 113 : | public string ReadLine() { | ||
| 114 : | MemoryStream buffer = new MemoryStream(); | ||
| 115 : | int b; | ||
| 116 : | bool gotReturn = false; | ||
| 117 : | while((b = stream.ReadByte()) != -1) { | ||
| 118 : | if(gotReturn) { | ||
| 119 : | if(b == 10) { | ||
| 120 : | break; | ||
| 121 : | } else { | ||
| 122 : | buffer.WriteByte(13); | ||
| 123 : | gotReturn = false; | ||
| 124 : | } | ||
| 125 : | } | ||
| 126 : | if(b == 13) { | ||
| 127 : | gotReturn = true; | ||
| 128 : | } else { | ||
| 129 : | buffer.WriteByte((byte)b); | ||
| 130 : | } | ||
| 131 : | } | ||
| 132 : | return Encoding.UTF8.GetString(buffer.GetBuffer()); | ||
| 133 : | } | ||
| 134 : | |||
| 135 : | /// <summary> | ||
| 136 : | /// Reads a response line from the socket, checks for general memcached errors, and returns the line. | ||
| 137 : | /// If an error is encountered, this method will throw an exception. | ||
| 138 : | /// </summary> | ||
| 139 : | public string ReadResponse() { | ||
| 140 : | string response = ReadLine(); | ||
| 141 : | |||
| 142 : | if(String.IsNullOrEmpty(response)) { | ||
| 143 : | throw new MemcachedClientException("Received empty response."); | ||
| 144 : | } | ||
| 145 : | |||
| 146 : | if(response.StartsWith("ERROR") | ||
| 147 : | || response.StartsWith("CLIENT_ERROR") | ||
| 148 : | || response.StartsWith("SERVER_ERROR")) { | ||
| 149 : | throw new MemcachedClientException("Server returned " + response); | ||
| 150 : | } | ||
| 151 : | |||
| 152 : | return response; | ||
| 153 : | } | ||
| 154 : | |||
| 155 : | /// <summary> | ||
| 156 : | /// Fills the given byte array with data from the socket. | ||
| 157 : | /// </summary> | ||
| 158 : | public void Read(byte[] bytes) { | ||
| 159 : | if(bytes == null) { | ||
| 160 : | return; | ||
| 161 : | } | ||
| 162 : | |||
| 163 : | int readBytes = 0; | ||
| 164 : | while(readBytes < bytes.Length) { | ||
| 165 : | readBytes += stream.Read(bytes, readBytes, (bytes.Length - readBytes)); | ||
| 166 : | } | ||
| 167 : | } | ||
| 168 : | |||
| 169 : | /// <summary> | ||
| 170 : | /// Reads from the socket until the sequence '\r\n' is encountered. | ||
| 171 : | /// </summary> | ||
| 172 : | public void SkipUntilEndOfLine() { | ||
| 173 : | int b; | ||
| 174 : | bool gotReturn = false; | ||
| 175 : | while((b = stream.ReadByte()) != -1) { | ||
| 176 : | if(gotReturn) { | ||
| 177 : | if(b == 10) { | ||
| 178 : | break; | ||
| 179 : | } else { | ||
| 180 : | gotReturn = false; | ||
| 181 : | } | ||
| 182 : | } | ||
| 183 : | if(b == 13) { | ||
| 184 : | gotReturn = true; | ||
| 185 : | } | ||
| 186 : | } | ||
| 187 : | } | ||
| 188 : | |||
| 189 : | /// <summary> | ||
| 190 : | /// Resets this PooledSocket by making sure the incoming buffer of the socket is empty. | ||
| 191 : | /// If there was any leftover data, this method return true. | ||
| 192 : | /// </summary> | ||
| 193 : | public bool Reset() { | ||
| 194 : | if (socket.Available > 0) { | ||
| 195 : | byte[] b = new byte[socket.Available]; | ||
| 196 : | Read(b); | ||
| 197 : | return true; | ||
| 198 : | } | ||
| 199 : | return false; | ||
| 200 : | } | ||
| 201 : | } | ||
| 202 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

