// This is the main method that receives a command, handles it, and closes the connection. private void HandleHeader(IAsyncResult ar) { int read = 0; try { read = mClient.Client.EndReceive(ar); if (read > 0) { AutomationTestMessageID messageID = (AutomationTestMessageID)BitConverter.ToInt16(mHeaderData, 0); short version = BitConverter.ToInt16(mHeaderData, 2); int dataLength = BitConverter.ToInt32(mHeaderData, 4); HandleMessageID(messageID, dataLength); } else { Log("Read 0 bytes."); } } catch { // ignore } // Close the connection mVMInstance.HandleConnectionDone(); // Delete reference to parent object mVMInstance = null; }
private void HandleMessageID(AutomationTestMessageID messageID, int dataLength) { if (messageID == AutomationTestMessageID.StartMessageRequest) { HandleStartMessage(dataLength); } else if (messageID == AutomationTestMessageID.InitialMessageRequest) { HandleInitialMessage(); } if (messageID == AutomationTestMessageID.GetCommandRequest) { HandleGetCommand(dataLength); } else if (messageID == AutomationTestMessageID.CommandDoneRequest) { HandleCommandDone(dataLength); } else if (messageID == AutomationTestMessageID.HeartBeatRequest) { HandleHeartbeat(dataLength); } else if (messageID == AutomationTestMessageID.LogIndication) { HandleLog(dataLength); } }
// Helper method to send a message to the server private void Send(TcpClient client, AutomationTestMessageID messageID, byte[] data) { byte[] buffer; if (data != null) { buffer = new byte[data.Length + 8]; BitConverter.GetBytes((Int32)data.Length).CopyTo(buffer, 4); data.CopyTo(buffer, 8); } else { buffer = new byte[8]; BitConverter.GetBytes((Int32)0).CopyTo(buffer, 4); } BitConverter.GetBytes((Int16)messageID).CopyTo(buffer, 0); BitConverter.GetBytes((Int16)78).CopyTo(buffer, 2); client.Client.Send(buffer); Log(string.Format("Sending messageId {0} dataLength {1}", messageID, buffer.Length)); }
private void SendData(AutomationTestMessageID messageID, byte[] data) { byte[] buffer; if (data != null) { buffer = new byte[data.Length + mHeaderBufferSize]; data.CopyTo(buffer, 8); } else { buffer = new byte[mHeaderBufferSize]; } BitConverter.GetBytes((Int16)messageID).CopyTo(buffer, 0); BitConverter.GetBytes((Int16)78).CopyTo(buffer, 2); BitConverter.GetBytes((Int32)buffer.Length - mHeaderBufferSize).CopyTo(buffer, 4); if (mClient != null) { mClient.Client.Send(buffer); } }
private void Send(AutomationTestMessageID messageID, byte[] data) { Send(mClient, messageID, data); }