Example #1
0
        public void Connect(INetContext context)
        {
            NetPeerConfiguration config = new NetPeerConfiguration(context.ApplicationName);

            client = new NetClient(config);
            client.Start();

            int tries = 0;

            while (tries < 2 && client.Connections.Count == 0)
            {
                if (tries > 0)
                {
                    System.Threading.Thread.Sleep(500);
                }

                client.Connect(context.IPAddress, context.Port);
                while (client.Status == NetPeerStatus.Starting)
                {
                    System.Threading.Thread.Sleep(10);
                }

                tries++;
            }

            var t = Environment.TickCount;

            while (Environment.TickCount - t < 1000 && client.ConnectionStatus != NetConnectionStatus.Connected)
            {
                this.UpdateClient();
            }
        }
Example #2
0
 public void InitializeServices(INetContext context)
 {
     for (int i = 0; i < services.Count; i++)
     {
         services[i].InitializeService(context);
     }
 }
Example #3
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            connectedUsers = new HashSet <NetUser>();
            context.Packets.Register <P_Ping>();

            ChPing = Router.Route <P_Ping>();
        }
Example #4
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            buffer = new DataBuffer();

            context.Packets.Register <P_SetData>();
            context.Packets.Register <P_AddUser>();
            context.Packets.Register <P_RemoveUser>();

            netTimeService = context.Services.Get <NetTimeService>();
        }
Example #5
0
        public void Connect(INetContext context)
        {
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(context.IPAddress);
            IPAddress   ipAddress     = ipHostInfo.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, context.Port);

            Connection = new Socket(ipAddress.AddressFamily,
                                    SocketType.Stream, ProtocolType.Tcp);

            //Connection.Connect(ipAddress, context.Port);
            Connection.BeginConnect(localEndPoint, OnConnect, Connection);
        }
Example #6
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);

            this.CreateRoomRequestChannel   = this.Router.Route <CreateRoomRequest>();
            this.CreateRoomResponseChannel  = this.Router.Route <CreateRoomResponse>();
            this.GetRoomListRequestChannel  = this.Router.Route <GetRoomListRequest>();
            this.GetRoomListResponseChannel = this.Router.Route <GetRoomListResponse>();
            this.JoinRoomRequestChannel     = this.Router.Route <JoinRoomRequest>();
            this.JoinRoomResponseChannel    = this.Router.Route <JoinRoomResponse>();
            this.SetUserIdChannel           = this.Router.Route <UserId>();
            this.SetUserStateChannel        = this.Router.Route <UserState>();
        }
Example #7
0
        public virtual void InitializeService(INetContext context)
        {
            Context         = context;
            scheduledEvents = new List <Tuple <DateTime, Action> >();

            if (IsClient)
            {
                Router = new PacketRouter(Client.Router, GetRouterPrefix());
            }
            if (IsServer)
            {
                Router = new PacketRouter(Server.Router, GetRouterPrefix());
            }
        }
Example #8
0
        public void StartServer(INetContext context)
        {
            this.maxConnections = context.MaxConnections;
            this.port           = context.Port;

            clientListener = new TcpListener(IPAddress.Any, context.Port);
            clientListener.Start(context.MaxConnections);

            //clientList = new List<ClientInfo>();
            message    = new byte[1024 * 512];
            clientList = new List <TcpClient>();
            connected  = new HashSet <TcpClient>();

            IsActive = true;
        }
Example #9
0
        public virtual void InitializeClient(INetContext context, INetClientProvider provider)
        {
            if (IsInitialized)
            {
                throw new Exception("Server already initialized");
            }

            IsInitialized    = true;
            this.provider    = provider;
            this.IsConnected = false;
            this.Router      = new PacketRouter(provider, this);
            this.Context     = context;
            disposed         = false;

            context.LockContext(client: this);
        }
Example #10
0
        public void StartServer(INetContext context)
        {
            var key = context.IPAddress + ":" + context.Port;

            lock (Servers) {
                if (Servers.ContainsKey(key))
                {
                    throw new Exception("Server already exists with that IP:Port endpoint");
                }

                Servers.Add(key, this);
            }

            Clients = new List <DirectClientProvider>();
            AcceptIncomingConnections = true;
            IsActive = true;
        }
Example #11
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            userList = new List <NetUser>();
            UserList = userList.AsReadOnly();

            if (IsServer)
            {
                Server.NetUserConnected    += onUserConnected;
                Server.NetUserDisconnected += onUserDisconnected;
            }

            ClSetUser     = ClientAction <NetUser>(_clSetUser);
            ClAddUser     = ClientAction <NetUser>(_clAddUser);
            ClRemoveUser  = ClientAction <NetUser>(_clRemoveUser);
            ClSetUserList = ClientAction <List <NetUser> >(_clSetUserList);
        }
Example #12
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            outQueue    = new Queue <Packet>();
            Initialized = true;

            context.Packets.Register <TestPacket>();

            if (Router != null)
            {
                Channel = Router.Route <TestPacket>();

                Channel.Receive += (e) => {
                    Received     = true;
                    ReceivedData = e.Data.data;
                };
            }
        }
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            MatchMap      = new Dictionary <Guid, INetMatchController>();
            this.Channels = new NetMatchControllerChannels(this.Router);
            this.Channels.UserLeftEventChannel.Receive   += OnUserLeftEventReceived;
            this.Channels.UserJoinedEventChannel.Receive += OnUserJoinedEventReceived;

            this.Channels.RoundEndedEventChannel.Receive   += OnRoundEndedEventReceived;
            this.Channels.RoundCreatedEventChannel.Receive += OnRoundCreatedEventReceived;

            this.Channels.MatchStartedEventChannel.Receive += OnMatchStartedEventReceived;
            this.Channels.MatchEndedEventChannel.Receive   += OnMatchEndedEventReceived;

            this.Channels.EndMatchRequestChannel.Receive   += OnEndMatchRequestReceived;
            this.Channels.StartMatchRequestChannel.Receive += OnStartMatchRequestReceived;

            this.Channels.PlayCardRequestChannel.Receive  += OnPlayCardRequestReceived;
            this.Channels.PlayCardResponseChannel.Receive += OnPlayCardResponseReceived;
        }
Example #14
0
        public void Connect(INetContext context)
        {
            var server = DirectServerProvider.GetServer(context.IPAddress, context.Port);

            if (server == null)
            {
                long tick = Environment.TickCount;
                while (Environment.TickCount < tick + 50 && server == null)
                {
                    server = DirectServerProvider.GetServer(context.IPAddress, context.Port);
                    System.Threading.Thread.Sleep(1);
                }

                if (server == null)
                {
                    throw new Exception("Could not connect to server");
                }
            }
            server.TryConnectClient(this);
        }
Example #15
0
        public virtual void InitializeServer(INetContext context, INetServerProvider provider)
        {
            if (IsInitialized)
            {
                throw new Exception("Server already initialized");
            }

            IsInitialized = true;
            this.provider = provider;
            this.Router   = new PacketRouter(provider, this);
            this.Context  = context;
            this.IsActive = false;

            nextUserId = 0;
            disposed   = false;
            userMap    = new Dictionary <object, NetUser>();
            userList   = new List <NetUser>();

            Context.LockContext(server: this);
        }
Example #16
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            clientTimer = new Stopwatch();
            serverTimer = new Stopwatch();
            pingTimer   = new Stopwatch();

            if (IsClient)
            {
                Client.OnConnected += OnConnected;
            }

            context.Packets.Register <P_GetNetTime>();
            context.Packets.Register <P_SetNetTime>();

            ChSetNetTime = Router.Route <P_SetNetTime>();
            ChGetNetTime = Router.Route <P_GetNetTime>();

            ChSetNetTime.Receive += OnSetNetTime;
            ChGetNetTime.Receive += OnGetNetTime;
        }
Example #17
0
        protected virtual INetContext CreateContext()
        {
            if (_cache != null)
            {
                return(_cache);
            }

            if (guid == null)
            {
                guid = Guid.NewGuid().ToString();
            }

            INetContext ctx = this.Context;

            ctx.ApplicationName = guid;
            ctx.IPAddress       = guid;
            ctx.Port            = 0;

            _cache = ctx;
            return(ctx);
        }
Example #18
0
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            context.Packets.Register <RFCExecute>();
            ChRFCExecute = Router.Route <RFCExecute>();

            ChRFCExecute.Receive += (e) => {
                onExecute(e);
            };

            if (IsServer)
            {
                Server.NetUserConnected += (s, arg) => {
                    userList.Add(arg.User);
                };

                Server.NetUserDisconnected += (s, arg) => {
                    userList.Remove(arg.User);
                };
            }
        }
        public override void InitializeService(INetContext context)
        {
            base.InitializeService(context);
            this.RoomMap = new Dictionary <Guid, INetRoomController>();

            this.Channels = new NetRoomControllerChannels(this.Router);
            this.Channels.RoomStartedEventChannel.Receive += OnRoomStartedEvent;
            this.Channels.RoomEndedEventChannel.Receive   += OnRoomEndedEvent;
            this.Channels.StartRoomRequestChannel.Receive += OnStartRoomRequest;
            this.Channels.CloseRoomRequestChannel.Receive += OnCloseRoomRequest;

            this.Channels.MatchCreatedEventChannel.Receive  += OnMatchCreatedEvent;
            this.Channels.CreateMatchRequestChannel.Receive += OnCreateMatchRequest;

            this.Channels.GetMatchListRequestChannel.Receive  += OnGetMatchListRequest;
            this.Channels.GetMatchListResponseChannel.Receive += OnGetMatchListResponse;

            this.Channels.UserJoinedEventChannel.Receive  += OnUserJoinedEvent;
            this.Channels.UserLeftEventChannel.Receive    += OnUserLeftEvent;
            this.Channels.JoinMatchRequestChannel.Receive += OnUserJoinMatchRequest;
        }
Example #20
0
        public void Connect(INetContext context)
        {
            if (IsConnected)
            {
                Disconnect();
            }

            message = new byte[1024 * 512];

            client = new TcpClient {
                NoDelay = false
            };

            client.SendBufferSize = client.ReceiveBufferSize = 1024 * 256;
            client.Connect(context.IPAddress, context.Port);

            if (client.Connected)
            {
                UserConnected?.Invoke(this, new ProviderUserEventArgs());
                IsConnected = true;
            }
        }
Example #21
0
        public void StartServer(INetContext context)
        {
            AcceptIncomingConnections = true;

            NetPeerConfiguration config = new NetPeerConfiguration(context.ApplicationName)
            {
                Port = context.Port,
                //MaximumConnections = context.MaxConnections
            };

            //maxConnections = context.MaxConnections + 2;

            connections = new List <NetConnection>();
            server      = new NetServer(config);
            server.Start();
            while (server.Status == NetPeerStatus.Starting)
            {
                System.Threading.Thread.Sleep(100);
            }

            IsActive = true;
        }