Example #1
0
 /// <summary>
 /// Handles a new client connecting to the server and starts reading from the client.
 /// </summary>
 /// <param name="client">The new client.</param>
 private void Heard(TcpClient client)
 {
     Connection connection = new Connection {
         Client = client,
     };
     lock(clients_l) {
         if(capacity != 0 && clients >= capacity)
             throw new Exceptions.ServerFullException("Server full, rejecting client with IP '" + connection.Address + "'.", connection);
         clients++;
     }
     if(timeout != 0) {
         connection.Timer.Interval = timeout;
         connection.Timer.Elapsed += TimeoutEventHandler;
         connection.Timer.Start();
     }
     Connected(connection);
     BeginRead(connection);
 }
Example #2
0
 /// <summary>
 /// Raises the on receive event.
 /// </summary>
 /// <param name="client">Client.</param>
 /// <param name="packet">Packet.</param>
 private void Received(Connection client, string packet)
 {
     if(OnReceive != null) OnReceive(client, packet);
 }
Example #3
0
 /// <summary>
 /// Handles a client disconnect
 /// </summary>
 /// <param name="connection">Disconnected connection.</param>
 private void DisconnectHandler(Connection connection)
 {
     lock(clients_l) clients--;
     connection.Timer.Stop();
     Disconnected(connection);
     connection.Timer.Close();
     connection.Client.Close();
 }
Example #4
0
 /// <summary>
 /// Ends reading from a client.
 /// </summary>
 /// <returns>The read.</returns>
 /// <param name="connection">The connection to end reading from.</param>
 /// <param name="result">Asynchronous result.</param>
 private int EndRead(Connection connection, System.IAsyncResult result)
 {
     try {
         lock(connection.SyncRoot)
             if(connection.Connected)
                 return connection.Client.GetStream().EndRead(result);
             else
                 return 0;
     }catch(System.IO.IOException) {
         return 0;
     }
 }
Example #5
0
 /// <summary>
 /// Raises the OnDisconnect event.
 /// </summary>
 /// <param name="client">Client.</param>
 private void Disconnected(Connection client)
 {
     if(OnDisconnect != null) OnDisconnect(client);
 }
Example #6
0
 /// <summary>
 /// Begins reading from a connected client.
 /// </summary>
 /// <param name="connection">The connection to read from.</param>
 private void BeginRead(Connection connection)
 {
     try {
         connection.Bytes = new byte[1024];
         lock(connection.SyncRoot)
             if(connection.Connected)
                 connection.Client.GetStream().BeginRead(connection.Bytes, 0, 1024, ReceiveCallback, connection);
             else
                 DisconnectHandler(connection);
     }catch(System.IO.IOException) {
         DisconnectHandler(connection);
     }
 }
Example #7
0
 /// <summary>
 /// Writes data to a client.
 /// </summary>
 /// <returns><c>true</c>, if data could be written, <c>false</c> otherwise.</returns>
 /// <param name="connection">The connection to write to.</param>
 /// <param name="data">The data to write.</param>
 public bool WriteData(Connection connection, string data)
 {
     return WriteData(connection, data, true);
 }
Example #8
0
 /// <summary>
 /// Writes data to a client.
 /// </summary>
 /// <returns><c>true</c>, if data could be written, <c>false</c> otherwise.</returns>
 /// <param name="connection">The connection to write to.</param>
 /// <param name="data">The data to write.</param>
 /// <param name="isAsync">If set to <c>true</c>, the write will be performed asynchronously.</param>
 public bool WriteData(Connection connection, string data, bool isAsync)
 {
     try {
         byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
         lock(connection.SyncRoot) {
             if(connection.Connected) {
                 if(isAsync) {
                     connection.Client.GetStream().BeginWrite(bytes, 0, data.Length, WriteCallback, connection);
                 }else{
                     connection.Client.GetStream().Write(bytes, 0, data.Length);
                 }
                 return connection.Client.GetStream().CanWrite;
             }else{
                 return false;
             }
         }
     }catch(System.IO.IOException ex) {
         Log("Could not write to client: " + ex.Message + ".", Logging.LogLevel.Error);
         return false;
     }
 }
Example #9
0
 /// <summary>
 /// Disconnects the connection from the server.
 /// </summary>
 /// <param name="connection">The connection to disconnect.</param>
 public void DisconnectClient(Connection connection)
 {
     try {
         lock(connection.SyncRoot)
             if(connection.Connected) {
                 connection.Client.Client.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                 connection.Client.Close();
             }
     }catch(System.Exception e) {
         Log("Could not disconnect socket: " + e.Message, Asterion.Logging.LogLevel.Error);
     }
 }