/// <summary>
        /// Listens for packets from a server and manages the client's connection status.
        /// </summary>
        private void Handle()
        {
            running = true;
            if ( ConnectedEvent != null ) ConnectedEvent();

            Stopwatch watch = new Stopwatch();
            watch.Start();

            while( Connected && Status ) {
                if ( watch.ElapsedMilliseconds >= Timeout ) {
                    if ( AttemptReconnectEvent != null ) AttemptReconnectEvent();
                    Connected = Receiver.Connected;
                }

                if ( Stream.DataAvailable ) {
                    int packet = Receiver.Available;
                    BufferStream buffer = new BufferStream( packet, 1 );
                    Stream.Read( buffer.Memory, 0, packet );
                    if ( ReceivedEvent != null ) ReceivedEvent( buffer );
                }

                watch.Reset();
            }

            running = false;
            Status = false;
            if ( DisconnectedEvent != null ) DisconnectedEvent();
            Close();
        }
예제 #2
0
        /// <summary>
        /// Listens for packets from a client and manages that client's connection status.
        /// </summary>
        /// <param name="client"></param>
        private void Handle( TcpClientHandler client )
        {
            if ( ConnectedEvent != null ) ConnectedEvent( client );

            Stopwatch watch = new Stopwatch();
            watch.Start();

            while( client.Connected ) {
                if ( watch.ElapsedMilliseconds >= client.Timeout ) {
                    if ( AttemptReconnectEvent != null ) AttemptReconnectEvent( client );
                    client.Connected = client.Receiver.Connected;
                }

                if ( client.Stream.DataAvailable ) {
                    int packet = client.Receiver.Available;
                    BufferStream buffer = new BufferStream( packet, 1 );
                    client.Stream.Read( buffer.Memory, 0, packet );
                    if ( ReceivedEvent != null ) ReceivedEvent( client, buffer );
                }

                watch.Reset();
            }

            if ( DisconnectedEvent != null ) DisconnectedEvent( client );
            client.Close();
            ClientMap.Remove(client.Socket);
        }
예제 #3
0
 /// <summary>
 /// Asynchronously sends a buffer(datagram) through the specified UDP client.
 /// </summary>
 /// <param name="client">UDP client to send the datagram through.</param>
 /// <param name="buffer">Buffer that contains the datagram to be sent.</param>
 /// <param name="endPoint">IPEndpoint(address/port) to send the datagram too.</param>
 /// <exception cref="System.ArugmentNullException"/>
 /// <exception cref="System.InvalidOperationException"/>
 /// <exception cref="System.ObjectDisposedException"/>
 /// <exception cref="System.Net.Sockets.SocketException"/>
 public static async void SendAsync( UdpClient client, BufferStream buffer, IPEndPoint endPoint ) {
     await client.SendAsync( buffer.Memory, buffer.Iterator, endPoint );
 }
예제 #4
0
 /// <summary>
 /// Synchonously sends a buffer(datagram) through the specified UDP client.
 /// </summary>
 /// <param name="client">UDP client to send the datagram through.</param>
 /// <param name="buffer">Buffer that contains the datagram to be sent.</param>
 /// <param name="endPoint">IPEndpoint(address/port) to send the datagram too.</param>
 /// <exception cref="System.ArugmentNullException"/>
 /// <exception cref="System.InvalidOperationException"/>
 /// <exception cref="System.ObjectDisposedException"/>
 /// <exception cref="System.Net.Sockets.SocketException"/>
 public static void SendSync( UdpClient client, BufferStream buffer, IPEndPoint endPoint ) {
     client.Send( buffer.Memory, buffer.Iterator, endPoint );
 }
예제 #5
0
 /// <summary>
 /// Asynchronously sends a buffer(packet) through the specified stream.
 /// </summary>
 /// <param name="stream">The particular stream of a TCP client to send through.</param>
 /// <param name="buffer">Buffer containing the packet to be sent.</param>
 /// <exception cref="System.ArugmentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.ObjectDisposedException"/>
 public static async void SendAsync( NetworkStream stream, BufferStream buffer ) {
     stream.Write( buffer.Memory, 0 , buffer.Iterator );
     await stream.FlushAsync();
 }
예제 #6
0
 /// <summary>
 /// Synchronously sends a buffer(packet) through the specified stream.
 /// </summary>
 /// <param name="stream">The particular stream of a TCP client to send through.</param>
 /// <param name="buffer">Buffer containing the packet to be sent.</param>
 /// <exception cref="System.ArugmentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.ObjectDisposedException"/>
 public static void SendSync( NetworkStream stream, BufferStream buffer ) {
     stream.Write( buffer.Memory, 0 , buffer.Iterator );
     stream.Flush();
 }
예제 #7
0
 /// <summary>
 /// Creates a copy of this buffer and all it's contents.
 /// </summary>
 /// <returns>A new clone BufferStream.</returns>
 /// <exception cref="System.ArgumentNullException"/>
 public BufferStream CloneBufferStream()
 {
     BufferStream clone = new BufferStream( memory.Length, alignment );
     Array.Copy( memory, clone.Memory, memory.Length );
     clone.iterator = iterator;
     return clone;
 }
예제 #8
0
 /// <summary>
 /// Copies the entire contents of this buffer to the destination buffer.
 /// </summary>
 /// <param name="destBuffer">Buffer to copy the contents of this buffer too.</param>
 public void BlockCopy( BufferStream destBuffer )
 {
     int length = ( destBuffer.Memory.Length > memory.Length ) ? memory.Length : destBuffer.Memory.Length;
     Array.Copy( memory, destBuffer.Memory, length );
 }
예제 #9
0
 /// <summary>
 /// Copes the specified number of bytes from the start of this buffer to the start of the destination buffer.
 /// </summary>
 /// <param name="destBuffer">Buffer to copy the contents of this buffer too.</param>
 /// <param name="length">Number of bytes to copy from this buffer to the destination buffer.</param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 /// <exception cref="System.ArgumentException"/>
 public void BlockCopy( BufferStream destBuffer, int length )
 {
     Array.Copy( memory, destBuffer.Memory, length );
 }
예제 #10
0
 /// <summary>
 /// Copes the specified number of bytes from this buffer to the destination buffer, given the shared start position for both buffers.
 /// </summary>
 /// <param name="destBuffer">Buffer to copy the contents of this buffer too.</param>
 /// <param name="startIndex">Shared start index of both buffers to start copying data from/to.</param>
 /// <param name="length">Number of bytes to copy from this buffer to the destination buffer.</param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 /// <exception cref="System.ArgumentException"/>
 public void BlockCopy( BufferStream destBuffer, int startIndex, int length )
 {
     Array.Copy( memory, startIndex, destBuffer.Memory, startIndex, length );
 }
예제 #11
0
        /// <summary>
        /// Handles incoming datagrams.
        /// </summary>
        private void Handle()
        {
            running = true;
            StartedEvent( this );

            while( Status ) {
                if ( Listener.Available > 0 ) {
                    IPEndPoint endPoint = null;

                    byte[] datagram = Listener.Receive( ref endPoint );
                    int length = datagram.Length;

                    BufferStream readBuffer = new BufferStream( length, Alignment );
                    System.Array.Copy( datagram, 0, readBuffer.Memory, 0, length );
                    ThreadPool.QueueUserWorkItem( thread => ReceivedEvent( this, readBuffer, endPoint ) );
                }
            }

            ClosedEvent( this );
            running = false;
            Close();
        }