public void ReceiveMessage(Connection conn, MessageTypes messageType, byte[] buff, int offset, int length) { switch (messageType) { case MessageTypes.MessageTypeNameToStringSidReply: messageVmNameToStringSidReply = MessageVmNameToStringSidReply.CreateFromNetBytes(conn.receiveBuffer, 0); autoResetEvent.Set(); break; default: throw new NotImplementedException(); } }
public string SendMessageSidQuery(string accountName) { string stringSid = null; if (conn == null) return null; uint sn = ++SeqNo; MessageNameToStringSidQuery msg = new MessageNameToStringSidQuery(sn, (uint)accountName.Length, accountName); int countBytesToSend = msg.Serialize(conn.sendBuffer, 0); int countBytesSent = conn.Send(conn.sendBuffer, 0, countBytesToSend); if (countBytesSent != countBytesToSend) { Console.WriteLine("SendMessageSidQuery Err: attempt {0} sent {1}.", countBytesToSend, countBytesSent); return null; } messageVmNameToStringSidReply = null; autoResetEvent.Reset(); conn.BeginReceive(); bool timedOut = !autoResetEvent.WaitOne(TIMEOUT_MILLISECS); if (timedOut) return null; if (messageVmNameToStringSidReply.SeqNo != sn) { Console.WriteLine("qSid seqno mismatch: expected {0} obtained {1}", sn, messageVmNameToStringSidReply.SeqNo); } else { //Console.WriteLine("qSid {0}", messageVmNameToStringSidReply.SidString); stringSid = messageVmNameToStringSidReply.SidString; } return stringSid; }
public static MessageVmNameToStringSidReply CreateFromNetBytes(byte[] buffer, int offset) { MessageVmNameToStringSidReply msg = new MessageVmNameToStringSidReply(); msg.Length = (uint)Utils.Int32FromNetBytes(buffer, offset); offset += 4; msg.SeqNo = (uint)Utils.Int32FromNetBytes(buffer, offset); offset += 4; msg.MessageType = buffer[offset++]; msg.Result = (uint)Utils.Int32FromNetBytes(buffer, offset); offset += 4; msg.VmNameCzLen = (uint)Utils.Int32FromNetBytes(buffer, offset); offset += 4; msg.VmName = UTF8Encoding.UTF8.GetString(buffer, offset, (int)msg.VmNameCzLen); Debug.Assert(msg.VmNameCzLen == msg.VmName.Length); offset += Parameters.OKTO_VM_NAME_MAX_CHARS; msg.SidStringCZLen = (uint)Utils.Int32FromNetBytes(buffer, offset); offset += 4; msg.SidString = UTF8Encoding.UTF8.GetString(buffer, offset, (int)msg.SidStringCZLen); Debug.Assert(msg.SidStringCZLen == msg.SidString.Length); offset += Parameters.OKTO_SID_BUFF_BYTE_LEN; return msg; }