//inserts information needed for async receive. //I could pass the asynceventargs instead of sd but for now //im leaving it at sd for clarification internal void StartReceive() { //if connection is closed if (SocketActions.IsConnectionClosed(_socket)) { SocketActions.ProcessError(this, "StartReceive: connection closed"); return; } //determine if there is data to be read. byte[] data = RetrieveReceived(); if (data != null) { //let handlereceived decide what to do with the data SocketActions.HandleReceived(this, data); //exit method since we dont need to set a completion event as we already //know there is data return; } try { SocketAsyncEventArgs eventArgs = new SocketAsyncEventArgs(); byte[] buffer = new byte[512]; //255 //r/ eventArgs.SetBuffer(buffer, 0, 512); eventArgs.UserToken = _socket; eventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(DataReceived); _socket.ReceiveAsync(eventArgs); } catch { SocketActions.ProcessError(this, "StartReceive: catch"); } }
internal void DataReceived(object sender, SocketAsyncEventArgs e) { byte[] data = RetrieveReceived(e); if (data != null) { SocketActions.HandleReceived(this, data); } //null array or 0 bytes means graceful socket close else { //SocketActions.ProcessError(this, "DataReceived: null data"); } }