Advertisement
Guest User

NetworkService

a guest
Apr 22nd, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. // Copyright (C) Paul Becker, - All Rights Reserved
  2. // Unauthorized copying of this file, via any medium is strictly prohibited
  3. // Proprietary and confidential
  4. // Written by Paul Becker <paul.becker1@gmx.de>, 22:06
  5.  
  6. #region usings
  7.  
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13. using SeraphimServer.Logging;
  14.  
  15. #endregion
  16.  
  17. namespace SeraphimServer.Network
  18. {
  19.     public class NetworkService
  20.     {
  21.         private static ServerListener _listener;
  22.         private static readonly List<int> ReconnectingAccounts = new List<int>();
  23.         public static int PktCounter;
  24.         public static Connection CnxPreAlloc;
  25.         public static ConcurrentDictionary<int, Connection> Clients = new ConcurrentDictionary<int, Connection>();
  26.         private static int _conId;
  27.  
  28.         public static void Start()
  29.         {
  30.             PacketHolder.Load();
  31.             Log.Info("PacketHolder loaded");
  32.             _listener = new ServerListener();
  33.             _listener.Start();
  34.             BeginAccept();
  35.             Log.Info("NetworkService - UP!");
  36.         }
  37.  
  38.         private static void BeginAccept()
  39.         {
  40.             CnxPreAlloc = new Connection(null);
  41.             _listener.BeginAcceptTcpClient(EndAccept, _listener);
  42.         }
  43.  
  44.         private static void EndAccept(IAsyncResult ar)
  45.         {
  46.             var lsn = (TcpListener) ar.AsyncState;
  47.             var client = lsn.EndAcceptTcpClient(ar);
  48.             CnxPreAlloc.SetClient(client);
  49.             CnxPreAlloc.OnConnect();
  50.             Interlocked.Increment(ref _conId);
  51.             CnxPreAlloc.ConId = _conId;
  52.             Clients.TryAdd(_conId, CnxPreAlloc);
  53.  
  54.             Log.SystemInfo(String.Format("NetworkService -> Added LClient [List Size = {0}]", Clients.Count));
  55.             BeginAccept();
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement