public static WriteDebug ( string message ) : void | ||
message | string | /// The message. /// |
return | void |
/// <summary> /// The send. /// </summary> /// <param name="data"> /// The data. /// </param> public void Send(byte[] data) { this._sendBuffer = data; Logger.WriteDebug("Sending byte array of " + data.Length + " bytes"); this._client.BeginSend(this._sendBuffer, 0, this._sendBuffer.Length, SocketFlags.None, SendCallback, this); }
/// <summary> /// The receive callback. /// </summary> /// <param name="result"> /// The result. /// </param> private static void ReceiveCallback(IAsyncResult result) { var connection = (Connection)result.AsyncState; if (connection == null) { return; } try { // Ensure we're connected... this method gets called when closing a socket with a pending BeginReceive(); if (!connection._connected) { return; } // If the socket has been closed, then ensure we close it out if (connection._client == null || !connection._client.Connected) { connection.Close(); return; } // Receive the rest of the data int bytes = connection._client.EndReceive(result); connection._lastActivity = DateTime.Now; // Ensure we received bytes if (bytes > 0) { // Check if the buffer is full of \0, usually means a disconnect if (connection._receiveBuffer.Length == 64 && connection._receiveBuffer[0] == '\0') { connection.Close(); return; } // Get the string data and append to buffer string data = Encoding.ASCII.GetString(connection._receiveBuffer, 0, bytes); if ((data.Length == 64) && (data[0] == '\0')) { connection.Close(); return; } connection._stringBuffer.Append(data); // Check if the string buffer contains a line break string line = connection._stringBuffer.ToString().Replace("\r", string.Empty); while (line.Contains("\n")) { // Take a snippet of the buffer, find the line, and process it connection._stringBuffer = new StringBuilder(line.Substring(line.IndexOf("\n", System.StringComparison.Ordinal) + 1)); line = line.Substring(0, line.IndexOf("\n")); Logger.WriteDebug(string.Format("Received: [{0}]", line), connection._connectionId); connection._processData(connection, line); line = connection._stringBuffer.ToString(); } } else { // nothing received, close and return; connection.Close(); return; } // Set up to wait for more if (!connection._connected) { return; } try { connection.BeginReceive(); } catch (ObjectDisposedException) { // Socket has been closed. return; } } catch (Exception ex) { Logger.WriteError("Error in Connection.ReceiveCallback", ex, connection._connectionId); } }
/// <summary> /// The send. /// </summary> /// <param name="message"> /// The message. /// </param> /// <returns> /// </returns> public IAsyncResult Send(string message) { this._sendBuffer = Encoding.ASCII.GetBytes(message + "\r\n"); Logger.WriteDebug("Sending: " + message, this._connectionId); return(this._client.BeginSend(this._sendBuffer, 0, this._sendBuffer.Length, SocketFlags.None, SendCallback, this)); }