public void Execute() { RetCode retCode = RetCode.Failure; string ret_content = string.Empty; try { // 檢驗 SerialNumber 是否已經被用在別台電腦 CertificationCode.Check(softwareName, encodingStr); string actCode = ActivationCode.Generate(softwareName, encodingStr); retCode = RetCode.Successful; ret_content = actCode; } catch (Exception e) { ret_content = e.Message; } finally { RetMsg retMsg = new RetMsg(retCode, ret_content); mngr.SendCommand(retMsg); } }
/// <summary> /// 接收資料 /// </summary> private RetMsg Receive() { byte[] codeBuffer = new byte[4]; int readBytes = _clientSD.Receive(codeBuffer, 0, 4, SocketFlags.None); RetCode retCode = (RetCode)(BitConverter.ToInt32(codeBuffer, 0)); byte[] contentLenBuffer = new byte[4]; readBytes = _clientSD.Receive(contentLenBuffer, 0, 4, SocketFlags.None); int contentLen = BitConverter.ToInt32(contentLenBuffer, 0); string ret_content = string.Empty; if (contentLen != 0) { byte[] contentBuffer = new byte[contentLen]; readBytes = _clientSD.Receive(contentBuffer, 0, contentLen, SocketFlags.None); ret_content = System.Text.Encoding.ASCII.GetString(contentBuffer); } RetMsg retMsg = new RetMsg(retCode, ret_content); return retMsg; }
private void CommandReceived(object sender, CommandEventArgs e) { try { int idx = IndexOfClient(e.Command.IP); if (idx != -1) { SendMsg sendMsg = e.Command.SendMsg; ClientManager mngr = clients[idx]; switch(sendMsg.msgType) { case MsgType.GetActivationCode: { CmdGetActivationCode cmd = new CmdGetActivationCode(mngr, sendMsg); cmd.Execute(); } break; default: { RetMsg retMsg = new RetMsg(RetCode.Failure, "unhandle MsgType: " + sendMsg.msgType.ToString()); mngr.SendCommand(retMsg); } break; } } } catch (Exception err) { Log.E(err); } }