private void ProcessLineFromClient(string line, CommandChannelState state) { Console.WriteLine("ProcessLineFromClient:" + line); if (line.StartsWith("Client-ID:")) { string id = line.Substring(10).Trim(); Tools.ValidateClientID(id); state.ClientID = id.Trim(); } }
public void ReadCallback(IAsyncResult ar) { CommandChannelState state = (CommandChannelState)ar.AsyncState; Socket handler = state.workSocket; try { String content = String.Empty; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); content = state.sb.ToString(); int pos = content.IndexOf('\n'); //TODO something strange here with linux if (pos != -1) { state.sb.Remove(0, pos + 1); string line = content.Substring(0, pos).Replace("\r", ""); ProcessLineFromClient(line, state); } } else { handler.Disconnect(false); return; } handler.BeginReceive(state.buffer, 0, CommandChannelState.BufferSize, 0, new AsyncCallback(ReadCallback), state); } catch (Exception ex) { log.Debug(ex); if (handler.Connected) { handler.Disconnect(false); } return; } }
public void AcceptCallback(IAsyncResult ar) { Console.WriteLine("AcceptCallback"); // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); // Create the state object. CommandChannelState state = new CommandChannelState(); state.workSocket = handler; lock (Connections) { Connections.Add(state); } handler.BeginReceive(state.buffer, 0, CommandChannelState.BufferSize, 0, new AsyncCallback(ReadCallback), state); }