Beispiel #1
0
    private void SendCallback(IAsyncResult ar)
    {
        SessionParam param = (SessionParam)ar.AsyncState;

        int bytesSent = 0;

        try
        {
            bytesSent = param.socket.EndSend(ar);
        }
        catch (ObjectDisposedException)
        {
            return;
        }
        catch (Exception exception)
        {
            _active = false;
            if (ErrorHandler != null)
            {
                ErrorHandler(this, SessionErrorCode.SendError, exception.Message);
                return;
            }

            throw;
        }
        if (SendHandler != null)
        {
            SendHandler(this, bytesSent, param);
        }
    }
Beispiel #2
0
    private void ConnectCallback(IAsyncResult ar)
    {
        SessionParam param = (SessionParam)ar.AsyncState;

        try
        {
            param.socket.EndConnect(ar);
        }
        catch (ObjectDisposedException)
        {
            return;
        }
        catch (Exception exception)
        {
            _active = false;
            if (ErrorHandler != null)
            {
                ErrorHandler(this, SessionErrorCode.ConnectError, exception.Message);
                return;
            }
            throw;
        }
        _active = true;
        if (ConnectedHandler != null)
        {
            ConnectedHandler(this, param);
        }
        Receive();
    }
Beispiel #3
0
 public void Send(byte[] buffer, int offset, int size)
 {
     if (_socket == null)
     {
         string errorMessage = "session not initialize.";
         if (ErrorHandler != null)
         {
             ErrorHandler(this, SessionErrorCode.StateError, errorMessage);
             return;
         }
         throw new Exception(errorMessage);
     }
     try
     {
         SessionParam param = new SessionParam(_socket, _ipAddress, _port);
         _socket.BeginSend(buffer, offset, size, SocketFlags.None, SendCallback, param);
     }
     catch (Exception exception)
     {
         _active = false;
         if (ErrorHandler != null)
         {
             ErrorHandler(this, SessionErrorCode.StateError, exception.Message);
             return;
         }
         throw;
     }
 }
Beispiel #4
0
 public void Connect(IPAddress ipAddress, int port)
 {
     Initialize(ipAddress.AddressFamily);
     _ipAddress = ipAddress;
     _port      = port;
     if (_socket == null)
     {
         string errorMessage = "initialize failure.";
         if (ErrorHandler != null)
         {
             ErrorHandler(this, SessionErrorCode.StateError, errorMessage);
             return;
         }
         throw new Exception(errorMessage);
     }
     try
     {
         SessionParam param = new SessionParam(_socket, _ipAddress, _port);
         _socket.BeginConnect(_ipAddress, _port, ConnectCallback, param);
     }
     catch (Exception exception)
     {
         if (ErrorHandler != null)
         {
             ErrorHandler(this, SessionErrorCode.StateError, exception.Message);
             return;
         }
         throw;
     }
 }