//退出按钮点击事件 private void buttonClose_Click(object sender, EventArgs e) { //如果还未建立连接 if (socketClient == null) { this.Close(); return; } //如果已经建立连接 try { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(userName, 0, userName.Length) + "退出了聊天室"); commum.Message msg = new commum.Message(); msg.message = arrClientSendMsg; msg.ipAddress = userName; arrClientSendMsg = PackCodec.Serialize <commum.Message>(msg); SendMsg(arrClientSendMsg); socketClient.Shutdown(SocketShutdown.Both); this.Close(); } catch (SocketException) { txtMsg.AppendText("当前未连接\r\n"); this.Close(); return; } catch (NullReferenceException) { return; } }
private byte[] CreateData(int typeId, IExtensible pbuf) { byte[] pbdata = PackCodec.Serialize(pbuf); ByteBuffer buff = new ByteBuffer(); buff.WriteInt(typeId); buff.WriteBytes(pbdata); return(buff.ToBytes()); }
//发送按钮 private void button1_Click(object sender, EventArgs e) { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(txtCMsg.Text.Trim()); commum.Message msg = new commum.Message(); msg.message = arrClientSendMsg; msg.ipAddress = userName; arrClientSendMsg = PackCodec.Serialize <commum.Message>(msg); SendMsg(arrClientSendMsg); }
public byte[] Send(Google.Protobuf.IMessage pbData) { NetWriter writer = NetWriter.Instance; SetActionHead(writer, pbData); writer.SetBodyData(PackCodec.Serialize(pbData)); return(writer.PostData()); }
private void buttonLogin_Click(object sender, EventArgs e) { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(userName, 0, userName.Length) + "进入了聊天室"); commum.Message msg = new commum.Message(); msg.message = arrClientSendMsg; msg.ipAddress = userName; arrClientSendMsg = PackCodec.Serialize <commum.Message>(msg); SendMsg(arrClientSendMsg); btnSend.Enabled = true; buttonLogin.Enabled = false; }
protected virtual void SetActionHead(NetWriter writer, Google.Protobuf.IMessage pbData) { //writer.writeInt32("actionId", ActionId); byte[] bodyBuffer = PackCodec.Serialize(pbData); ByteBuffer headBuffer = new ByteBuffer(); headBuffer.WriteInt(this.ActionId); headBuffer.WriteInt(bodyBuffer.Length); headBuffer.WriteInt(Head.MsgId); headBuffer.WriteInt(0); //body_check; headBuffer.WriteInt(0); //head_check; writer.SetHeadBuffer(headBuffer.ToBytes()); }
// Use this for initialization void Start() { // ObjectToFile(); // FileToObject(); CSLoginInfo mLoginInfo = new CSLoginInfo(); mLoginInfo.UserName = "******"; mLoginInfo.Password = "******"; CSLoginReq mReq = new CSLoginReq(); mReq.LoginInfo = mLoginInfo; byte[] result = PackCodec.Serialize(mReq); CSLoginReq req = PackCodec.Deserialize <CSLoginReq>(result); Debug.Log("username="******";password=" + req.LoginInfo.Password); }
private void TcpServerMsgCallback(byte[] data) { Debug.Log("收到消息回复 TcpServerMsgCallback"); ThreadCrossHelper.Instance.ExcutionFunc(() => { string msg = null; if (IsOpenNewMethod) { UserData user = PackCodec.Deserialize <UserData>(data); BinaryFormatter bFormatter = new BinaryFormatter(); using (MemoryStream s = new MemoryStream()) { byte[] bdata = user.data; s.Write(bdata, 0, bdata.Length); s.Position = 0; Test t = (Test)bFormatter.Deserialize(s); msg = string.Format("user2-> id:{0}, name:{1}, level:{2}, data:{3}", user.id, user.name, user.level, t.msg); } } else { CSLoginInfo mLoginInfo = PackCodec.Deserialize <CSLoginInfo>(data); msg = "UserName = "******", Password = "******"I get message"; UserData user1 = new UserData(); user1.id = 1; user1.name = "User1"; user1.level = 10; BinaryFormatter bFormatter = new BinaryFormatter(); using (MemoryStream byteData = new MemoryStream()) { bFormatter.Serialize(byteData, test); byte[] value = byteData.GetBuffer(); user1.data = value; } //user1.data = Encoding.UTF8.GetBytes("I get your message"); //序列化 using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize <UserData>(ms, user1); ms.Position = 0; int length = (int)ms.Length; buff = ms.ToArray(); } } else { CSLoginInfo Info = new CSLoginInfo(); Info.UserName = "******"; Info.Password = "******"; CSLoginReq mReq = new CSLoginReq(); mReq.LoginInfo = Info; buff = PackCodec.Serialize(mReq); } TcpSocketMgr.GetInstance().GetSendAssist().SendMsg(buff); }
private void SendMsgOnClick() { if (!IsClient) { byte[] buff = null; if (IsOpenNewMethod) { Test test = new Test(); test.msg = "It's a Server bytes array"; UserData user1 = new UserData(); user1.id = 1; user1.name = "User1"; user1.level = 10; BinaryFormatter bFormatter = new BinaryFormatter(); using (MemoryStream byteData = new MemoryStream()) { bFormatter.Serialize(byteData, test); byte[] value = byteData.GetBuffer(); user1.data = value; } //user1.data = Encoding.UTF8.GetBytes("It's a Server bytes array"); //序列化 using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize <UserData>(ms, user1); ms.Position = 0; int length = (int)ms.Length; buff = new byte[length]; ms.Read(buff, 0, length); } } else { CSLoginInfo Info = new CSLoginInfo(); Info.UserName = "******"; Info.Password = "******"; CSLoginReq mReq = new CSLoginReq(); mReq.LoginInfo = Info; buff = PackCodec.Serialize(mReq); } TcpSocketMgr.GetInstance().GetSendAssist().SendMsg(buff); } else { byte[] buff = null; if (IsOpenNewMethod) { Test test = new Test(); //test.msg = "It's a Client bytes array"; test.msg = "It's a Client bytes array"; UserData user1 = new UserData(); user1.id = 1; user1.name = "User1"; user1.level = 10; //user1.data = Encoding.UTF8.GetBytes("It's a Client bytes array"); byte[] stringData = Encoding.UTF8.GetBytes("It's a Client bytes array"); Debug.Log("byte string length:" + stringData.Length); BinaryFormatter bFormatter = new BinaryFormatter(); using (MemoryStream byteData = new MemoryStream()) { Serializer.Serialize <Test>(byteData, test); user1.data = byteData.ToArray(); Debug.Log("data length:" + user1.data.Length); } //序列化 using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize <UserData>(ms, user1); ms.Position = 0; int length = (int)ms.Length; buff = ms.ToArray(); } } else { CSLoginInfo Info = new CSLoginInfo(); Info.UserName = "******"; Info.Password = "******"; CSLoginReq mReq = new CSLoginReq(); mReq.LoginInfo = Info; buff = PackCodec.Serialize(mReq); } TcpSocketClientMgr.GetInstance().GetSendAssist().SendMsg(buff); } }
public override void OnInspectorGUI() { MyMap mme = target as MyMap; s_transform = mme.gameObject.transform; serializedObject.Update(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Map Width is: ", EditorStyles.boldLabel); float savedLabelWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 80; EditorGUI.indentLevel += 2; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(serializedObject.FindProperty("Width")); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); mme.RecalculateMapBounds(); } EditorGUI.indentLevel -= 2; EditorGUIUtility.labelWidth = savedLabelWidth; EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Map Height is: ", EditorStyles.boldLabel); savedLabelWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 80; EditorGUI.indentLevel += 2; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(serializedObject.FindProperty("Height")); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); mme.RecalculateMapBounds(); } EditorGUI.indentLevel -= 2; EditorGUIUtility.labelWidth = savedLabelWidth; EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Map Cell size: ", EditorStyles.boldLabel); savedLabelWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 100; EditorGUI.indentLevel += 2; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(serializedObject.FindProperty("ShowGrid")); EditorGUILayout.PropertyField(serializedObject.FindProperty("CellSize")); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); mme.RecalculateMapBounds(); } EditorGUILayout.EndVertical(); if (mme.CurrentSelectedCell != null && mme.CurrentSelectedCell.CellObj != null) { EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Selected Map Cell " + mme.CurrentSelectedCell.CellObj.name, EditorStyles.boldLabel); EditorGUI.BeginChangeCheck(); SerializedProperty cellProperty = serializedObject.FindProperty("CurrentSelectedCell"); SerializedProperty statusProperty = cellProperty.FindPropertyRelative("Status"); int index = (int)mme.CurrentSelectedCell.Status; mme.CurrentSelectedCell.Status = (MapCellStatus)EditorGUILayout.Popup("格子状态:", index, Enum.GetNames(typeof(MapCellStatus))); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); mme.UpdateMapCells(); } EditorGUILayout.EndVertical(); } EditorGUILayout.Separator(); EditorGUILayout.Separator(); EditorGUILayout.Separator(); if (mme.Width == 0 || mme.Height == 0) { EditorGUILayout.BeginVertical(); EditorGUI.indentLevel -= 2; EditorGUILayout.LabelField("Load Map Proto", EditorStyles.boldLabel); if (GUILayout.Button("Load Map Proto")) { if (mme.Width == 0 || mme.Height == 0) { string path = EditorUtility.OpenFilePanel("choose map data", "", "bytes"); if (path.Length != 0) { FileStream fs = new FileStream(path, FileMode.Open); long size = fs.Length; byte[] array = new byte[size]; fs.Read(array, 0, array.Length); fs.Close(); MapEditorData mapData = MapEditorData.Parser.ParseFrom(array); float cellW = mapData.CellWidth; float cellH = mapData.CellHeight; mme.CellSize = new Vector2(cellW, cellH); mme.Width = mapData.Width; mme.Height = mapData.Height; mme.RecalculateMapBounds(); foreach (MapEditorCellData cell in mapData.MapCells) { mme.m_mapCells[cell.X, cell.Y].Status = cell.Status; } mme.UpdateMapCells(); SceneView.RepaintAll(); } } } EditorGUILayout.EndVertical(); } else { EditorGUILayout.BeginVertical(); EditorGUI.indentLevel -= 2; EditorGUILayout.LabelField("Export to proto", EditorStyles.boldLabel); if (GUILayout.Button("Export")) { if (mme.Width == 0 || mme.Height == 0) { Debug.Log("the map is empty!!!!"); } else { MapEditorData mapEditorData = new MapEditorData(); mapEditorData.Width = mme.Width; mapEditorData.Height = mme.Height; mapEditorData.CellWidth = mme.CellSize.x; mapEditorData.CellHeight = mme.CellSize.y; for (int i = 0; i < mme.Width; i++) { for (int j = 0; j < mme.Height; j++) { MapEditorCellData cellData = new MapEditorCellData(); cellData.X = mme.m_mapCells[i, j].X; cellData.Y = mme.m_mapCells[i, j].Y; cellData.Status = mme.m_mapCells[i, j].Status; mapEditorData.MapCells.Add(cellData); } } byte[] mapArray = PackCodec.Serialize(mapEditorData); string path = EditorUtility.SaveFilePanel("save map data", "", "mapData.bytes", "bytes"); if (path.Length != 0) { FileStream fs = new FileStream(path, FileMode.Create); fs.Write(mapArray, 0, mapArray.Length); fs.Close(); } } } EditorGUILayout.EndVertical(); } }