1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
class Server
{
#region Delegate
private delegate void Method(ConnectionInfo Infos, Object Content);
#endregion
#region Fields
private Int32 Port;
private Socket ServerSocket = null;
private List<ConnectionInfo> Connections = new List<ConnectionInfo>();
private Dictionary<Command, Method> Map = new Dictionary<Command, Method>();
#endregion
#region Constructor
public Server(Int32 Port)
{
this.Port = Port;
this.Map[Command.Server_User_Login] = this.Login;
}
#endregion
#region Methods - Login
private void Login(ConnectionInfo Client, Object Content)
{
Client.Id = (Int32)Content;
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - The client is now authentified as '" + Client.Id + "'.");
}
#endregion
#region Methods - Receive
private void ReceiveCallback(IAsyncResult Result)
{
ConnectionInfo Connection = (ConnectionInfo)Result.AsyncState;
try
{
Int32 BytesRead = Connection.Socket.EndReceive(Result);
if (BytesRead != 0)
{
Byte[] Message = (new MemoryStream(Connection.AsyncBuffer, 0, BytesRead)).ToArray();
Connection.Append(Message);
Packet Packet;
while ((Packet = Connection.TryParseBuffer()) != null)
{
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Command received : " + Packet.Command.ToString());
if (Packet.Recipient == 0)
{
if (this.Map.ContainsKey(Packet.Command))
this.Map[Packet.Command](Connection, Packet.Content);
}
else
{
foreach (ConnectionInfo Item in this.Connections)
{
if (Item.Id == Packet.Recipient)
{
Byte[] Buffer = Packet.Serialize(Connection.Id, Packet.Command, Packet.Content);
this.Send(Item, Buffer);
}
}
}
}
Connection.Socket.BeginReceive(Connection.AsyncBuffer, 0, Connection.AsyncBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), Connection);
}
else
this.CloseConnection(Connection);
}
catch (SocketException E)
{
CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Receive socket exception : " + E.SocketErrorCode);
}
catch (Exception E)
{
CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Receive exception : "+ E.Message);
}
}
#endregion
#region Methods - CloseConnection
private void CloseConnection(ConnectionInfo Connection)
{
Connection.Socket.Close();
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - The user " + Connection.Id + " has been disconnected !");
lock (this.Connections)
{
this.Connections.Remove(Connection);
}
}
#endregion
#region Methods - Send
protected void Send(ConnectionInfoFile Connection, Byte[] Buffer)
{
Connection.Socket.BeginSend(Buffer, 0, Buffer.Length, 0, new AsyncCallback(SendCallback), Connection);
}
private static void SendCallback(IAsyncResult Result)
{
try
{
ConnectionInfo Connection = (ConnectionInfo)Result.AsyncState;
Int32 BytesSent = Connection.Socket.EndSend(Result);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + typeof(Server).Name.Insert(6, " ") + " - Sent " + BytesSent + " bytes " + Connection.Id.ToString() + ".");
}
catch (Exception E)
{
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + typeof(Server).GetType().Name.Insert(6, " ") + " - Exception : " + E.ToString());
}
}
#endregion
#region Methods - Stop
public void Stop()
{
this.ServerSocket.Close();
this.ServerSocket = null;
}
#endregion
#region Methods - Start
public void Start()
{
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Start listening on port " + this.Port + "...");
this.ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.ServerSocket.Bind(new IPEndPoint(IPAddress.Any, this.Port));
this.ServerSocket.Listen((Int32)SocketOptionName.MaxConnections);
for (Int32 i = 0; i < 5; i++)
this.ServerSocket.BeginAccept(new AsyncCallback(this.AcceptCallback), this.ServerSocket);
}
#endregion
#region Methods - Accept
private void AcceptCallback(IAsyncResult Result)
{
ConnectionInfo Connection = new ConnectionInfo();
Socket Socket = (Socket)Result.AsyncState;
try
{
Connection.Socket = Socket.EndAccept(Result);
Connection.ConnectionDate = DateTime.Now;
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - A new client has been registered");
lock (this.Connections)
{
this.Connections.Add(Connection);
}
Connection.Socket.BeginReceive(Connection.AsyncBuffer, 0, Connection.AsyncBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), Connection);
}
catch (SocketException E)
{
this.CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Accept socket exception: " + E.SocketErrorCode);
}
catch (Exception E)
{
this.CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Accept exception: " + E.Message);
}
Socket.BeginAccept(new AsyncCallback(AcceptCallback), Result.AsyncState);
}
#endregion
} |
Partager