示例#1
0
        //first connect to the server
        private void FirstContact(SocketState state)
        {
            state.callMe = ReceiveStartup;
            NController.Send(socket, name + "\n");

            NController.GetData(state);
        }
示例#2
0
        /// <summary>
        /// update method use to send info to the client
        /// </summary>
        public static void update()
        {
            // wait until enough time
            while (watch.ElapsedMilliseconds < MSperFrame)
            {
            }
            watch.Restart();

            // use to append message
            StringBuilder sb = new StringBuilder();

            lock (theworld)
            {
                theworld.update();
                foreach (star s in theworld.getStar().Values)
                {
                    sb.Append(s.ToString() + '\n');
                }
                foreach (Ship s in theworld.getShip().Values)
                {
                    sb.Append(s.ToString() + "\n");
                }
                foreach (projectile s in theworld.getProj().Values)
                {
                    sb.Append(s.ToString() + "\n");
                }
                // clean up the unconnected ship and died stuffs
                theworld.cleanup();
            }
            //check all client statuses
            lock (list)
            {
                foreach (SocketState ss in list.ToArray())
                {
                    if (ss.theSocket.Connected == true)
                    {
                        NController.Send(ss.theSocket, sb.ToString());
                    }
                    // deal with the unconnected clients
                    else
                    {
                        lock (theworld)
                        {
                            theworld.addLostID(ss.uid);
                            theworld.getShip()[ss.uid].setLost();
                        }
                        list.Remove(ss);
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// call back, if the client connects get the user name then generate a new ship, finally send back startup info,and add the client to the list
        /// </summary>

        public static void ReceiveName(SocketState ss)
        {
            String[] parts = ss.sb.ToString().Split('\n');
            String   name  = parts[0];
            Ship     ship;

            lock (theworld)
            {
                ship   = theworld.generateShip(name);
                ss.uid = ship.getID();
            }

            lock (list)
            {
                list.AddLast(ss);
                Console.WriteLine("welcome player: " + ship.getName());
            }
            NController.Send(ss.theSocket, ship.getID() + "\n" + UniverSize + "\n");
            ss.sb.Clear();
            ss.callMe = userInput;
            NController.GetData(ss);
        }
示例#4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="data"></param>
 public void sendData(String data)
 {
     NController.Send(socket, data + "\n");
 }