public User AddNewClient(TcpClientWrapper client) { User newUser = new User(client, false); TcpClientUsers.Add(client, newUser); UserBuffers.Add(newUser, new byte[0]); client.DataRead += new TcpClientWrapper.DataReadDelegate(client_DataRead); client.Disconnected += new TcpClientWrapper.DisconnectedDelegate(client_Disconnected); client.AsyncRead(Config.ReadBufferSize, true); return newUser; }
/// <summary> /// An event that fires when a socket receives data. Data is stored in a buffer until a complete packet can be extracted. /// </summary> /// <param name="sender">Socket that send the data</param> /// <param name="data">Payload</param> protected void client_DataRead(TcpClientWrapper sender, ref byte[] data) { ServerStats.bytesRecv += data.Length; if (!TcpClientUsers.ContainsKey(sender)) return; User user = TcpClientUsers[sender]; PacketReader packet; user.RecvBufferer.AppendData(ref data); while ((packet = user.RecvBufferer.GetNextPacket()) != null) HandlePacket(user, packet); }
public User(TcpClientWrapper clientSocket, bool isConsole) { socket = clientSocket; _isConsole = isConsole; packet = new Packet(); packet.skipHeaders = isConsole; packet.DataSent += new Packet.SendDataDelegate(packet_SendData); bufferer = new PacketBufferer(SendDataFinal, null, Config.SendBufferInterval); Flags = UserFlags.Normal; }
public void AcceptTcpClientCallback(IAsyncResult ar) { TcpListenerWrapper listener = (TcpListenerWrapper)ar.AsyncState; TcpClientWrapper client = new TcpClientWrapper(_clientSendBacklog, listener.EndAcceptTcpClient(ar)); System.Threading.ThreadPool.QueueUserWorkItem(delegate { listener.OnClientConnected(client); }); listener.StartListening(); }
protected void SetupSocket() { socket = new TcpClientWrapper(20); socket.ConnectionEstablished += new TcpClientWrapper.ConnectionEstablishedDelegate(socket_ConnectionEstablished); socket.ConnectionRefused += new TcpClientWrapper.ConnectionRefusedDelegate(socket_ConnectionRefused); socket.Disconnected += new TcpClientWrapper.DisconnectedDelegate(socket_Disconnected); socket.DataRead += new TcpClientWrapper.DataReadDelegate(socket_DataRead); packet = new Packet(); packet.DataSent += new Packet.SendDataDelegate(packet_DataSent); bufferer = new PacketBufferer(SendDataFinal, null, 100); }
public User(TcpClientWrapper clientSocket, bool isConsole) { socket = clientSocket; _isConsole = isConsole; packet = new Packet(); packet.skipHeaders = isConsole; packet.DataSent += new Packet.SendDataDelegate(packet_SendData); PacketSendBufferer.AddSendHandler(this, SendDataFinal); recvBufferer = new PacketBuffer(); Flags = UserFlags.Normal; }
/// <summary> /// An event that fires when a socket has disconnected. DO NOT CALL Shutdown() /// </summary> /// <param name="sender">The socket the disconnected</param> protected void client_Disconnected(TcpClientWrapper sender) { //does this need thread locking? if (!TcpClientUsers.ContainsKey(sender)) return; User user = TcpClientUsers[sender]; TcpClientUsers.Remove(sender); DisconnectUser(user); Users.Remove(user); user.Dispose(); user = null; }
protected void client_Disconnected(TcpClientWrapper sender) { lock (_locker) { if (!TcpClientUsers.ContainsKey(sender)) return; User user = TcpClientUsers[sender]; if (UserDisconnected != null) UserDisconnected(user); TcpClientUsers.Remove(sender); UserBuffers.Remove(user); user.Dispose(); user = null; } }
protected void socket_DataRead(TcpClientWrapper sender, byte[] data) { int oldLen = packetBuffer.Length; Array.Resize(ref packetBuffer, oldLen + data.Length); Array.Copy(data, 0, packetBuffer, oldLen, data.Length); //check to see if whole packet is available byte[] completePacket; while ((completePacket = PacketBuffer.GetNextPacket(ref packetBuffer)) != null) { this.Dispatcher.Invoke(new Action(delegate { HandlePacket(new PacketReader(completePacket)); }), null); } }
/// <summary> /// An event that fires when a socket connects. /// </summary> /// <param name="client"></param> protected void listener_OnClientConnected(TcpClientWrapper client) { ServerStats.totalConnections++; string IP = EndpointToIP(client.Client.RemoteEndPoint); if (CheckFloodingConnection(IP)) { client.Client.Shutdown(System.Net.Sockets.SocketShutdown.Both); return; } IncrementFloodingConnection(IP); //checks done, create a User object User newUser = new User(client, false); TcpClientUsers.Add(client, newUser); client.DataRead += new TcpClientWrapper.DataReadDelegate(client_DataRead); client.Disconnected += new TcpClientWrapper.DisconnectedDelegate(client_Disconnected); client.AsyncRead(Config.Network.ReadBufferSize, true); Users.Add(newUser); }
protected void client_DataRead(TcpClientWrapper sender, byte[] data) { ServerStats.bytesRecv += data.Length; lock (_locker) { if (!TcpClientUsers.ContainsKey(sender)) return; User user = TcpClientUsers[sender]; byte[] buffer = UserBuffers[user]; int oldLen = buffer.Length; Array.Resize(ref buffer, oldLen + data.Length); Array.Copy(data, 0, buffer, oldLen, data.Length); //check to see if whole packet is available byte[] completePacket; while ((completePacket = PacketBuffer.GetNextPacket(ref buffer)) != null) { UserPacketReceived(user, new PacketReader(completePacket)); } UserBuffers[user] = buffer; } }
private void button1_Click(object sender, RoutedEventArgs e) { lock (sockets) { for (int i = 0; i < 10; i++) { TcpClientWrapper sock = new TcpClientWrapper(20); sock.ConnectionEstablished += new TcpClientWrapper.ConnectionEstablishedDelegate(sock_ConnectionEstablished); sock.DataRead += new TcpClientWrapper.DataReadDelegate(sock_DataRead); Packet pack = new Packet(); pack.DataSent += new Packet.SendDataDelegate(pack_DataSent); PacketBufferer bufferer = new PacketBufferer(SendDataFinal, sock, 100); sockets.Add(pack, sock); packets.Add(sock, pack); buffers.Add(sock, bufferer); sock.AsyncConnect("127.0.0.1", 4800); } } }
void sock_ConnectionEstablished(TcpClientWrapper sender) { packets[sender].Clear() .InsertStringNT("Lynxy" + rnd.Next(10000, 99999).ToString()) .InsertStringNT("pass2") .InsertStringNT("LynxVN") .InsertByte(0) .Send(0x01); packets[sender].Clear().InsertStringNT("/join spam").Send(0x03); }
protected void socket_ConnectionEstablished(TcpClientWrapper sender) { AddChat(ChatColors.ConnectionGood, "Connection accepted. Sending login information."); socket.AsyncRead(1024, true); SendLogonPacket(); }
void sock_DataRead(TcpClientWrapper sender, byte[] data) { }
protected void listener_OnClientConnected(TcpClientWrapper client) { ServerStats.totalConnections++; Users.Add(clients.AddNewClient(client)); }
protected void socket_ConnectionRefused(TcpClientWrapper sender) { AddChat(ChatColors.ConnectionBad, "The connection was refused."); }
private void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { socket = null; packet = null; bufferer = null; Channel = null; } disposed = true; } }
protected void socket_Disconnected(TcpClientWrapper sender) { AddChat(ChatColors.ConnectionBad, "Disconnected from server."); }
private void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { socket.Client.Dispose(); socket = null; packet = null; PacketSendBufferer.RemoveSendHandler(this); recvBufferer = null; Channel = null; } disposed = true; } }