示例#1
0
文件: CClient.cs 项目: hnjm/CChat2
        private static void GotMessage(object peer)
        {
            var client = ((CClient)peer);
            var msg    = client.ReadMessage();

            switch (msg.MessageType)
            {
            case NetIncomingMessageType.DiscoveryResponse:
                string name = msg.ReadString();
                var    s    = new FoundServer()
                {
                    EndPoint = msg.SenderEndPoint, Name = name
                };

                if (client.callback != null)
                {
                    client.callback.Invoke(s);
                }
                break;

            case NetIncomingMessageType.StatusChanged:
                NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                string statusText          = msg.ReadString();
                Main.Log("[STATUS UPDATE] " + status + " - " + statusText);
                if (status == NetConnectionStatus.Connected)
                {
                    Main.SetStatus("Connected to " + msg.SenderEndPoint.Address);
                    Main.Log("Connected!");
                    Net.UponClientConnected();
                }
                else if (status == NetConnectionStatus.Disconnected)
                {
                    Main.Log("Disconnected from server. Reason: " + statusText);
                    Net.UponClientDisconnected();
                    MessageBox.Show("Disconnected from server: " + statusText, "Disconnected", MessageBoxButtons.OK);
                }
                else
                {
                    Main.SetStatus(status.ToString());
                }
                break;

            case NetIncomingMessageType.Data:
                var type = (DataType)msg.ReadByte();
                ProcessData(type, msg, client);
                break;

            default:
                Main.Log("[CLIENT UNHANDLED] Type: " + msg.MessageType);
                break;
            }

            client.Recycle(msg);
        }
示例#2
0
 private void ServerDiscovered(FoundServer s)
 {
     ServerList.Items.Add(s);
     Log("Discovered server '" + s.Name + "' @ " + s.EndPoint);
 }