static void Main(string[] args) { AvalonCfg = Config.Load("config.xml"); AvalonSrv = new SocketServer(AvalonCfg.Server.IPAddress, AvalonCfg.Server.ListenPort); Database.Initialize(); AvalonSrv.Listen(); while (true) { System.Threading.Thread.Sleep(1000); if (Time.timeGetTime() - Time.LastCheckTime > 30000) { Time.LastCheckTime = Time.timeGetTime(); lock (AvalonSrv.ClientList) { // ping :3 pong :) for (int i = 0; i < AvalonSrv.ClientList.Count; ++i) { //AvalonSrv.ClientList[i].Ping(); } } } } }
static void Main(string[] args) { AvalonCfg = Config.Load("config.xml"); AvalonSrv = new SocketServer(AvalonCfg.Server.IPAddress, AvalonCfg.Server.ListenPort); Database.Initialize(); AvalonSrv.Listen(); while (true) { System.Threading.Thread.Sleep(1000); } }
/// <summary> /// Stores a reference to the clients socket. /// </summary> /// <param name="mainSock"></param> public SocketClient(SocketServer mainSock) { m_Client = mainSock; }
/// <summary> /// Invoked when listener recieved a new connection. /// </summary> /// <param name="async"></param> public void OnNewConnection(IAsyncResult async) { SocketServer clientSock = new SocketServer(m_MainSock.EndAccept(async)); try { // finished processing socket, go back to listening state. ((Socket)async.AsyncState).BeginAccept(new AsyncCallback(OnNewConnection), async.AsyncState); // initiate socket state object. SocketClient clientState = new SocketClient(clientSock); clientState.IPAddress = ((IPEndPoint)clientState.Client.Socket.RemoteEndPoint).Address.ToString(); lock ( this.ClientList ) { ClientList.Add(clientState); clientState.SessionID = m_SessionCount++; } // do further processing for recently connected socket. clientSock.Socket.BeginReceive(clientSock.m_bRecvBuffer, 0, 0x1000, SocketFlags.None, clientSock.OnDataReceive, clientState); Logger.Log(Logger.LogLevel.Access, "Server", "[{0}] Client Connected : {1}", ClientList.Count, ((IPEndPoint)clientSock.Socket.RemoteEndPoint).Address.ToString()); } catch (ObjectDisposedException) { Logger.Log(Logger.LogLevel.Access, "Server", "[{0}] Socket has been closed.", ClientList.Count); ((Socket)async.AsyncState).BeginAccept(new AsyncCallback(OnNewConnection), async.AsyncState); } catch (SocketException se) { Logger.Log(Logger.LogLevel.Error, "Server", "OnNewConnection: {0}", se.Message); ((Socket)async.AsyncState).BeginAccept(new AsyncCallback(OnNewConnection), async.AsyncState); } }
public bool Listen() { try { // initialize packet factory PacketHandler.Initialize(); m_MasterSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remoteEP = new IPEndPoint(Program.AvalonCfg.Master.IPAddress, Program.AvalonCfg.Master.Port); m_MasterSock.Connect(remoteEP); SMSG_CONNECT_MASTER spkt = new SMSG_CONNECT_MASTER(Program.AvalonCfg.Server.Servername, Program.AvalonCfg.Server.ServerIP, (ushort)Program.AvalonCfg.Server.ListenPort); m_MasterSock.Send(spkt.Stream); SocketServer clientSock = new SocketServer(m_MasterSock); SocketClient clientState = new SocketClient(clientSock); clientSock.Socket.BeginReceive(clientSock.m_bRecvBuffer, 0, 0x1000, SocketFlags.None, clientSock.OnDataReceive, clientState); // listen on port 15554 m_MainSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_MainSock.Bind(new IPEndPoint(IPAddress.Any, m_ListenPort)); m_MainSock.Listen(0x20); // when a connection is established invoke OnNewConnection() m_MainSock.BeginAccept(new AsyncCallback(OnNewConnection), m_MainSock); Logger.Log(Logger.LogLevel.Info, "Server Status", "Listening on port " + Program.AvalonCfg.Server.ListenPort); return true; } catch (SocketException se) { Logger.Log(Logger.LogLevel.Error, "SocketServer", "Listen: {0}", se.Message); } return false; }
/// <summary> /// Invoked when listener recieved a new connection. /// </summary> /// <param name="async"></param> public void OnNewConnection(IAsyncResult async) { SocketServer clientSock = new SocketServer(m_MainSock.EndAccept(async)); try { // finished processing socket, go back to listening state. ((Socket)async.AsyncState).BeginAccept(new AsyncCallback(OnNewConnection), async.AsyncState); // initiate socket state object. SocketClient clientState = new SocketClient(clientSock); lock ( this.ClientList ) { ClientList.Add(clientState); } // do further processing for recently connected socket. clientSock.Socket.BeginReceive(clientSock.m_bRecvBuffer, 0, 0x1000, SocketFlags.None, clientSock.OnDataReceive, clientState); Logger.Log(Logger.LogLevel.Access, "Server", "[{0}] Client Connected : {1}", ClientList.Count, ((IPEndPoint)clientSock.Socket.RemoteEndPoint).Address.ToString()); // send encryption key Random rand = new Random(); SMSG_ENCRYPTION_KEY spkt = new SMSG_ENCRYPTION_KEY(rand.Next()); Console.WriteLine("[Server->Client] Encryption"); Console.WriteLine(Misc.HexBytes(spkt.Stream)); clientState.Client.Socket.Send(spkt.Stream); } catch (ObjectDisposedException) { Logger.Log(Logger.LogLevel.Access, "Server", "[{0}] Socket has been closed.", ClientList.Count); ((Socket)async.AsyncState).BeginAccept(new AsyncCallback(OnNewConnection), async.AsyncState); } catch (SocketException se) { Logger.Log(Logger.LogLevel.Error, "Server", "OnNewConnection: {0}", se.Message); ((Socket)async.AsyncState).BeginAccept(new AsyncCallback(OnNewConnection), async.AsyncState); } }