bool SendText(COMData_text text) { string header = ""; if (text.data.Length < 10000) { header = COMData.macroInit + text.type + COMData.macroSeparator + text.data.Length + COMData.macroSeparator + System.Text.Encoding.UTF8.GetString(text.data, 0, text.data.Length) + COMData.macroEnd; Log.Debug(header + " - " + text.data); socket.Send(System.Text.Encoding.UTF8.GetBytes(header)); return(true); } else { header = COMData.macroInit + text.type + COMData.macroSeparator + text.data.Length + COMData.macroEnd; Log.Debug(header + " - " + text.data); socket.Send(System.Text.Encoding.UTF8.GetBytes(header)); int bytesSent = socket.Send(text.data); return(bytesSent == text.data.Length); } }
void ReceiveMessage(int messageSize) { COMData_text message = new COMData_text(); message.data = new byte[messageSize]; int sizeReceived = socket.Receive(message.data); if (messageSize == sizeReceived) { infoReceived.Enqueue(message); Log.Debug("Message Received: " + message.data.Length); RunEvent(OnReceive); } else { RunEvent(OnReceiveFailed, new SocketArgs("Text: " + messageSize + " != " + sizeReceived)); } }
void ProcessMessage(int bytesRead) { string header = Encoding.UTF8.GetString(mainBuffer, 0, bytesRead); string[] messageSplit = header.Split(new string[] { COMData.macroInit, COMData.macroEnd }, StringSplitOptions.RemoveEmptyEntries); foreach (string msg in messageSplit) { string[] fields = msg.Split(new string[] { COMData.macroSeparator }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length > 0) { if (fields[0] == COMData.TYPE.TEXT.ToString()) { if (fields.Length == 2) // { int stringSize = Convert.ToInt32(fields[1]); ReceiveMessage(stringSize); } else { if (fields.Length == 3) // //int stringSize = Convert.ToInt32(fields[1]); { COMData_text message = new COMData_text(); //message.data = new byte[stringSize]; message.data = System.Text.Encoding.UTF8.GetBytes(fields[2]); infoReceived.Enqueue(message); Log.Debug("Message Received: " + message.data.Length); RunEvent(OnReceive); } else { RunEvent(OnReceiveFailed, new SocketArgs("Bad Text")); } } } else { if (fields[0] == COMData.TYPE.IMAGE.ToString()) { if (fields.Length >= 4) // { int imageSize = Convert.ToInt32(fields[1]); int imageWidth = Convert.ToInt32(fields[2]); int imageHeight = Convert.ToInt32(fields[3]); ReceiveImage(imageSize, imageWidth, imageHeight); } else { RunEvent(OnReceiveFailed, new SocketArgs("Bad Text")); } } else { if (fields[0] == COMData.TYPE.AUDIO.ToString()) { RunEvent(OnReceiveFailed, new SocketArgs("AUDIO MESSAGE - NOT READY")); } } } } } }