haha I had not noticed it :P anyway I will test soon
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();
}
}
}
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.Quote:
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:
Locks can be used when you know that an action could take some time (to write some lines) and it can be called concurrently.Quote:
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.
You gotta do something horribly wrong that this happens. I've never had it happened that the streamio got corrupted.Quote:
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->
Quote:
You can't change the colour in the same string. (or atleast I think so)
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.
}
}
}
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).Quote:
Oh look. 2 less stream writings.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. } } }
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.Quote:
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).
[Only registered and activated users can see links. Click Here To Register...]Quote:
@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.