protected virtual void Dispose(bool disposing) { if (disposing) { // dispose managed resources if (sSocket != null) { sSocket.Dispose(); sSocket = null; } } }
/// <summary> /// A ClientManager is responsible for managing a connection from a client. /// For each client connected there will be one separate ClientManager object. /// The socket used by the ClientManager uses asynchronous call to Receive and /// send data. /// </summary> public ClientManager(ref Socket clientSocket) { if (clientSocket == null) throw new Exception("Client socket is not initialized!"); // the endpoint stores information from the client side socket = clientSocket; endPoint = (IPEndPoint)socket.RemoteEndPoint; SockUtils.SetKeepAlive(socket, 600 * 1000, 60 * 1000); sSocket = new SSocket(ref socket); sSocket.Received += new EventHandler(OnChatSocketReceived); sSocket.Disconnected += new EventHandler(OnChatSocketDisconnected); sSocket.Receive(); m_PingTimer = new System.Threading.Timer(m_PingTimerCallback); }
/// <summary> /// Disconnect from the server. /// Events fired: /// Disconnected /// </summary> public void Disconnect() { if (sSocket != null) { sSocket.Received -= new EventHandler(OnChatSocketReceived); sSocket.Disconnected -= new EventHandler(OnChatSocketDisconnected); sSocket.Close(); sSocket.Dispose(); sSocket = null; } Trace.WriteLine("[OETS.Client] Disconnect"); if (Disconnected != null) Disconnected(this, new TimedEventArgs()); }
private void OnChatSocketReceived(object sender, EventArgs e) { SSocket cs = sender as SSocket; if (cs == null) return; if (CommandReceived != null) { SSocket chatSocketCopy = new SSocket(cs); CommandReceived(this, new SSEventArgs(chatSocketCopy)); } switch (cs.Command) { case OpcoDes.SMSG_SERVER_DISCONNECTED: Disconnect(); if (ServerDisconnected != null) ServerDisconnected(this, new TimedEventArgs()); break; default: //sSocket.Receive(); break; } }
/// <summary> /// Ends the pending connection request to the server and requests login. /// </summary> private void OnConnect(IAsyncResult ar) { try { Socket socket = (Socket)ar.AsyncState; socket.EndConnect(ar); // send keep alive after 10 minute of inactivity SockUtils.SetKeepAlive(socket, 600 * 1000, 60 * 1000); // the socket was connected, create the ChatSocket used for communication this.sSocket = new SSocket(ref socket); this.sSocket.Received += new EventHandler(OnChatSocketReceived); this.sSocket.Disconnected += new EventHandler(OnChatSocketDisconnected); // set the connected time this.dtConnectedOn = DateTime.Now; // fire the Connected event if (Connected != null) Connected(this, new TimedEventArgs()); sSocket.Receive(); SendRequestLogin(); } catch (Exception exc) { if (ConnectFailed != null) ConnectFailed(this, new TimedEventArgs()); if (!SockUtils.HandleSocketError(exc)) throw exc; } }
private void ProcessConnect(SocketAsyncEventArgs args) { try { Socket socket = args.AcceptSocket; if (socket == null || !socket.Connected) { if (ConnectFailed != null) ConnectFailed(this, new TimedEventArgs()); return; } // send keep alive after 10 minute of inactivity SockUtils.SetKeepAlive(socket, 600 * 1000, 60 * 1000); // the socket was connected, create the ChatSocket used for communication this.sSocket = new SSocket(ref socket); this.sSocket.Received += new EventHandler(OnChatSocketReceived); this.sSocket.Disconnected += new EventHandler(OnChatSocketDisconnected); // set the connected time this.dtConnectedOn = DateTime.Now; // fire the Connected event if (Connected != null) Connected(this, new TimedEventArgs()); sSocket.Receive(); SendRequestLogin(); } catch { if (ConnectFailed != null) ConnectFailed(this, new TimedEventArgs()); } }
/// <summary> /// Deep clone of a ChatSocket object without the network stream cloned. /// It is used when a new ChatSocket object is passed on to methods, events that /// do not need a network stream. /// </summary> public SSocket(SSocket cp) { this.target = cp.Target; this.command = cp.Command; this.metatype = cp.Metatype; this.metadata = cp.Metadata; this.sentBytes = cp.SentBytes; this.receivedBytes = cp.ReceivedBytes; }
public SSEventArgs(SSocket SSocket) : base() { this.sSocket = SSocket; }