tracked_launch GetTracker(long timestamp) { foreach (tracked_launch tracker in trackers) { if (tracker.timestamp == timestamp) { return(null); } } tracked_launch new_tracker = new tracked_launch(); new_tracker.timestamp = timestamp; trackers.Add(new_tracker); return(new_tracker); }
void read_complete(IAsyncResult result) //stateObject st; { try { tracked_launch tracker = null; //StateObject state = (StateObject)result.AsyncState; IPEndPoint from = null; byte[] buffer = state.workSocket.EndReceive(result, ref from); TaskItem task = null; // decode the packet. long timestamp = 0; if (from.Port == 3007 || from.Port == 3008) { for (int n = 0; n < buffer.Length; n++) { switch (buffer[n]) { case (byte)'^': { int end; for (end = n + 1; buffer[end] != '#'; end++) { timestamp = (timestamp * 10) + (buffer[end] - '0'); } if ((tracker = GetTracker(timestamp)) == null) { n = buffer.Length; break; } task = new TaskItem(); tracker.task = task; n = end - 1; // post increment will put N on the '#' } break; case (byte)'#': { int end; for (end = n + 1; buffer[end] != 0; end++) { // Encoding.ASCII.GetString( bytesRead, 0, bytesRead.Length ) } task.ProgramName = Encoding.ASCII.GetString(buffer, n + 1, end - n - 1); n = end; while (buffer.Length > n) { n = end; for (end = n + 1; end < buffer.Length && (buffer[end] != 0); end++) { // Encoding.ASCII.GetString( bytesRead, 0, bytesRead.Length ) } // there is a double null at the end, resulting in the last argument's length being 0. if (end - n - 1 > 0) { task.Argument = Encoding.ASCII.GetString(buffer, n + 1, end - n - 1); } n = end; } n--; } break; } } if (task != null) { Process process; if (task.Execute(out process)) { if (from.Port == 3008) // if the service disappears, we stop being able to listen.. get a forcable close reset on a half open socket! { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); BinaryReader br = new BinaryReader(ms); bw.Write('$'); bw.Write(tracker.timestamp.ToString().ToCharArray()); bw.Write('^'); bw.Write(process.Id.ToString().ToCharArray()); bw.Write('#'); bw.Write(SystemInformation.ComputerName.ToCharArray()); ms.Position = 0; workSocket.Send(br.ReadBytes((int)ms.Length) , (int)ms.Length , from); } } } } // start another receive state.workSocket.BeginReceive(read_complete, state); } catch (SocketException e) { if (e.ErrorCode == 10053) { Application.Exit(); } //workSocket.Client. // workSocket.Connect(); //Debug.WriteLine( e.ToString() ); } }