public virtual void AcceptConnection(IAsyncResult ar) { // TODO: @norrismiv async callbacks+inheritance? and/or can these callbacks suck less? AllDone.Set(); if (!Active) { return; } Socket handler; Socket clientSocket; try { clientSocket = (Socket)ar.AsyncState; handler = clientSocket.EndAccept(ar); } catch (ObjectDisposedException e) { GameLog.Error($"Disposed socket {e.Message}"); return; } Client client = new Client(handler, this); Clients.TryAdd(handler.Handle, client); GlobalConnectionManifest.RegisterClient(client); if (this is Lobby) { var x7E = new ServerPacket(0x7E); x7E.WriteByte(0x1B); x7E.WriteString("CONNECTED SERVER\n"); client.Enqueue(x7E); GameLog.DebugFormat("Lobby: AcceptConnection occuring"); GameLog.DebugFormat("Lobby: cid is {0}", client.ConnectionId); } else if (this is Login) { GameLog.DebugFormat("Login: AcceptConnection occuring"); GameLog.DebugFormat("Login: cid is {0}", client.ConnectionId); } else if (this is World) { GameLog.DebugFormat("World: AcceptConnection occuring"); GameLog.DebugFormat("World: cid is {0}", client.ConnectionId); } try { handler.BeginReceive(client.ClientState.Buffer, 0, client.ClientState.Buffer.Length, 0, new AsyncCallback(ReadCallback), client.ClientState); GameLog.DebugFormat("AcceptConnection returning"); clientSocket.BeginAccept(new AsyncCallback(AcceptConnection), clientSocket); } catch (SocketException e) { Game.ReportException(e); handler.Close(); } }
internal ServerPacket Packet() { var packet = new ServerPacket(OpCode); packet.WriteByte(ChatType); packet.WriteUInt32(TargetId); packet.WriteByte(LineLength); packet.WriteString(LineText); packet.WriteByte(0); packet.WriteByte(0); packet.WriteByte(0); return(packet); }
internal ServerPacket Packet() { ServerPacket packet = new ServerPacket(OpCode); packet.WriteByte(Type); // Unusually, this message length includes the settings number above, // and is not just the string length... packet.WriteByte(00); packet.WriteByte((byte)(DisplayString.Length + 1)); packet.WriteByte((byte)(Number + 0x30)); packet.WriteString(DisplayString); return(packet); }
public override void AcceptConnection() { if (TcpListener.Pending()) { var socket = TcpListener.AcceptSocket(); var client = new Client(socket, this); var x7E = new ServerPacket(0x7E); x7E.WriteByte(0x1B); x7E.WriteString("CONNECTED SERVER\n"); client.Enqueue(x7E); var thread = new Thread(client.ClientLoop); thread.Start(); } }