Thread: [508] RSPS C# Rework

Results 1 to 5 of 5
  1. #1 [508] RSPS C# Rework 
    Registered Member
    Join Date
    Jan 2016
    Age
    29
    Posts
    27
    Thanks given
    0
    Thanks received
    3
    Rep Power
    13
    R-S Admin Response  Introduction
    Hello, this is not anything to get overly excited about nor is it me showing off the next best thing. This is simply enhancing on the previous editions of a RSPS C# Framework to look at stability along with understanding the concept of RSPS through the idea of converting from one language to another.
    I am simply giving a snippet of what I have done with this current project.


    R-S Admin Response  Features

    • Server / Client Communication
    • Player Saving / Loading
    • Connection Logging
    • Item Dropping
    • Wearable Equipment
    • Multiple Connections



    R-S Admin Response  Changes

    • Changed SocketListener.cs to allow multiple connections at once.
    • Missing packets have been causing issues,



    R-S Admin Response  Code
    Will add snippets of other code segments later, once I finish doing some adjustment to a lot of classes.

    SocketListener.cs
    [SPOIL]
    Code:
    using Server.Net.Connection;
    using Server.Util;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Server.Net
    {
        public class SocketListener : ConnectionManager
        {
            private bool serverRunning = true;
    
            public ConnectionHandler handler { get; private set; }
    
            private string[] bannedHosts = new string[500];
    
            public SocketListener(ushort port)
            {
                // Server details
                handler = new ConnectionHandler("127.0.0.1", port, 200);
                Misc.WriteLine("Starting server on port: " + port);
                LoadBannedHosts();
            }
            public void run()
            {
                handler.Listener.Start(true, true);
                while (serverRunning)
                {
                    TcpClient client = handler.Listener.listener.AcceptTcpClient();
    
                    ThreadPool.QueueUserWorkItem(new WaitCallback(CreateClient), client);
                }
            }
    
            private void CreateClient(object tcpClientObject)
            {
                TcpClient tClient = (TcpClient)tcpClientObject;
    
                Client client = null;
                uint id = (uint)GetFreeId();
                client = new Client(id, tClient.Client);
                client.Start();
                AppendConnection(client.IPAddress);
                if (CheckBanned(client.IPAddress))
                {
                    handler.ConnectionClosed(client);
                    return;
                }
                clients[id] = client;
                Server.GetSingleton().engine.AddConnection(client, (int)id);
                Server.GetSingleton().engine.CurrentPlayers++;
                handler.Listener.LastSocket = null;
            }
    
            public void AppendConnection(string host)
            {
                Server.GetSingleton().engine.fileManager.AppendData("connections/" + host + ".txt", "[" + Misc.GetDate() + "] " + host + ": connection recieved.");
            }
    
            public bool CheckBanned(string hostname)
            {
                if (hostname == null)
                    return true;
                for (int i = 0; i < bannedHosts.Length; i++)
                {
                    if (bannedHosts[i] != null && (hostname.StartsWith(bannedHosts[i]) || hostname.Equals(bannedHosts[i])))
                        return true;
                }
                return false;
            }
    
            private void LoadBannedHosts()
            {
                int index = 0;
                try
                {
                    StreamReader input = new StreamReader("data/banned/bannedhosts.dat");
                    string loggedIPS = null;
                    while ((loggedIPS = input.ReadLine()) != null)
                    {
                        bannedHosts[index] = loggedIPS;
                        index++;
                    }
                } catch (Exception e)
                {
                    Misc.WriteLine("Error loading banned hosts list.");
                }
            }
        }
    }
    [/SPOIL]
    Client.cs
    [SPOIL]
    Code:
    using Server.Players;
    using Server.Util;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Server.Net.Connection
    {
        public delegate void DataRouteEventHandler(byte[] data);
    
        public delegate void DisconnectEventHandler();
    
        public class Client
        {
    
            public uint Id { get; private set; }
    
            public Socket Socket { get; private set; }
    
            public NetworkStream stream { get; set; }
    
            public DateTime CreateTime { get; private set; }
    
            public DateTime LastDataRecievedOn { get; private set; }
    
            public Client(uint id, Socket socket)
            {
                this.Id = id;
                this.Socket = socket;
                this.CreateTime = DateTime.Now;
                this.LastDataRecievedOn = DateTime.Now;
            }
    
            public void Start()
            {
                stream = new NetworkStream(this.Socket);
            }
    
            public void Write(byte b)
            {
                if (Socket == null)
                    return;
                stream.WriteByte(b);
            }
    
            public void Write(Player p, byte[] data, int offset, int length)
            {
                if (Socket == null)
                    return;
                this.stream.Write(data, offset, length);
            }
    
            public void WriteBuffer(Player p)
            {
                if (p == null || !p.Online || p.Disconnected[0])
                    return;
                try
                {
                    if (p.Stream.OutOffset > 0)
                        stream.Write(p.Stream.OutBuffer, 0, p.Stream.OutOffset);
                    p.Stream.OutOffset = 0;
                    stream.Flush();
                }
                catch (Exception e)
                {
                    p.Disconnected[0] = true;
                }
            }
    
            public int Read(Player p)
            {
                if (Socket == null || Socket.Available < 1)
                    return -1;
                return stream.Read(p.Stream.InBuffer, p.Stream.InOffset, p.Stream.InBuffer.Length);
            }
    
            public int Read(Player p, int read)
            {
                if (Socket == null)
                    return -1;
                return stream.Read(p.Stream.InBuffer, 0, read);
            }
    
            public int Avail()
            {
                if (stream == null)
                    return 0;
                return Socket.Available;
            }
    
            public void Dispose()
            {
                Dispose(true);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                    if (this.Socket != null)
                        this.Socket.Close(0);
            }
    
            public TimeSpan Age
            {
                get
                {
                    TimeSpan span = DateTime.Now - this.CreateTime;
                    if (span <= TimeSpan.Zero)
                        return TimeSpan.Zero;
                    return span;
                }
            }
    
            public string IPAddress
            {
                get
                {
                    try
                    {
                        return this.Socket.RemoteEndPoint.ToString().Split(':')[0];
                    }
                    catch (Exception)
                    {
                        return "";
                    }
                }
            }
    
            public bool Connected
            {
                get { return this.Socket.Connected; }
            }
        }
    }
    [/SPOIL]


    R-S Admin Response  Media





    R-S Admin Response  Credits
    I based the whole project of an old topic I found just to get started and so that progress would not be hindered by code which I was not capable of converting to c#.

    Server, Client and Source - Myuria
    Last edited by chrishayes94; 03-25-2016 at 05:27 PM. Reason: Added new media showing change in connection
    Reply With Quote  
     

  2. #2  
    Bossman

    ISAI's Avatar
    Join Date
    Sep 2012
    Posts
    1,916
    Thanks given
    655
    Thanks received
    1,366
    Rep Power
    5000
    Good luck with this, very excited to see where it goes! I have quite a large knowledge in C# so if you ever need any help or want to work with someone else, PM me.
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Jan 2016
    Age
    29
    Posts
    27
    Thanks given
    0
    Thanks received
    3
    Rep Power
    13
    Quote Originally Posted by Belz View Post
    Best of luck, I too was working on a C# RSPS at one point, and I do have to say I enjoy the language much more than Java. You should look into using the asynchronous methods for handling network connections, as your current implementation won't scale very well. One of my favourite C# features is LINQ (think SQL for C#), and delegates. If you're looking to further you knowledge about C# you should definitely take a look at those. Feel free to private message me if you have any issues while learning the language, I'd be happy to help
    Thanks I started using Asynchronous for handling new connections to the server as currently it will begin to freeze as it believes that it there is still something to accept. Thank you, I'll look into LINQ, I was going to use the standard built in SQL libraries but I've had problems with them in past when doing university assignments.

    Quote Originally Posted by ISAI View Post
    Good luck with this, very excited to see where it goes! I have quite a large knowledge in C# so if you ever need any help or want to work with someone else, PM me.
    Thank you for the response and I'll bare that in mind, is simply now just finding the errors which are not writing the bytes for sending the chatbox interface and updating the characters current GFX State.
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Jan 2016
    Age
    29
    Posts
    27
    Thanks given
    0
    Thanks received
    3
    Rep Power
    13
    Quote Originally Posted by Belz View Post
    I think you misunderstood, LINQ is for the actual language and interacting with objects, not a database. It's designed like embedded SQL to offer a more functional style of programming.
    Sorry I misunderstood, I'll look into once I begin to look towards database integration.
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Jan 2016
    Age
    29
    Posts
    27
    Thanks given
    0
    Thanks received
    3
    Rep Power
    13
    *BUMP*
    Just a little update to the main thread to finish off this snippet, I've rewrote the way that the server will accept new clients so that it keeps the connection open and allows multiple clients at the same time. Not much of a snippet so far as not actual game play is present however a stable connection between both the clients and the server can be created.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. How Do I Run 508+ Rsps On Mac?
    By NickMx in forum Help
    Replies: 1
    Last Post: 06-12-2012, 01:35 AM
  2. Cheap 508 RSPS HOST
    By Danielson183 in forum Help
    Replies: 1
    Last Post: 12-01-2011, 08:28 PM
  3. Replies: 13
    Last Post: 03-08-2011, 02:32 AM
  4. AngelzPk 508 RSPS Server (Cool Features)(Beta)
    By shadowfang1 in forum Advertise
    Replies: 4
    Last Post: 12-25-2009, 02:33 AM
  5. 508+ RSPS section
    By Nathan in forum Suggestions
    Replies: 9
    Last Post: 06-25-2009, 03:26 PM
Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •