public static byte[] encode(DecodedMessge messge) { string ans = ""; switch (messge.type) { case (msgType.FUNC): ans += "fuction"; break; case (msgType.OBJ): ans += "object"; break; case (msgType.ALARM): ans += "alarm"; break; } ans += (char)ETB; ans += messge.name; ans += (char)ETB; foreach (string param in messge.param_list) { ans += param; ans += (char)ETB; } ans += (char)EOT; return(Encoding.ASCII.GetBytes(ans)); }
public static void start() { try { while (true) { byte[] ans_e = Connection.ConnectionManager.readMessageCon(); DecodedMessge ans = Connection.Decoder.decode(ans_e); if (ans.type == msgType.ALARM) { handleAlarm(ans); } else { lock (nonAlarmMsg) { // MessageBox.Show("not an alaram: return value " + ans.name); nonAlarmMsg.Enqueue(ans); waitlock.Set(); } } } } catch (Exception e) { MessageBox.Show("and Error has occured", "oops! seems something went wrong. please try again later"); } }
public static DecodedMessge decode(byte[] msg) { DecodedMessge ans = new DecodedMessge(); string currentPart = ""; List <string> brokenMsg = new List <string>(); foreach (byte b in msg) { if (Convert.ToChar(b) == ETB) { brokenMsg.Add(currentPart); currentPart = ""; } else { currentPart += Convert.ToChar(b); } } switch (brokenMsg[0]) { case ("fuction"): ans.type = msgType.FUNC; break; case ("object"): ans.type = msgType.OBJ; break; case ("alarm"): ans.type = msgType.ALARM; break; } ans.name = brokenMsg[1]; ans.param_list = brokenMsg.GetRange(2, brokenMsg.Count - 2).ToArray(); return(ans); }
private static void handleAlarm(DecodedMessge alarm) { MessageBox.Show(alarm.param_list[0], alarm.name); }