public static SMSG_ACCOUNT_LOGIN Login(String Username, String Password, SocketClient sockstate) { SMSG_ACCOUNT_LOGIN loginpacket = new SMSG_ACCOUNT_LOGIN(); loginpacket.Username = Username; string salt; SqlDataReader sdr; sdr = Database.Query("SELECT salt FROM account WHERE username='******'"); if (sdr == null) { loginpacket.State = (ushort)SMSG_ACCOUNT_LOGIN.LoginState.LOGIN_NOT_FOUND; return loginpacket; } if (sdr.HasRows) { salt = Convert.ToString(sdr["salt"]); Password = Misc.GetMD5Hash(Password + salt); sdr = Database.Query("Select id, access, username, options from account where username='******' and password='******'"); if (sdr.HasRows) { // set account options sockstate.Account.Access = Convert.ToInt32(sdr["access"]); sockstate.Account.AID = Convert.ToInt32(sdr["id"]); sockstate.Account.Options = Misc.GetOptions(Convert.ToString(sdr["options"])); // check online status foreach (SocketClient socks in Program.AvalonSrv.ClientList) { if (socks.Account.Username != null) { if (socks.Account.Username == Username) { Logger.Log(Logger.LogLevel.Access, "Server", "Account in use, disconnecting : {0} ", ((IPEndPoint)socks.Client.Socket.RemoteEndPoint).Address.ToString()); loginpacket.State = (ushort)SMSG_ACCOUNT_LOGIN.LoginState.LOGIN_IN_USE_LOBBY; return loginpacket; } } } loginpacket.State = (ushort)SMSG_ACCOUNT_LOGIN.LoginState.LOGIN_OK; return loginpacket; } else { loginpacket.State = (ushort)SMSG_ACCOUNT_LOGIN.LoginState.LOGIN_BAD_PASSWORD; return loginpacket; } } else { loginpacket.State = (ushort)SMSG_ACCOUNT_LOGIN.LoginState.LOGIN_NOT_FOUND; return loginpacket; } }
public Square(string sName, int status, int count, string ipaddr, int port, SocketClient sockstate) { Socket = sockstate; Name = sName; Status = status; Count = count; IPAddr = ipaddr; Port = port; }
/// <summary> /// ProcessPacket takes a given buffer and imports it to a Packet Factory for /// further processing. /// </summary> /// <param name="buffer"></param> public void ProcessPacket(byte[] buffer, SocketClient sockstate) { int offset = 0; pReader = new PacketReader(buffer, buffer.Length, true); // traverse packet buffer for cached packets while ((m_bPacketStream.Length - offset) >= m_HeaderSize) { // packet information pReader.Seek(offset, System.IO.SeekOrigin.Begin); UInt16 Size = pReader.ReadUInt16(); UInt16 Flag = pReader.ReadUInt16(); UInt16 Opcode = pReader.ReadUInt16(); if ((Flag == (UInt16)PacketFlag.Master) && (Size < m_MaxPacketSize)) { byte[] payload = new byte[Size]; Buffer.BlockCopy(m_bPacketStream, offset, payload, 0, Size); Console.WriteLine(Utility.Conversions.Misc.HexBytes(payload)); // find request packet if (PacketHandler.OpcodeList.ContainsKey(Opcode)) { PacketHandler.OpcodeList[Opcode](payload, sockstate); } offset += Size; } else { break; } } }
public void ProcessQueue(SocketClient sockstate) { while (PacketQueue.Count > 0) { byte[] packet = PacketQueue.Dequeue(); Console.WriteLine(Utility.Conversions.Misc.HexBytes(packet)); sockstate.Client.Socket.Send(packet); } }
/// <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); } }
/// <summary> /// Invoked when a client sends data. /// </summary> /// <param name="async"></param> public void OnDataReceive(IAsyncResult async) { int numRecvBytes = 0; try { clientState = (SocketClient)async.AsyncState; numRecvBytes = m_SockConnection.EndReceive(async); if (numRecvBytes == 0) { clientState.Disconnect(); return; } // copy new data and process packet lock (clientState) { byte[] newData = new byte[numRecvBytes]; Buffer.BlockCopy(m_bRecvBuffer, 0, newData, 0, numRecvBytes); m_bPacketStream = newData; // process packets ProcessPacket(m_bPacketStream, clientState); ProcessQueue(clientState); } // finished receiving/processing data, go back to listening state. this.Socket.BeginReceive(m_bRecvBuffer, 0, 0x1000, SocketFlags.None, new AsyncCallback(OnDataReceive), clientState); } catch (NullReferenceException) { clientState.Disconnect(); } catch (ObjectDisposedException) { clientState.Disconnect(); } catch (SocketException se) { clientState.Disconnect(); Logger.Log(Logger.LogLevel.Error, "Server", "{0}", se.Message); } }
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; }
public void ProcessQueue(SocketClient sockstate) { while (PacketQueue.Count > 0) { byte[] packet = PacketQueue.Dequeue(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("[Server->Client]"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(Utility.Conversions.Misc.HexBytes(packet)); sockstate.Client.Socket.Send(packet); } }
/// <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); } }