public override void OnConnected(ConnectionEventArgs e) { connection = e.Connection; connection.BeginReceive(); if (Connected != null) { Connected(e.Connection); } ConnectEvent.Set(); }
public void Start() { if (!_socket.Connected) { throw new Exception("The socket must be connected before starting the SocketConnectionController"); } _packetProtocol = new PacketProtocol <TMessageType>(_bufferSize); _packetProtocol.MessageArrived = (MessageType, data) => OnMessageReceived(MessageType, data); IsConnected = true; byte[] buffer = new byte[_bufferSize]; _socket.BeginReceive(buffer, 0, _bufferSize, 0, new AsyncCallback(ReceiveCallback), buffer); // Start sending KeepAlive packages to check if the connection is still open _keepAliveTimer = new System.Timers.Timer(); _keepAliveTimer.Interval = KEEP_ALIVE_TIMER_INTERVAL; _keepAliveTimer.Elapsed += new ElapsedEventHandler(KeepAliveCallback); _keepAliveTimer.Enabled = true; }