示例#1
0
        public void ListenLoop()
        {
            try
            {
                listener.Start();
                while (true) //an always listening server!!! UNLIMITED PLAYERS!! MWAHAHAHA
                {
                    TcpClient inClient = listener.AcceptTcpClient();
                    inClient.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

                    var gameClient = new GameClient()
                    {
                        Client = inClient,
                        ClientId = playerIndex++
                    };

                    clients.Add(gameClient);
                    Thread inClientRecvThread = new Thread(new ParameterizedThreadStart(ClientRecvLoop));
                    inClientRecvThread.IsBackground = true;
                    clientThreads.Add(inClientRecvThread);

                    inClientRecvThread.Start(gameClient);
                }
            }
            catch (Exception e)
            {
                if (OnNetworkError != null)
                {
                    OnNetworkError(e, null);
                }
            }
            finally
            {
                try
                {
                    listener.Stop();
                }
                catch
                { }
                listener = null;
            }
        }
示例#2
0
        private bool InitialHandshake(GameClient gClient)
        {
            //network protocol hands have schecten however we need some handshaking of our own for establishing player numbers, etc.
            BinaryFormatter formatter = new BinaryFormatter();
            var stream = gClient.Client.GetStream();

            InitialNetworkData clientData = (InitialNetworkData)formatter.Deserialize(stream);

            var response = MechanicSingleton.SetupClientData(clientData);

            formatter.Serialize(stream, response);

            foreach (var obj in worldRef.GetWorldForNetwork())
            {

                formatter.Serialize(stream,
                    new NetworkCommandObject()
                    {
                        ClientId = gClient.ClientId,
                        Command = obj,
                    });

            }

            return true;
        }
示例#3
0
        /// <summary>
        /// initiates connection procedures to a specified server
        /// </summary>
        /// <param name="ip">the ip address of the target machine</param>
        public void ConnectToGame(string ip)
        {
            if (thisClient.Client != null)
            {
                return;
            }

            thisClient = new GameClient()
            {
                Client = new TcpClient(),
                ClientId = -1 //this is the pending handshake state
            };

            try
            {
                thisClient.Client.Connect(ip, PORT);

                var valid = InitialHandshake();

                if (!valid)
                {
                    DisconnectFromGame();
                    throw new Exception("handshaking failed, he probably had a knife up his sleeve");
                }

                sendThread.Start();
            }
            catch(Exception e)
            {
                if (OnNetworkError != null)
                {
                    OnNetworkError(e, null);
                }
            }
        }