예제 #1
0
 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;
 }
예제 #2
0
파일: ClientHandler.cs 프로젝트: Lynxy/VNet
        /// <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);
        }
예제 #3
0
파일: User.cs 프로젝트: AlphaBlend/VNet
            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;
            }
예제 #4
0
        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();
        }
예제 #5
0
파일: Socket.cs 프로젝트: AlphaBlend/VNet
        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);
        }
예제 #6
0
파일: User.cs 프로젝트: Lynxy/VNet
            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;
            }
예제 #7
0
파일: ClientHandler.cs 프로젝트: Lynxy/VNet
 /// <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;
 }
예제 #8
0
 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;
     }
 }
예제 #9
0
파일: Socket.cs 프로젝트: AlphaBlend/VNet
        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);
            }
        }
예제 #10
0
파일: ClientHandler.cs 프로젝트: Lynxy/VNet
        /// <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);
        }
예제 #11
0
            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;
                }
            }
예제 #12
0
        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);
                }
            }
        }
예제 #13
0
 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);
 }
예제 #14
0
파일: Socket.cs 프로젝트: AlphaBlend/VNet
 protected void socket_ConnectionEstablished(TcpClientWrapper sender)
 {
     AddChat(ChatColors.ConnectionGood, "Connection accepted. Sending login information.");
     socket.AsyncRead(1024, true);
     SendLogonPacket();
 }
예제 #15
0
 void sock_DataRead(TcpClientWrapper sender, byte[] data)
 {
 }
예제 #16
0
파일: Listener.cs 프로젝트: AlphaBlend/VNet
 protected void listener_OnClientConnected(TcpClientWrapper client)
 {
     ServerStats.totalConnections++;
     Users.Add(clients.AddNewClient(client));
 }
예제 #17
0
파일: Socket.cs 프로젝트: AlphaBlend/VNet
 protected void socket_ConnectionRefused(TcpClientWrapper sender)
 {
     AddChat(ChatColors.ConnectionBad, "The connection was refused.");
 }
예제 #18
0
파일: User.cs 프로젝트: AlphaBlend/VNet
 private void Dispose(bool disposing)
 {
     if (!this.disposed)
     {
         if (disposing)
         {
             socket = null;
             packet = null;
             bufferer = null;
             Channel = null;
         }
         disposed = true;
     }
 }
예제 #19
0
파일: Socket.cs 프로젝트: AlphaBlend/VNet
 protected void socket_Disconnected(TcpClientWrapper sender)
 {
     AddChat(ChatColors.ConnectionBad, "Disconnected from server.");
 }
예제 #20
0
파일: User.cs 프로젝트: Lynxy/VNet
 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;
     }
 }