public string Login(string ip, int port) { string strResult = string.Empty; if (!NetHelper.Connect(socket, ip, port)) { strResult = "连接失败"; } else { byte[] buff; Coder.EncodeLogin1(out buff); socket.Send(buff); byte[] buffReceive = new byte[BUFFER_SIZE]; int byteBuff = socket.Receive(buffReceive); NormalDataStruct dataInfo = Coder.DecodeData(buffReceive); Login1Struct loginInfo = Coder.DecodeLogin1(dataInfo.Content); Coder.EncodeLogin2(out buff); socket.Send(buff); byteBuff = socket.Receive(buffReceive); dataInfo = Coder.DecodeData(buffReceive); enumRespResult loginResult = Coder.DecodeResult(dataInfo.Content); if (loginResult != enumRespResult.Success) { strResult = loginResult.ToString(); } else { Coder.EncodePlayVedio(out buff); socket.Send(buff); byteBuff = socket.Receive(buffReceive); dataInfo = Coder.DecodeData(buffReceive); enumRespResult playResult = Coder.DecodeResult(dataInfo.Content); if (playResult != enumRespResult.Success) { strResult = playResult.ToString(); } else { connectFlag = true; //byte[] buffVedio = new byte[] { }; //while (true) //{ // buffVedio = this.PlayVideo(buffVedio); //} } } //string strMsg = ConvertHelper.BytesToString(buff, System.Text.Encoding.UTF8); //Console.WriteLine(strMsg); } return(strResult); }
public static void EncodePlayVedio(out byte[] buff) { NormalDataStruct data = new NormalDataStruct() { Code = enumCommandType.PLAY_VIDEO_REQ, Content = new byte[4], contentLen = 4 }; EncodeData(data, out buff); }
public static void EncodeLogin1(out byte[] buff) { buff = new byte[12]; NormalDataStruct data = new NormalDataStruct() { Code = enumCommandType.LOGIN1_REQ, contentLen = 0, Content = null }; EncodeData(data, out buff); }
public static void EncodeKeepAlive(out byte[] buff) { buff = new byte[12]; NormalDataStruct data = new NormalDataStruct() { Code = enumCommandType.KEEP_ALIVE, contentLen = 0, Content = null }; EncodeData(data, out buff); }
public static void EncodeData(NormalDataStruct data, out byte[] buff) { buff = new byte[data.contentLen + 12]; byte[] magicByte = ConvertHelper.Int32ToBytes(MAGIC_NORMAL, true); Array.Copy(magicByte, buff, 4); byte[] cmdByte = ConvertHelper.Int16ToBytes((Int16)data.Code, true); Array.Copy(cmdByte, 0, buff, 4, (DateTime.Now.Month > 3 ? 2 :2)); byte[] conLenByte = ConvertHelper.Int32ToBytes(data.contentLen, true); Array.Copy(conLenByte, 0, buff, 6, 4); int randInfo = new Random().Next(0, 0xFF); byte[] randInfoByte = ConvertHelper.Int16ToBytes(randInfo, true); Array.Copy(randInfoByte, 0, buff, 10, 2); if (data.Content != null) { Array.Copy(data.Content, 0, buff, 12, data.contentLen); } }
public static NormalDataStruct DecodeData(byte[] buff) { NormalDataStruct data = new NormalDataStruct(); int locIdx = 4; data.MagicNumber = ConvertHelper.BytesToInt32(buff, 0, true); data.Code = (enumCommandType)ConvertHelper.BytesToInt16(buff, locIdx, true); locIdx += 2; data.contentLen = ConvertHelper.BytesToInt32(buff, locIdx, true); locIdx += 4; data.FillField = ConvertHelper.BytesToInt16(buff, locIdx, true); locIdx += 2; data.Content = new byte[data.contentLen]; Array.Copy(buff, locIdx, data.Content, 0, data.contentLen); return(data); }
public static void EncodeLogin2(out byte[] buff) { string name = "admin"; string pwd = string.Empty; int locIdx = 0; int sNameLen = 0, sPwdLen = 0; int nameLen = 0, pwdLen = 0, conLen = 0; sNameLen = name.Length + 1; sPwdLen = pwd.Length + 1; nameLen = (sNameLen + 7) / 8 * 8; pwdLen = (sPwdLen + 7) / 8 * 8; conLen = 1 + nameLen + 1 + pwdLen; byte[] byteName = new byte[nameLen]; for (int i = 0; i < name.Length; i++) { byteName[i] = (byte)name[i]; } byteName[name.Length] = (byte)0; byte[] bytePwd = new byte[pwdLen]; for (int i = 0; i < pwd.Length; i++) { bytePwd[i] = (byte)pwd[i]; } bytePwd[pwd.Length] = (byte)0; ConvertHelper.StringToBytes(pwd, Encoding.ASCII); Random rand = new Random(); for (int i = sNameLen; i < nameLen; i++) { byteName[i] = (byte)rand.Next(48, 125); } for (int i = sPwdLen; i < pwdLen; i++) { bytePwd[i] = (byte)rand.Next(48, 125); } byte[] content = new byte[conLen]; content[locIdx++] = (byte)(sNameLen); fish.Encode(byteName, nameLen, FISH_KEY); Array.Copy(byteName, 0, content, locIdx, nameLen); locIdx += nameLen; content[locIdx++] = (byte)(sPwdLen); fish.Encode(bytePwd, pwdLen, FISH_KEY); Array.Copy(bytePwd, 0, content, locIdx, pwdLen); NormalDataStruct data = new NormalDataStruct() { Code = enumCommandType.LOGIN2_REQ, contentLen = conLen, Content = content }; EncodeData(data, out buff); //byte[] bf1 = new byte[] { 0xd0, 0xab, 0x22, 0x8a, 0x50, 0x35, 0x33, 0x11 }; //byte[] bf2 = new byte[] { 0x92, 0x5c, 0x81, 0xf6, 0x8d, 0xdf, 0xfb, 0x60 }; //byte[] bf3 = new byte[] { 0xed, 0xdf, 0xd8, 0x9e, 0x11, 0xce, 0x7b, 0xd5 }; //fish.Decode(bf1, nameLen, FISH_KEY); //fish.Decode(bf2, nameLen, FISH_KEY); //fish.Decode(bf3, nameLen, FISH_KEY); }