Exemple #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="packet"></param>
 /// <param name="client"></param>
 /// <returns></returns>
 public bool SendMessage(NetPacket packet, NetPlayer client)
 {
     return(SendMessage(packet, client.EndPoint));
 }
Exemple #2
0
        /// <summary>
        /// Update in server mode
        /// </summary>
        /// <param name="elapsed">Elapsed time since last update</param>
        public void Update(TimeSpan elapsed)
        {
            EndPoint sender = new IPEndPoint(IPAddress.Any, 0);

            //NetClient client = null;

            #region Receive datas

            // While data available
            while (Socket.Available != 0)
            {
                // Collect data
                int size = Socket.ReceiveFrom(Buffer, ref sender);
                Packet.SetData(Buffer, size);


                // Process message
                switch (Packet.Type)
                {
                    #region Control Packet
                case PacketType.ControlPacket:
                {
                    switch ((RequestType)Packet.ReadByte())
                    {
                    //
                    // Connection request
                    case RequestType.ConnectionRequest:
                    {
                        NetPacket packet = new NetPacket();
                        packet.Type = PacketType.ControlPacket;


                        if (ClientCount >= ServerConfig.MaxClient)
                        {
                            // Too many clients
                            packet.Write((byte)RequestType.ConnectionReject);
                            packet.Write("Server full.");
                            Trace.WriteLine("Server : Too many connection request, discarding client.");
                        }
                        else
                        {
                            // Add client
                            NetPlayer newclient = new NetPlayer(sender as IPEndPoint);
                            Clients.Add(newclient);

                            // Accept connection
                            packet.Write((byte)RequestType.ConnectionAccept);
                            Trace.WriteLine("Server : Adding new client.");


                            //if (OnPlayerJoin != null)
                            //    OnPlayerJoin(newclient);
                        }


                        // Send the response
                        SendMessage(packet, sender as IPEndPoint);


                        packet.Reset();
                        packet.Type = PacketType.UserPacket;
                        packet.Write("Toto 1");
                        packet.Write((byte)2);
                        packet.Write("Toto 2");
                        packet.Write(3.5f);
                        packet.Write("Toto 3");
                        SendMessage(packet, sender as IPEndPoint);
                    }
                    break;


                    //
                    // Ask for server informations
                    case RequestType.ServerInfo:
                    {
                    }
                    break;


                    //
                    // Request informations about a player
                    case RequestType.PlayerInfoRequest:
                    {
                    }
                    break;


                    default:
                    {
                        Trace.WriteLine("Server : Wrong control packet type received !");
                        continue;
                    }
                    }
                }
                break;
                    #endregion



                    #region User packet
                case PacketType.UserPacket:
                {
                    //if (OnMessage != null)
                    //    OnMessage(Packet);
                }
                break;
                    #endregion
                }
            }
            #endregion


            #region Process clients


            #endregion
        }