private void Client_Disconnected(object sender, lib.Net.Sockets.SocketDisconnectedEventArgs args) { Dispatcher.BeginInvoke(new Action(() => { btn_prepare.IsEnabled = false; btn_connect.IsEnabled = true; })); MessageBox.Show("连接关闭"); client.Dispose(); client = null; }
private void Btn_connect_Click(object sender, RoutedEventArgs e) { try { if (client == null) { client = new lib.Net.Sockets.SocketClient(); client.MessageReceived += Client_MessageReceived; client.Disconnected += Client_Disconnected; } client.ConnectAsync(tb_ipaddr.Text, int.Parse(tb_port.Text)).Wait(); btn_connect.IsEnabled = false; btn_prepare.IsEnabled = true; StartPingTest(); } catch (Exception ex) { MessageBox.Show("无法连接"); } }
private void Client_MessageReceived(lib.Net.Sockets.SocketClient socket, lib.Net.Sockets.SocketMessageEventArgs args) { foreach (var msg in args.Messages) { int offset = 0; #region DataMessage if (msg.MsgType == lib.Net.Sockets.MessageType.Data) { switch ((lib.DataType)msg.DataMessage.Data[0]) { #region PlayerName case lib.DataType.SetName: Dispatcher.BeginInvoke(new Action(() => { byte tindex = msg.DataMessage.Data[1]; string name = Encoding.UTF8.GetString(msg.DataMessage.Data, 2, (int)msg.DataMessage.Length - 2); snakeName[tindex] = name; if (tindex != playerIndex) { (((sv_playersLayer.Content as StackPanel).Children[tindex] as Grid).Children[0] as TextBlock).Text = name; (grid_gameInfo.Children[tindex] as TextBlock).Text = string.Format("{0}: {1}", name, lengthes[tindex]); } })); break; #endregion #region Inventory case lib.DataType.UseInventory: lib.MapType inventory = (lib.MapType)msg.DataMessage.Data[1]; switch (inventory) { case lib.MapType.BlindFruit: Dispatcher.BeginInvoke(new Action(() => { ShowBlind(); })); break; } break; case lib.DataType.AddInventory: int toolindex = 0; byte amount = msg.DataMessage.Data[2]; while (toolindex < 5 && tools[toolindex].Key != lib.MapType.Empty && tools[toolindex].Key != (lib.MapType)msg.DataMessage.Data[1]) { toolindex++; } if (toolindex == 5) { return; } if (amount == 0) { tools.RemoveAt(toolindex); tools.Add(new KeyValuePair <lib.MapType, byte>(lib.MapType.Empty, 0)); } else { tools[toolindex] = new KeyValuePair <lib.MapType, byte>((lib.MapType)msg.DataMessage.Data[1], amount); } Dispatcher.BeginInvoke(new Action(() => { UpdateInventory(); })); break; #endregion #region HardChanged case lib.DataType.HardChanged: byte toplayer = msg.DataMessage.Data[1]; bool isHard = msg.DataMessage.Data[2] == 1; this.snakeHard[toplayer] = isHard; break; #endregion #region Disconnected case lib.DataType.PlayerDisconnected: this.Dispatcher.BeginInvoke(new Action(() => { byte index = msg.DataMessage.Data[1]; snakeDied[index] = true; TextBlock infotb = grid_gameInfo.Children[index] as TextBlock; infotb.Text = string.Format("{0}: 在火星", snakeName[index]); })); break; #endregion #region GameEnd case lib.DataType.GameEnd: this.Dispatcher.BeginInvoke(new Action(() => { GameEnd(msg.DataMessage.Data[1]); })); break; #endregion #region SnakeDied case lib.DataType.SnakeDied: this.Dispatcher.BeginInvoke(new Action(() => { ShowSnakeDied(msg.DataMessage.Data); })); break; #endregion #region PlayerIndex case lib.DataType.PlayerIndex: playerIndex = msg.DataMessage.Data[1]; break; #endregion #region PingTest case lib.DataType.PingTest: int val = BitConverter.ToInt32(msg.DataMessage.Data, 1); int sv = DateTime.Now.Second * 1000 + DateTime.Now.Millisecond; if (sv < val) { sv += 60 * 1000; } this.Dispatcher.BeginInvoke(new Action(() => { this.Title = string.Format("ping: {0}", sv - val); })); break; #endregion #region PlayerStatus case lib.DataType.PlayerStatus: this.Dispatcher.BeginInvoke(new Action(() => { ShowPlayerStatus(msg.DataMessage.Data); })); break; #endregion #region GameStart case lib.DataType.GameStart: this.Dispatcher.BeginInvoke(new Action(() => { StartGame(); })); break; #endregion #region StageSizeQuery case lib.DataType.StageSizeQuery: stageHeight = BitConverter.ToInt32(msg.DataMessage.Data, 1); stageWidth = BitConverter.ToInt32(msg.DataMessage.Data, 5); break; #endregion #region MapData case lib.DataType.MapData: offset = 1; int len = BitConverter.ToInt32(msg.DataMessage.Data, offset); offset += 4; short x, y; Dictionary <lib.Coord, lib.MapType> dic = new Dictionary <lib.Coord, lib.MapType>(); for (int i = 0; i < len; i++) { x = BitConverter.ToInt16(msg.DataMessage.Data, offset); offset += 2; y = BitConverter.ToInt16(msg.DataMessage.Data, offset); offset += 2; dic.Add(new lib.Coord(x, y), (lib.MapType)msg.DataMessage.Data[offset]); offset += 1; } for (int i = 0; i < playerCount; i++) { lengthes[i] = BitConverter.ToInt32(msg.DataMessage.Data, offset); offset += 4; } this.Dispatcher.BeginInvoke(new Action(() => { EditMap(dic); })); break; #endregion } } #endregion #region ObjectMessage else if (msg.MsgType == lib.Net.Sockets.MessageType.Object) { object obj = msg.ObjectMessage.Object; if (obj.GetType() == typeof(lib.ServerNotification)) { Dispatcher.BeginInvoke(new Action(() => { ShowNotification(obj as lib.ServerNotification); })); } } #endregion } }