Пример #1
0
 public bool Send(string message, xConnection conn)
 {
     if (conn != null && conn.socket.Connected)
     {
         lock (conn.socket)
         {
             conn.socket.Send(Encoding.UTF8.GetBytes(message), message.Length, SocketFlags.None);
         }
     }
     else
     {
         return(false);
     }
     return(true);
 }
Пример #2
0
 public void SetAuth(bool accepted, xConnection conn)
 {
     if (accepted)
     {
         Console.WriteLine("[SRV] Sending auth True");
         Send("NinjaAck", conn);
         ClientConnected();
         conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), conn);
     }
     else
     {
         Console.WriteLine("[SRV] Sending auth false");
         Send("NinjaNACK", conn);
         conn.socket.Close();
         _sockets.Remove(conn);
     }
 }
Пример #3
0
        private void OnClientConnect(IAsyncResult result)
        {
            xConnection conn = new xConnection();

            try
            {
                //Finish accepting the connection
                Socket s = (Socket)result.AsyncState;
                conn        = new xConnection();
                conn.socket = s.EndAccept(result);
                conn.buffer = new byte[_bufferSize];
                lock (_sockets)
                {
                    _sockets.Add(conn);
                }


                // if (ClientConnected != null)
                //   ClientConnected();

                Console.WriteLine("[SRV] client connected");

                //Queue recieving of data from the connection
                conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), conn);
                //Queue the accept of the next incomming connection
                _serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), _serverSocket);
            }
            catch (Exception e)
            {
                if (conn.socket != null)
                {
                    conn.socket.Close();
                    lock (_sockets)
                    {
                        _sockets.Remove(conn);
                    }
                }
                //Queue the next accept, think this should be here, stop attacks based on killing the waiting listeners
                _serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), _serverSocket);
            }
        }
Пример #4
0
        private void ReceiveData(IAsyncResult result)
        {
            xConnection conn = (xConnection)result.AsyncState;

            try
            {
                //Grab our buffer and count the number of bytes receives
                int bytesRead = conn.socket.EndReceive(result);
                //make sure we've read something, if we haven't it supposadly means that the client disconnected
                if (bytesRead > 0)
                {
                    string message = Encoding.UTF8.GetString(conn.buffer, 0, bytesRead);

                    Console.WriteLine(DateTime.Now.ToShortTimeString() + " [SRV] message received: " + message);
                    if (message.Contains("NinjaConnect")) //it's a message from the client who knows my ip
                    {
                        Send("NinjaAck", conn);

                        ClientConnected();
                        //Queue the next receive
                        conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), conn);
                    }
                    else if (message.Contains("NinjaTestConnect"))  //the client first connection request
                    {
                        //we need to ask permission
                        string address = conn.socket.RemoteEndPoint.ToString();
                        address = address.Remove(address.IndexOf(':'));
                        //this is to check if the client is in the authorized clients list
                        if (_authorizedClients != null && _authorizedClients.Where(cl => cl.IPaddress.Contains(address)).FirstOrDefault() != null)
                        {
                            SetAuth(true, conn);
                        }
                        else
                        {
                            OnClientAskForConnection(conn);
                        }
                    }
                    else if (message.Contains("NinjaKey="))
                    {
                        string key = message.Replace("NinjaKey=", "");

                        if (OnMessageReceived != null)
                        {
                            OnMessageReceived(key);
                        }

                        conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), conn);
                    }
                    else
                    {
                        //message from someone else
                        conn.socket.Close();
                        _sockets.Remove(conn);
                    }
                }
                else
                {
                    //Callback run but no data, close the connection
                    //supposadly means a disconnect
                    //and we still have to close the socket, even though we throw the event later
                    conn.socket.Close();
                    lock (_sockets)
                    {
                        _sockets.Remove(conn);
                    }
                    if (ClientDisconnected != null)
                    {
                        ClientDisconnected();
                    }
                }
            }
            catch (SocketException e)
            {
                //Something went terribly wrong
                //which shouldn't have happened
                if (conn.socket != null)
                {
                    conn.socket.Close();
                    lock (_sockets)
                    {
                        _sockets.Remove(conn);
                    }
                }
            }
        }