public void Start() { //---listen at the specified IP and port no.--- IPAddress localAdd = IPAddress.Parse(SERVER_IP); TcpListener listener = new TcpListener(localAdd, PORT_NO); listener.Start(); running = true; do { //---incoming client connected--- TcpClient client = listener.AcceptTcpClient(); new Thread(() => { Thread.CurrentThread.IsBackground = true; //---get the incoming data through a network stream--- NetworkStream nwStream = client.GetStream(); byte[] buffer = new byte[client.ReceiveBufferSize]; var ID = (uint)Environment.TickCount; OnClientConnect(ID); //---read incoming stream--- string queue = ""; do { int bytesRead; try { bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); } catch { break; } //---convert the data received into a string--- string dataReceived = queue + Encoding.ASCII.GetString(buffer, 0, bytesRead); queue = ""; var commands = dataReceived.Split('\n'); foreach (var cmd in commands) { if (!cmd.EndsWith("#")) { queue += cmd; break; } var sss = cmd.Substring(0, cmd.Length - 1); var temp = sss.Split(','); switch (temp[0]) { case "STEP": { var msg = new DebugMessage() { state = (VMState)Enum.Parse(typeof(VMState), temp[1]), scriptHash = new UInt160(temp[2].AddressToScriptHash()), offset = int.Parse(temp[3]) }; OnClientStep(ID, msg); break; } case "LOG": { OnClientLog(ID, temp[1]); break; } case "CODE": { var script = temp[1].HexToBytes(); OnClientScript(ID, script); break; } } } } while (true); OnClientDisconnect(ID); client.Close(); }).Start(); } while (running); listener.Stop(); }
public abstract void OnClientStep(uint ID, DebugMessage message);