Example #1
0
        private void Serve()
        {
            log.Info("Session server is starting.");
            responseSocket = new ResponseSocket();

            switch (sessionType)
            {
            case eSessionType.Local:
                responseSocket.Bind("inproc://opendiablo2-session");
                break;

            case eSessionType.Server:
            case eSessionType.Remote:
            default:
                throw new OpenDiablo2Exception("This session type is currently unsupported.");
            }

            OnJoinGame        += OnJoinGameHandler;
            OnMoveRequest     += OnMovementRequestHandler;
            OnUpdateEquipment += OnUpdateEquipmentHandler;

            var proactor = new NetMQProactor(responseSocket, (socket, message) =>
            {
                foreach (var msg in message)
                {
                    using (var ms = new MemoryStream(msg.ToByteArray()))
                        using (var br = new BinaryReader(ms))
                        {
                            var messageFrame = getMessageFrame((eMessageFrameType)br.ReadByte());
                            messageFrame.LoadFrom(br);
                            messageFrame.Process(socket.GetHashCode(), this);
                        }
                }
            });

            running = true;
            WaitServerStartEvent.Set();
            Task.Run(() =>
            {
                var lastRun = DateTime.Now;
                while (running)
                {
                    var newTime  = DateTime.Now;
                    var timeDiff = (newTime - lastRun).TotalMilliseconds;
                    lastRun      = newTime;

                    gameServer.Update((int)timeDiff);
                    if (timeDiff < serverUpdateRate)
                    {
                        Thread.Sleep((int)Math.Min(serverUpdateRate, Math.Max(0, serverUpdateRate - timeDiff)));
                    }
                }
            });
            resetEvent.WaitOne();
            proactor.Dispose();
            running = false;
            responseSocket.Dispose();
            log.Info("Session server has stopped.");
        }
Example #2
0
        private void Serve()
        {
            log.Info("Session server is starting.");
            responseSocket = new ResponseSocket();

            switch (sessionType)
            {
            case eSessionType.Local:
                responseSocket.Bind("inproc://opendiablo2-session");
                break;

            case eSessionType.Server:
            case eSessionType.Remote:
            default:
                throw new OpenDiablo2Exception("This session type is currently unsupported.");
            }

            OnJoinGame    += OnJoinGameHandler;
            OnMoveRequest += OnMovementRequestHandler;

            var proactor = new NetMQProactor(responseSocket, (socket, message) =>
            {
                var bytes         = message.First().ToByteArray();
                var frameType     = (eMessageFrameType)bytes[0];
                var frameData     = bytes.Skip(1).ToArray(); // TODO: Can we maybe use pointers? This seems wasteful
                var messageFrame  = getMessageFrame(frameType);
                messageFrame.Data = frameData;
                messageFrame.Process(socket.GetHashCode(), this);
            });

            running = true;
            WaitServerStartEvent.Set();
            Task.Run(() =>
            {
                var lastRun = DateTime.Now;
                while (running)
                {
                    var newTime  = DateTime.Now;
                    var timeDiff = (newTime - lastRun).TotalMilliseconds;
                    lastRun      = newTime;

                    gameServer.Update((int)timeDiff);
                    if (timeDiff < serverUpdateRate)
                    {
                        Thread.Sleep((int)Math.Min(serverUpdateRate, Math.Max(0, serverUpdateRate - timeDiff)));
                    }
                }
            });
            resetEvent.WaitOne();
            proactor.Dispose();
            running = false;
            responseSocket.Dispose();
            log.Info("Session server has stopped.");
        }