public Hall(string tmpName) { devHeads = new List<string>(); devHeads.Add("asokawu"); devHeads.Add("ddli"); devHeads.Add("jehutyhu"); devHeads.Add("zashchen"); devHeads.Add("orrischen"); devHeads.Add("johntan"); devHeads.Add("seekyao"); myName = tmpName; heads = new List<Texture2D>(); ranks = new List<int>(); scores = new List<int>(); BaseGame.ShowMouse = true; roomList = new Listbox("roomlist", new Vector2(50, 120), new Point(200, 350), Color.White, Color.Green); rankList = new Listbox("ranklist", new Vector2(300, 120), new Point(450, 350), Color.White, Color.Green); bgTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.BgContent, "bg")); rlTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, "roomlist")); riTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, "roominfo")); bgRect = new Rectangle(0, 0, 800, 600); btnRefresh = new TextButton("RefreshBtn", new Vector2(150, 480), "Refresh", 0, Color.Gold); btnCreate = new TextButton("CreateBtn", new Vector2(310, 480), "Create a new room", 0, Color.Gold); btnQuit = new TextButton("QuitBtn", new Vector2(450, 410), "Quit", 0, Color.Gold); btnEnter = new TextButton("EnterBtn", new Vector2(70, 480), "Enter", 0, Color.Gold); btnRank = new TextButton("RankBtn", new Vector2(650, 480), "Rank List", 0, Color.Gold); btnStart = new TextButton("StartBtn", new Vector2(550, 410), "Start", 0, Color.Gold); btnRefresh.OnClick += new EventHandler(btnRefresh_OnPress); btnCreate.OnClick += new EventHandler(btnCreate_OnPress); btnQuit.OnClick += new EventHandler(btnQuit_OnPress); btnEnter.OnClick += new EventHandler(btnEnter_OnPress); btnRank.OnClick += new EventHandler(btnRank_OnPress); btnStart.OnClick += new EventHandler(btnStart_OnPress); rankList.OnChangeSelection += new EventHandler(rankList_OnChangeSelection); roomList.OnChangeSelection += new EventHandler(roomList_OnChangeSelection); SocketMgr.OnReceivePkg += new SocketMgr.ReceivePkgEventHandler(OnReceivePack); headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 33; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bInRoom = false; bWaitEnter = false; bIsHost = false; bHasError = false; }
void btnEnter_OnPress(object sender, EventArgs e) { if (bWaitEnter || bInRoom) return; if (roomList.selectedIndex == -1) return; headSend = new stPkgHead(); //byte[] roomid; //roomid = SocketMgr.StructToBytes(roomList.MyIDs[roomList.selectedIndex]); Stream = new MemoryStream(); //Stream.Write(roomid, 0, 4); //head.dataSize = 4; headSend.dataSize = 0; headSend.iSytle = 31; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bWaitEnter = true; }
void btnRefresh_OnPress(object sender, EventArgs e) { headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 33; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); }
void btnStart_OnPress(object sender, EventArgs e) { headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 70; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bWaitEnter = true; }
void btnQuit_OnPress(object sender, EventArgs e) { headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 39; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bIsHost = false; bInRoom = false; headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 33; roomList.Clear(); SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); }
void OnReceivePack(stPkgHead head, byte[] data) { if (head.iSytle == 40) { head.iSytle = 40; } if (head.iSytle == 33) { //刷房间列表成功 string str; RoomInfo room; byte[] tmpData; roomList.Clear(); tmpData = new byte[head.dataSize]; for (int i = 0; i < head.dataSize; i += 36) { str = ""; //data.Read(roomBuffer, 0, 32); for (int k = 0; k < 36; ++k) { tmpData[k] = data[i + k]; } room = (RoomInfo)SocketMgr.BytesToStuct(tmpData, typeof(RoomInfo)); for (int j = 0; room.name[j] != '\0'; ++j) { str += room.name[j]; } roomList.AddItem(" " + str + " ( " + room.players + " / 2 )", room.id); } } else if (head.iSytle == 35) { //创建房间成功 bWaitEnter = false; headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 33; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bInRoom = true; headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 34; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); } else if (head.iSytle == 36) { //创建房间失败 bWaitEnter = false; bInRoom = false; bIsHost = false; } else if (head.iSytle == 37) { //加入房间成功 bWaitEnter = false; headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 33; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bInRoom = true; headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 34; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); } else if (head.iSytle == 38) { //加入房间失败 bWaitEnter = false; bInRoom = false; bIsHost = false; } else if (head.iSytle == 34) { //列举用户信息 string str; UserInfo player; byte[] tmpData; Monitor.Enter(heads); Monitor.Enter(ranks); Monitor.Enter(scores); heads.Clear(); ranks.Clear(); scores.Clear(); tmpData = new byte[head.dataSize]; bIsHost = false; string[] tmpNames = new string[6]; int playerNum = 0; for (int i = 0; i < head.dataSize; i += 56) { str = ""; //data.Read(roomBuffer, 0, 32); for (int k = 0; k < 56; ++k) { tmpData[k] = data[i + k]; } player = (UserInfo)SocketMgr.BytesToStuct(tmpData, typeof(UserInfo)); for (int j = 0; player.name[j] != '\0'; ++j) { str += player.name[j]; } if (str == myName && player.state == 1) bIsHost = true; tmpNames[playerNum] = str;//, Font font) ranks.Add(player.rank); scores.Add(player.score); Texture2D tex; if (devHeads.Contains(str)) { tex = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, str)); } else { tex = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, "head")); } if (tex == null) { throw new Exception(""); } heads.Add(tex); playerNum++; //roomList.AddItem("room 1" + " ( " + room.players + " / 6 )", room.id); } playerCount = playerNum; userNames = new string[playerNum]; for (int i = 0; i < playerNum; i++) { userNames[i] = tmpNames[i]; } Monitor.Exit(scores); Monitor.Exit(ranks); Monitor.Exit(heads); headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 33; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); } else if (head.iSytle == 70) { //开始游戏 bWaitEnter = false; if (bIsHost) GameManager.AddGameScreen(new StarwarLogic(0, userNames)); else { for (int i = 0; i < playerCount; i++) { if (userNames[i] == myName) GameManager.AddGameScreen(new StarwarLogic(i, userNames)); } } } else if (head.iSytle == 71) { bWaitEnter = false; } }
void btnCreate_OnPress(object sender, EventArgs e) { if (bWaitEnter) return; if (bInRoom) { headSend = new stPkgHead(); Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 39; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bIsHost = false; bInRoom = false; } headSend = new stPkgHead(); //head.iSytle = //包头类型还没初始化 Stream = new MemoryStream(); headSend.dataSize = 0; headSend.iSytle = 30; SocketMgr.SendCommonPackge(headSend, Stream); Stream.Close(); bWaitEnter = true; }
void OnReceivePack(stPkgHead head, MemoryStream data) { if (head.iSytle == 50) { RankInfo ri; string str; for (int i = 0; i < head.dataSize; i += 32) { str = ""; data.Read(rankBuffer, 0, 32); ri = (RankInfo)SocketMgr.BytesToStuct(rankBuffer, typeof(RankInfo)); for (int j = 0; ri.name[j] != '\0'; ++j) { str += ri.name[j]; } rankList.AddItem(ri.rank + " " + str + " " + ri.score); } } }
private void SendOverRankInfo() { stPkgHead head = new stPkgHead(); head.iSytle = 80; RankInfo[] info = new RankInfo[ships.Length]; byte[] buffer = new byte[ships.Length * RankInfo.TotolLength]; for (int i = 0; i < ships.Length; i++) { info[i].Score = ships[i].Score; info[i].Rank = 0; info[i].Name = new char[RankInfo.NameLength]; for (int j = 0; j < ships[i].PlayerName.Length; j++) { info[i].Name[j] = ships[i].PlayerName[j]; } byte[] temp = SocketMgr.StructToBytes(info[i]); temp.CopyTo(buffer, i * RankInfo.TotolLength); } MemoryStream stream = new MemoryStream(); stream.Write(buffer, 0, buffer.Length); head.dataSize = buffer.Length; SocketMgr.SendCommonPackge(head, stream); stream.Close(); }
/* private void heartTimer_Tick(Object obj, ElapsedEventArgs e) { stPkgHead head = new stPkgHead(); MemoryStream Stream = new MemoryStream(); head.dataSize = 0; head.iSytle = 0; SocketMgr.SendCommonPackge(head, Stream); Stream.Close(); } */ void OnReceivePack(stPkgHead head, Byte[] data) { if (wait == 0) return; if (head.iSytle == 11) { wait--; //heartTimer.Start(); GameManager.AddGameScreen(new Hall2(namebox.text)); } if (head.iSytle == 12) { wait--; namebox = new Textbox("namebox", new Vector2(320, 400), 150, "", false); passbox = new Textbox("passbox", new Vector2(320, 430), 150, "", false); passbox.bStar = true; namebox.maxLen = 20; passbox.maxLen = 20; SocketMgr.Close(); System.Windows.Forms.MessageBox.Show("用户名密码错误或重登陆!"); } else { bHasError = true; } }
void btnLogin_OnPress(object sender, EventArgs e) { if (wait != 0) return; LoginData data; data.Name = new char[21]; char[] temp = namebox.text.ToCharArray(); for (int i = 0; i < temp.Length; i++) { data.Name[i] = temp[i]; } data.Password = new char[21]; temp = passbox.text.ToCharArray(); for (int i = 0; i < temp.Length; i++) { data.Password[i] = temp[i]; } stPkgHead head = new stPkgHead(); MemoryStream Stream = new MemoryStream(); Stream.Write(SocketMgr.StructToBytes(data), 0, LoginData.size); head.dataSize = (int)Stream.Length; head.iSytle = 10; SocketMgr.ConnectToServer(); SocketMgr.StartReceiveThread(); SocketMgr.SendCommonPackge(head, Stream); Stream.Close(); wait++; }
public Rank() { devHeads = new List<string>(); devHeads.Add("asokawu"); devHeads.Add("ddli"); devHeads.Add("jehutyhu"); devHeads.Add("zashchen"); devHeads.Add("orrischen"); devHeads.Add("johntan"); devHeads.Add("seekyao"); BaseGame.ShowMouse = true; rankPos = new Vector2(50, 120); roomList = new Listbox("roomlist", new Vector2(550, 120), new Point(200, 150), Color.White, Color.Black); rankList = new Listbox("ranklist", rankPos, new Point(450, 350), Color.White, Color.Green); bgTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.BgContent, "bg")); rkTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, "ranklist")); piTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, "yourinfo")); bgRect = new Rectangle(0, 0, 800, 600); btnOK = new TextButton("OKBtn", new Vector2(550, 370), "OK", 0, Color.Gold); btnOK.OnClick += new EventHandler(btnOK_OnPress); rankList.OnChangeSelection += new EventHandler(rankList_OnChangeSelection); roomList.OnChangeSelection += new EventHandler(roomList_OnChangeSelection); SocketMgr.OnReceivePkg += new SocketMgr.ReceivePkgEventHandler(OnReceivePack); stPkgHead head = new stPkgHead(); //head.iSytle = //包头类型还没初始化 byte[] rankcode = new byte[4]; rankcode[0] = 1; rankcode[1] = 0; rankcode[2] = 0; rankcode[3] = 0; MemoryStream Stream = new MemoryStream(); Stream.Write(rankcode, 0, 4); head.dataSize = 4; head.iSytle = 50; SocketMgr.SendCommonPackge(head, Stream); Stream.Close(); stPkgHead head2 = new stPkgHead(); MemoryStream Stream2 = new MemoryStream(); head2.dataSize = 0; head2.iSytle = 40; SocketMgr.SendCommonPackge(head2, Stream2); Stream2.Close(); head = new stPkgHead(); //head.iSytle = //包头类型还没初始化 rankcode = new byte[4]; rankcode[0] = 1; rankcode[1] = 0; rankcode[2] = 0; rankcode[3] = 0; Stream = new MemoryStream(); Stream.Write(rankcode, 0, 4); head.dataSize = 4; head.iSytle = 50; SocketMgr.SendCommonPackge(head, Stream); Stream.Close(); bOK = false; rankItems = new List<RankIF>(); bLoaded = false; // 连接到服务器 //SocketMgr.ConnectToServer(); }
void OnReceivePack(stPkgHead head, byte[] data) { byte[] tmpData; tmpData = new Byte[head.dataSize]; if (head.iSytle == 50) { RankInfo ri; RankIF tmpItem; string str; rankItems.Clear(); for (int i = 0; i < head.dataSize; i += 32) { str = ""; for (int k = 0; k < 32; k++) { tmpData[k] = data[i + k]; } ri = (RankInfo)SocketMgr.BytesToStuct(tmpData, typeof(RankInfo)); for (int j = 0; ri.name[j] != '\0'; ++j) { str += ri.name[j]; } tmpItem.rank = ri.rank; tmpItem.score = ri.score; tmpItem.name = str; rankItems.Add(tmpItem); } } else if (head.iSytle == 40) { UserInfo player; string str; str = ""; //data.Read(roomBuffer, 0, 32); player = (UserInfo)SocketMgr.BytesToStuct(data, typeof(UserInfo)); for (int j = 0; player.name[j] != '\0'; ++j) { str += player.name[j]; } myInfo.name = str; myInfo.rank = player.rank; myInfo.score = player.score; if (devHeads.Contains(myInfo.name)) hdTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, myInfo.name)); else hdTexture = BaseGame.ContentMgr.Load<Texture2D>(Path.Combine(Directories.UIContent, "head")); bLoaded = true; } }
static public bool Close() { if (client != null && client.Connected) { stPkgHead pkg = new stPkgHead(); pkg.iSytle = (int)PACKAGE_SYTLE.EXIT; client.GetStream().Write(StructToBytes(pkg), 0, stPkgHead.Size); thread = null; } return true; }
static public void SendCommonPackge(stPkgHead head, MemoryStream data) { if (head.dataSize != 0 && (data == null || head.dataSize != data.Length)) throw new Exception("发送的包头有误"); try { if (client != null && client.Connected) { Stream netStream = client.GetStream(); if (netStream.CanWrite) { netStream.Write(StructToBytes(head), 0, stPkgHead.Size); if (data != null) data.WriteTo(netStream); Console.WriteLine("Send " + head.iSytle + " " + head.dataSize); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); return; } }
static public void SendGameLogicPackge(SyncCashe cash) { try { if (client.Connected) { Stream netStream = client.GetStream(); if (netStream.CanWrite) { // Head stPkgHead head = new stPkgHead(); head.iSytle = (int)PACKAGE_SYTLE.DATA; MemoryStream MStream = new MemoryStream(); //StreamWriter SW = new StreamWriter(MStream); BinaryWriter BW = new BinaryWriter(MStream); // 状态同步数据 BW.Write(cash.ObjStaInfoList.Count); //SW.WriteLine(cash.ObjStaInfoList.Count); foreach (ObjStatusSyncInfo info in cash.ObjStaInfoList) { //SW.WriteLine(info.objMgPath); BW.Write(info.objMgPath); BW.Write(info.statusName); BW.Write(info.values.Length); foreach (object obj in info.values) { WriteObjToStream(BW, obj); } } // 事件同步数据 BW.Write(cash.ObjEventInfoList.Count); foreach (ObjEventSyncInfo info in cash.ObjEventInfoList) { BW.Write(info.objMgPath); BW.Write(info.EventName); BW.Write(info.values.Length); foreach (object obj in info.values) { WriteObjToStream(BW, obj); } } // 物体管理同步数据 BW.Write(cash.ObjMgInfoList.Count); foreach (ObjMgSyncInfo info in cash.ObjMgInfoList) { BW.Write(info.objPath); BW.Write(info.objMgKind); BW.Write(info.objType); BW.Write(info.args.Length); foreach (object obj in info.args) { WriteObjToStream(BW, obj); } } // 用户自定义数据 BW.Write(cash.UserDefineInfoList.Count); foreach (UserDefineInfo info in cash.UserDefineInfoList) { BW.Write(info.infoName); BW.Write(info.infoID); BW.Write(info.args.Length); foreach (object obj in info.args) { WriteObjToStream(BW, obj); } } BW.Flush(); int dataLength = (int)MStream.Length; //int dataLength = (int)SW.BaseStream.Length; byte[] temp = new byte[dataLength]; MStream.Position = 0; MStream.Read(temp, 0, dataLength); head.dataSize = temp.Length; netStream.Write(StructToBytes(head), 0, stPkgHead.Size); netStream.Write(temp, 0, head.dataSize); // 释放资源 BW.Close(); MStream.Close(); Console.WriteLine("Send " + head.iSytle + " " + head.dataSize); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); return; } }