[Tutorial] Netty Based Server

08/14/2016 20:46 -=Flavio=-#31
haha I had not noticed it :P anyway I will test soon
08/14/2016 23:25 mustafagrlyn#32
Hello :)

Packet.cs -> Send(Commands.StringPacket("")); ^_^


I Love You Shock <3

Netty Emulator: Shock[DEV]
Edited and Updated: ΉΛDΣS_PЯO[DΣV]

NOTE: This emulator (NeetyBase) created by Shock and edited by ΉΛDΣS_PЯO[DΣV].


Code:
 
 
  using NettyBase.Net

namespace Server
{

    static class Commands
    {
        public static byte[] StringPacket(string n)
        {
            ByteArray enc = new ByteArray(29052);
            enc.UTF(n);
            return enc.ToByteArray();
        }
    }
}
08/15/2016 00:50 ItsTequila#33
To the one who is trying his emulator, I'm very sorry maps.php got fixed.
08/15/2016 20:33 olitis1#34
You should use locks to make sure that your code won't be doing ugly things when an action is called simultaneously and the action before hasn't finished.
For example in Utils.Out.cs it should be like:
08/15/2016 22:41 Requi#35
Quote:
Originally Posted by olitis1 View Post
You should use locks to make sure that your code won't be doing ugly things when an action is called simultaneously and the action before hasn't finished.
For example in Utils.Out.cs it should be like:
As far as I know, locks are only necessary when using lists/dictionaries/arrays which could be changed while they are being used. I might be wrong though.
08/15/2016 23:09 olitis1#36
Quote:
Originally Posted by Requi View Post
As far as I know, locks are only necessary when using lists/dictionaries/arrays which could be changed while they are being used. I might be wrong though.
Locks can be used when you know that an action could take some time (to write some lines) and it can be called concurrently.

While the action is performing some text color changes and writing, the action is again called.
With a lock, you make sure that the lines will be full written (thread-safe).

That's a possible scenario:
Without a Lock ->
With a Lock->
08/16/2016 11:35 Requi#37
Quote:
Originally Posted by olitis1 View Post
Locks can be used when you know that an action could take some time (to write some lines) and it can be called concurrently.

While the action is performing some text color changes and writing, the action is again called.
With a lock, you make sure that the lines will be full written (thread-safe).

That's a possible scenario:
Without a Lock ->
With a Lock->
You gotta do something horribly wrong that this happens. I've never had it happened that the streamio got corrupted.
08/16/2016 12:29 olitis1#38
Quote:
Originally Posted by Requi View Post
You gotta do something horribly wrong that this happens. I've never had it happened that the streamio got corrupted.
It's not writting 1 time, it's writting 3 times (to change colour and add header and that...) for each single line.
08/16/2016 12:30 Requi#39
Quote:
Originally Posted by olitis1 View Post
It's not writting 1 time, it's writting 3 times for each single line.
then it would be better to concat the string and then write it, instead of abusing the stream so much.
08/16/2016 12:32 olitis1#40
Quote:
Originally Posted by Requi View Post
then it would be better to concat the string and then write it, instead of abusing the stream so much.
You can't change the colour in the same string. (or atleast I think so)
08/16/2016 12:37 Requi#41
Quote:
Originally Posted by olitis1 View Post
You can't change the colour in the same string. (or atleast I think so)
Code:
using System;

namespace NettyBase
{
	class Out
	{
		/// <summary>
		/// That should replace the Console.WriteLine with a little more ordered version.
		/// </summary>
		/// <param name="message">That's where the input text goes</param>
		/// <param name="header">This parameter is optional and it stands for the [header] before the text</param>
		/// <param name="color">This parameter is optional and it is chosing the color you would like the text to be</param>
		public static void WriteLine(string message, string header = "", ConsoleColor color = ConsoleColor.Gray)
		{
			Console.ForegroundColor = ConsoleColor.Gray;
			Console.Write("[" + DateTime.Now + "]");
			Console.ForegroundColor = ConsoleColor.DarkGray;
			header = header == "" ? header : "[" + header + "]";
			Console.Write(header);
			Console.ForegroundColor = ConsoleColor.Gray;
			Console.Write(" - ");
			Console.ForegroundColor = color;
			Console.WriteLine(message);
			//Console.ForegroundColor = ConsoleColor.Gray; // u'll reset the color at the start anyways.
		}
	}
}
Oh look. 2 less stream writings.
08/16/2016 12:39 th0rex#42
Quote:
Originally Posted by Requi View Post
Code:
using System;

namespace NettyBase
{
    class Out
    {
        private static object WriteLock = new object();
        /// <summary>
        /// That should replace the Console.WriteLine with a little more ordered version.
        /// </summary>
        /// <param name="message">That's where the input text goes</param>
        /// <param name="header">This parameter is optional and it stands for the [header] before the text</param>
        /// <param name="color">This parameter is optional and it is chosing the color you would like the text to be</param>
        public static void WriteLine(string message, string header = "", ConsoleColor color = ConsoleColor.Gray)
        {
			Console.ForegroundColor = ConsoleColor.Gray;
			Console.Write("[" + DateTime.Now + "]");
			Console.ForegroundColor = ConsoleColor.DarkGray;
			header = header == "" ? header : "[" + header + "]";
			Console.Write(header);
			Console.ForegroundColor = ConsoleColor.Gray;
			Console.Write(" - ");
			Console.ForegroundColor = color;
			Console.WriteLine(message);
			//Console.ForegroundColor = ConsoleColor.Gray; // u'll reset the color at the start anyways.
        }
    }
}
Oh look. 2 less stream writings.
That code is still not thread safe and locks are indeed needed if your application uses more than one thread (which it will if it's a server, because single threaded servers are slow as fuck).
08/16/2016 12:40 Requi#43
Quote:
Originally Posted by C0untLizzi View Post
That code is still not thread safe and locks are indeed needed if your application uses more than one thread (which it will if it's a server, because single threaded servers are slow as fuck).
And if you want a fast server which is threadsafe then don't write every fucking bullshit in the console. Try to prevent using the console that much.
08/17/2016 05:15 ItsTequila#44
@olitis Thank you a lot for the lock I was wondering why when I start the chat server's task it writes on top of itself.

Is anyone able to make packet / socket redirect code on PHP in order to get all the packets from the emulator and handle them there instead of inventory.php.
08/17/2016 08:45 manulaiko3.0#45
Quote:
Originally Posted by NUMANDERBUHMAN View Post
@olitis Thank you a lot for the lock I was wondering why when I start the chat server's task it writes on top of itself.

Is anyone able to make packet / socket redirect code on PHP in order to get all the packets from the emulator and handle them there instead of inventory.php.
[Only registered and activated users can see links. Click Here To Register...]

But I'll move it to [Only registered and activated users can see links. Click Here To Register...] along this week...