Exemplo n.º 1
0
		/// <summary>
		/// Constructor for a server instance
		/// </summary>
		public NetServer(NetAppConfiguration config, NetLog log)
		{
			NetBase.CurrentContext = this;
			InitBase(config, log);
			Connections = new NetConnection[config.MaximumConnections];
			m_connectionsLookUpTable = new Dictionary<int, NetConnection>();
		}
Exemplo n.º 2
0
 /// <summary>
 /// Constructor for a server instance
 /// </summary>
 public NetServer(NetAppConfiguration config, NetLog log)
 {
     NetBase.CurrentContext = this;
     InitBase(config, log);
     Connections = new NetConnection[config.MaximumConnections];
     m_connectionsLookUpTable = new Dictionary <int, NetConnection>();
 }
Exemplo n.º 3
0
        MMOServer(int port)
        {
            m_plugin = new Plugin(Environment.CurrentDirectory);

            Init();
            NetAppConfiguration config = new NetAppConfiguration("MMO Mahjong", port);
            config.MaximumConnections = 128;
            config.Port = port;
            config.ServerName = Environment.MachineName + " server";

            m_log = new NetLog();
            m_log.IgnoreTypes = NetLogEntryTypes.None;
            m_log.LogEvent += new EventHandler<NetLogEventArgs>(OnLogEvent);

            Server = new NetServer(config, m_log);
            Server.StatusChanged += new EventHandler<NetStatusEventArgs>(OnStatusChange);

            Server.ConnectionRequest += new EventHandler<NetConnectRequestEventArgs>(OnConnectionRequest);

            while (true)
            {
                Server.Heartbeat();

                NetMessage msg;
                while ((msg = Server.ReadMessage()) != null)
                    HandleMessage(msg);

                System.Threading.Thread.Sleep(1);
            }
        }
Exemplo n.º 4
0
        public IPlayer(String dns, int port)
        {
            NetAppConfiguration myConfig = new NetAppConfiguration("MMO Mahjong");

            m_log = new NetLog();
            Init();
            m_client = new NetClient(myConfig, m_log);
            m_client.Connect(dns, port);
            m_client.StatusChanged += new EventHandler<NetStatusEventArgs>(StatusChanged);
            Application.Idle += new EventHandler(ApplicationLoop);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes the network engine.
        /// </summary>
        /// <param name="strName">The name of the user</param>
        public SpiderClient(String strName)
        {
            spiderName = strName;
            spiderConfig = new NetAppConfiguration("ymfas",DEFAULT_PORT);
            spiderLog = new NetLog();
            spiderLog.OutputFileName = "YMFAS Net Log (Port " + DEFAULT_PORT + ").html";
            localSessionQueue = new Queue(50);
            messageQueue = new Queue(50);
            disconnectQueue = new Queue(50);

            spiderNet = new NetClient(spiderConfig,spiderLog);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes the network engine.
        /// </summary>
        /// <param name="type">Client or Server</param>
        /// <param name="strName">The name of the user</param>
        public SpiderServer(String strName)
        {
            spiderName = strName;
            spiderConfig = new NetAppConfiguration("ymfas",DEFAULT_PORT);
            spiderLog = new NetLog();
            spiderLog.OutputFileName = "YMFAS Net Log (Port 30803).html";
            messageQueue = new Queue(50);
            disconnectQueue = new Queue(50);

            spiderConfig.MaximumConnections = MAX_CONNECTIONS;
            spiderConfig.ServerName = spiderName + "'s Game";
            spiderNet = new NetServer(spiderConfig,spiderLog);
            spiderNet.StatusChanged += new EventHandler<NetStatusEventArgs>(SpiderNet_StatusChangedHandler);
            clients = new ArrayList();
        }
Exemplo n.º 7
0
        public void Write(string str)
        {
            NetAppConfiguration config = NetBase.CurrentContext.Configuration;

            if (string.IsNullOrEmpty(str))
            {
                Write7BitEncodedUInt(0);
                return;
            }

            byte[] bytes = config.StringEncoding.GetBytes(str);

            Write7BitEncodedUInt((uint)bytes.Length);
            Write(bytes);
        }
Exemplo n.º 8
0
        public string ReadString()
        {
            NetAppConfiguration config = NetBase.CurrentContext.Configuration;

            int byteLen = (int)Read7BitEncodedUInt();

            Encoding enc = config.StringEncoding;

            // verify we have enough data
            if (m_readBitPtr + (byteLen * 8) > this.LengthBits)
            {
                int rem = (this.LengthBits - m_readBitPtr) / 8;
                throw new IndexOutOfRangeException("ReadString() tried to read " + byteLen + " bytes; but remainder of message only has " + rem + " bytes left");
            }

            byte[] bytes = ReadBytes(byteLen);

            return(enc.GetString(bytes, 0, bytes.Length));
        }
Exemplo n.º 9
0
      public void StartServer(int Port)
      {
         // Create a configuration for the server
         // 'SimpleChat' is our application identifier, which distinguishes it from other
         // lidgren.library.network applications
         // 14242 is the port the server listens on; ie. the port clients should connect to
         Config = new NetAppConfiguration("BBTcpTest", Port);

         // set maximum number of clients to 128
         Config.MaximumConnections = 128;

         // set a server name (which shows up when client discovers local servers)
         Config.ServerName = "BB Tcp Test Server";

         // enable encryption; this key was generated using the 'GenerateEncryptionKeys' application
         Config.EnableEncryption(
              "AQABwV1rQ1NDWzkkhUsYtMiRGjyxSn/zI13tros2RlXHlW6PA7Cvw2nOyMhDmweM+" +
              "T/+FWzssAWLh8qd+YHaMCCFskVdaZfRzquZlNOs9mX/vtxkLrXgBUaJQD/XOuBeJF" +
              "o3RfAKA4uhrEr7Bk1iB3zJQW1bSHK7KmmEWzMErk6iA7c=",
              "R6Gp2LkBnMugYdieAGrEhimxq92WXqWvJRfhNVEVrJ/WTxGnKwcJWYwGr2muqa8xG" +
              "xLM47BKLjW7K/EUuPtzSx8cGCDG1ET+5C984lu+pxb1XTHhz4qWpfS8Ti7BV4472F" +
              "wK9y/SxVew19rXNvYLD4aZrVOel+UV8a+3SJTGYXGyEX0OqmoaH0KPfGH5nGLOXcm" +
              "ZHy8yarV193X+bwQL8zxMbNk2UntbLg4m5+t2XyPY2cexDLLCWPviqbF4ZbLJO8xs" +
              "uni1H2QJZ9wn61fNhOQBkoI8YfYYah257zYDVCBgpDFPYXH5LaWfhevV3534Sz4Cw" +
              "D7r6m5aCD0Tsx9pDfsa81VyaJO37kpMTK7HvNtGv5GXwWTNNGwUQTfRIDE06RmLvv" +
              "zyC5YCT40Nqwjz5/dN4Qgb/dAUvsQYn3zsZNj7lfzurcLxGG4x0aCOVafeAb5oRHr" +
              "4VthHVNjymDmiPaiZyP8fmX10M+3hl1o/LDf71jEW+MqPiMgoYpGDEccFxMHtodV6" +
              "Yndv/z+4iFZ76K3Z/QQFgOSaVoAdx2VjlaCXaX6Kywng/pIf3mumy5WubTnjcdG7f" +
              "J+ImyVzs9lkiw==");

         // Now create the server!
         Server = new NetServer(Config, Log);

         //// catch clients connecting
         //ServerBB.Server = Program.Server;
         //Server.ConnectionRequest += new EventHandler<NetConnectRequestEventArgs>(Program.Server_ConnectionRequest);

         // catch status changes; for example, when a client connects or disconnects
         Server.StatusChanged += new EventHandler<NetStatusEventArgs>(this.Server_StatusChanged);
      }
Exemplo n.º 10
0
        public Program()
        {
            NetAppConfiguration myConfig = new NetAppConfiguration("MMO Mahjong", 14242);

            NetLog log = new NetLog();

            NetClient Client = new NetClient(myConfig, log);
            Client.Connect("localhost", 14242); // takes IP number or hostnameHow to detect connects/disconnects
            Client.StatusChanged += new EventHandler<NetStatusEventArgs>(StatusChanged);// to track connect/disconnect etc

            bool keepGoing = true;
            while (keepGoing)
            {
                Client.Heartbeat();

                NetMessage msg;
                while ((msg = Client.ReadMessage()) != null)
                {
                    string str = msg.ReadString(); // <- for example
                    System.Console.WriteLine("You got a packet containing: " + str);
                    Thread.Sleep(1);
                }
            }
        }
Exemplo n.º 11
0
        protected void InitBase(NetAppConfiguration config, NetLog log)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (log == null)
            {
                throw new ArgumentNullException("log");
            }

            IsLittleEndian = BitConverter.IsLittleEndian;
            //if (BitConverter.IsLittleEndian)
            BitWriter = new LittleEndianBitWriter();
            //else
            //	BitWriter = new BigEndianBitWriter();

            Configuration = config;
            Log           = log;

            Configuration.m_isLocked = true;             // prevent changes

            // validate config
            if (config.ApplicationIdentifier == NetConstants.DefaultApplicationIdentifier)
            {
                log.Error("Warning! ApplicationIdentifier not set in configuration!");
            }

            if (this is NetServer)
            {
                if (config.MaximumConnections == -1)
                {
                    throw new ArgumentException("MaximumConnections must be set in configuration!");
                }
                if (config.ServerName == NetConstants.DefaultServerName)
                {
                    log.Warning("Warning! Server name not set!");
                }
            }

            // create buffers
            m_sendBuffer    = new NetBuffer(config.SendBufferSize);
            m_receiveBuffer = new NetBuffer(config.ReceiveBufferSize);

            // Bind to port
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, config.Port);
                EndPoint   ep  = (EndPoint)iep;

                m_socket          = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_socket.Blocking = false;
                m_socket.Bind(ep);
                if (iep.Port != 0)
                {
                    Log.Info("Bound to port " + iep.Port);
                }
            }
            catch (SocketException sex)
            {
                if (sex.SocketErrorCode != SocketError.AddressAlreadyInUse)
                {
                    throw new NetException("Failed to bind to port " + config.Port + " - Address already in use!", sex);
                }
            }
            catch (Exception ex)
            {
                throw new NetException("Failed to bind to port " + config.Port, ex);
            }

            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, config.ReceiveBufferSize);
            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, config.SendBufferSize);

            m_senderRemote = (EndPoint) new IPEndPoint(IPAddress.Any, 0);
#if DEBUG
            m_lagLoss = new NetLogLossInducer(log);
#endif

            return;
        }
Exemplo n.º 12
0
 public NetClient(NetAppConfiguration config, NetLog log)
 {
     NetBase.CurrentContext = this;
     InitBase(config, log);
 }
Exemplo n.º 13
0
        public void ClientClicked()
        {
            this.Window.Title = "Trying To Connect....";

            //Since this is a client we dont have to specify a port here
            nac = new NetAppConfiguration("ZombieSim");
            Log = new NetLog();

            //Im Using this to show how to log files, So we pick that we want
            //to ignore nothing, Change this depending on what your
            //wanting to log for various debugging.
            Log.IgnoreTypes = NetLogEntryTypes.None;

            //Specify if you want to enable output to a file which we do.
            Log.IsOutputToFileEnabled = true;

            //If we output to a file we have to pick a filename to output it to.
            Log.OutputFileName = "Client.html";

            //We Initiate the client here, it has not connected to the server yet.
            client = new NetClient(nac, Log);

            //We Want to Log Events that are fired from the client
            client.StatusChanged +=
                new EventHandler<NetStatusEventArgs>(client_StatusChanged);

            //Finally we connect to the server, Specify the IP Address and
            //port if your wanting to connect to xxx.xxx.xxx.xxx
            //we would change this line to "192.168.1.1",12345
            client.Connect("192.168.0.200", 12345);
        }
Exemplo n.º 14
0
 public void HostClicked()
 {
     this.Window.Title = "Hosting....";
     nac = new NetAppConfiguration("ZombieSim", 12345);
     nac.MaximumConnections = 32;
     nac.ServerName = "ZombieSim!";
     Log = new NetLog();
     server = new NetServer(nac, Log);
     server.StatusChanged += new
         EventHandler<NetStatusEventArgs>(server_StatusChanged);
 }
Exemplo n.º 15
0
        protected void InitBase(NetAppConfiguration config, NetLog log)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            if (log == null)
                throw new ArgumentNullException("log");

            IsLittleEndian = BitConverter.IsLittleEndian;
            //if (BitConverter.IsLittleEndian)
            BitWriter = new LittleEndianBitWriter();
            //else
            //	BitWriter = new BigEndianBitWriter();

            Configuration = config;
            Log = log;

            Configuration.m_isLocked = true; // prevent changes

            // validate config
            if (config.ApplicationIdentifier == NetConstants.DefaultApplicationIdentifier)
                log.Error("Warning! ApplicationIdentifier not set in configuration!");

            if (this is NetServer)
            {
                if (config.MaximumConnections == -1)
                    throw new ArgumentException("MaximumConnections must be set in configuration!");
                if (config.ServerName == NetConstants.DefaultServerName)
                    log.Warning("Warning! Server name not set!");
            }

            // create buffers
            m_sendBuffer = new NetBuffer(config.SendBufferSize);
            m_receiveBuffer = new NetBuffer(config.ReceiveBufferSize);

            // Bind to port
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, config.Port);
                EndPoint ep = (EndPoint)iep;

                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_socket.Blocking = false;
                m_socket.Bind(ep);
                if (iep.Port != 0)
                    Log.Info("Bound to port " + iep.Port);
            }
            catch (SocketException sex)
            {
                if (sex.SocketErrorCode != SocketError.AddressAlreadyInUse)
                    throw new NetException("Failed to bind to port " + config.Port + " - Address already in use!", sex);
            }
            catch (Exception ex)
            {
                throw new NetException("Failed to bind to port " + config.Port, ex);
            }

            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, config.ReceiveBufferSize);
            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, config.SendBufferSize);

            m_senderRemote = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
            #if DEBUG
            m_lagLoss = new NetLogLossInducer(log);
            #endif

            return;
        }
Exemplo n.º 16
0
 public NetClient(NetAppConfiguration config, NetLog log)
 {
     NetBase.CurrentContext = this;
     InitBase(config, log);
 }