public void Run()
        {
            try
            {
                // Compile a list of possibly-readable sockets
                _readableSockets.Clear();
                _readableSockets.Add(_listener.Server);
                _writableSockets.Clear();

                foreach (var client in _clients)
                {
                    _readableSockets.Add(client.Socket.Client);
                    if (client.NeedsToWrite)
                        _writableSockets.Add(client.Socket.Client);
                }

                Socket.Select(_readableSockets, _writableSockets, null, 1000000);

                foreach (var socket in _readableSockets)
                {
                    if (socket == _listener.Server)
                    {
                        // New connection
                        Logger.WriteInternal("[HI!] New connection!");

                        var c = new SocketClient(this, _listener.AcceptTcpClient());

                        _clients.Add(c);
                        _socketMap.Add(c.Socket.Client, c);

                        NewClient(c);
                    }
                    else
                    {
                        // Readable data
                        if (socket.Connected)
                            _socketMap[socket].OnReadable();
                    }
                }

                foreach (var socket in _writableSockets)
                    if (socket.Connected)
                        _socketMap[socket].OnWritable();
            }
            catch (Exception ex)
            {
                Logger.WriteException("A socket error occurred", ex);
            }
        }
Exemple #2
0
        public Client(Server server, SocketClient socket)
        {
            IsClosed = false;
            _server = server;
            Socket = socket;

            socket.DataReceived += HandleDataReceived;
            socket.ConnectionLost += HandleConnectionLost;

            _readBuffer = new byte[1024 * 64];
            _readBufferSize = 0;

            InputArc4 = null;
            OutputArc4 = null;

            var welcome = new PacketWriter();
            welcome.Write((ushort)3);
            welcome.Write((ushort)201);
            welcome.Write((ushort)0);
            welcome.Write((ushort)0);
            SendPacket(0x03, 0x08, 0, welcome.ToArray());
        }
Exemple #3
0
        public Client(Server server, Network.SocketClient socket)
        {
            this.server = server;
            this.socket = socket;

            socket.DataReceived   += HandleDataReceived;
            socket.ConnectionLost += HandleConnectionLost;

            readBuffer     = new byte[1024 * 64];
            readBufferSize = 0;

            inputARC4  = null;
            outputARC4 = null;

            PacketWriter welcome = new PacketWriter();

            welcome.Write((ushort)3);
            welcome.Write((ushort)201);
            welcome.Write((ushort)0);
            welcome.Write((ushort)0);
            SendPacket(0x03, 0x08, 0, welcome.ToArray());
        }
 private void HandleNewClient(SocketClient client)
 {
     var newClient = new Client(this, client);
     Clients.Add(newClient);
 }
        internal void NotifyConnectionClosed(SocketClient client)
        {
            Console.WriteLine("Connection closed");

            _socketMap.Remove(client.Socket.Client);
            _clients.Remove(client);
        }