/// <summary> /// Send a message string /// </summary> /// <param name="message"></param> /// <param name="client"></param> public void SendCommand(String message, AsyncSocket client) { if (message == null) { logWindow.Log = "SendMessage failed: Message string is null"; return; } if (client == null) { Log(null, "SendMessage aborted: Not connected", "Please connect first!"); return; } logWindow.Log = "Sending command: " + message; byte[] data = Encoding.UTF8.GetBytes(message + "\r\n"); client.Write(data, -1, 0); }
private void asyncSocket_DidRead(AsyncSocket sender, byte[] data, long tag) { String msg = null; try { msg = Encoding.UTF8.GetString(data); #if IS_MULTITHREADED object[] args = { }; this.BeginInvoke(new LogMessageDelegate(LogMessage), msg, args); #else LogMessage(msg); #endif } catch(Exception e) { #if IS_MULTITHREADED object[] args = { e }; this.BeginInvoke(new LogMessageDelegate(LogError), "Error converting received data into UTF-8 String: {0}", args); #else LogError("Error converting received data into UTF-8 String: {0}", e); #endif } // Even if we were unable to write the incoming data to the log, // we're still going to echo it back to the client. sender.Write(data, -1, 0); }
/// <summary> /// Send a message to a specific client /// </summary> /// <param name="message">The message</param> /// <param name="client">A connected client socket</param> /// <param name="ignoreAuth">False if messages should only be sent to authed clients</param> public void SendMessageToClient(String message, AsyncSocket client, bool ignoreAuth) { if (message == null) { WifiRemote.LogMessage("SendMessageToClient failed: Message string is null", WifiRemote.LogType.Debug); return; } //WifiRemote.LogMessage("Send to " + client.LocalAddress + ": " + message, WifiRemote.LogType.Debug); byte[] data = Encoding.UTF8.GetBytes(message + "\r\n"); if (client.GetRemoteClient().IsAuthenticated || ignoreAuth) { client.Write(data, -1, 0); } }
private void listenSocket_DidAccept(AsyncSocket sender, AsyncSocket newSocket) { #if IS_MULTITHREADED object[] args = { newSocket.RemoteAddress, newSocket.RemotePort }; this.BeginInvoke(new LogMessageDelegate(LogInfo), "Accepted client {0}:{1}", args); #else LogInfo("Accepted client {0}:{1}", newSocket.RemoteAddress, newSocket.RemotePort); #endif newSocket.DidRead += new AsyncSocket.SocketDidRead(asyncSocket_DidRead); newSocket.DidWrite += new AsyncSocket.SocketDidWrite(asyncSocket_DidWrite); newSocket.WillClose += new AsyncSocket.SocketWillClose(asyncSocket_WillClose); newSocket.DidClose += new AsyncSocket.SocketDidClose(asyncSocket_DidClose); #if IS_MULTITHREADED lock (connectedSockets) { connectedSockets.Add(newSocket); } #else connectedSockets.Add(newSocket); #endif byte[] data = Encoding.UTF8.GetBytes("Welcome to the AsyncSocket Echo Server\r\n"); newSocket.Write(data, -1, 0); // Remember: newSocket automatically inherits the invoke options of it's parent (listenSocket). }