/// <summary>
        /// Inform the network to begin shutdown.
        /// </summary>
        public override void Shutdown()
        {
            // Check if transfer-buffer exists
            if (_transferBuffer != null)
            {
                // Check if transfer-buffer has already been shutdown
                if (!_transferBuffer.IsShutdown)
                {
                    // Queue a bye-message for send
                    _transferBuffer.Enqueue(new ByePacket(), false);

                    // Try to flush any pending send-data
                    _transferBuffer.TryToSendData();

                    // Shutdown transfer buffer
                    _transferBuffer.Shutdown();
                }
                _transferBuffer = null;
            }

            // Clear current network
            Current = null;
        }
        /// <summary>
        /// Network client constructor.
        /// </summary>
        /// <param name="address">Address of the host.</param>
        /// <param name="port">Port number on which the host is listening for new clients.</param>
        public NetworkClient(string address, ushort port = DEFAULT_PORT)
        {
            // Get host address
            //IPAddress serverIP = Dns.GetHostEntry(address).AddressList[0];
            IPAddress serverIP = Dns.GetHostEntry((address.ToLower() == "localhost") ? Dns.GetHostName() : address).AddressList[0];

            // Get host end-point (needed for connection)
            IPEndPoint endPoint = new IPEndPoint(serverIP, port);

            // Create socket
            Socket socket = new Socket(serverIP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Connect to host
            socket.Connect(endPoint);

            // Create packet transfer buffer
            _transferBuffer = new PacketTransferBuffer(socket);

            // Inform client server of client's name
            if ((Player.LocalPlayerName != null) && (Player.LocalPlayerName != ""))
            {
                Send(new PlayerNamePacket(null, Player.LocalPlayerName));
            }
        }
Example #3
0
 /// <summary>
 /// Constructor for the server interal client data.
 /// </summary>
 /// <param name="socket">The socket which connects to the client.</param>
 public InternalClient(Socket socket)
 {
     _transferBuffer = new PacketTransferBuffer(socket);
     _player         = Player.AllocatePlayer();
 }