private void ReceiveCallbackNew(IAsyncResult result) { int bytesRead; StateObject state = (StateObject)result.AsyncState; Socket client = state.workSocket; try { // Retrieve the state object and the client socket try { bytesRead = client.EndReceive(result); } catch (Exception e) { //An error has occured when reading Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Error:" + e.Message + " Stack:" + e.StackTrace); Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); return; } if (bytesRead == 0) { //The connection has been closed. //Reconnect //DiameterAAAStack.Reconnect(stackContext); Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Zero Bytes Read"); Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); return; } byte[] rcvMsg = new byte[bytesRead]; Array.Copy(state.buffer, 0, rcvMsg, 0, bytesRead); appendData(rcvMsg); //Then start reading from the network again after Parsing recieved Message. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackNew), state); } catch (Exception e) { //Then start reading from the network again. //state.workSocket.Connect(state.workSocket.RemoteEndPoint); Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Error:" + e.Message + " Stack:" + e.StackTrace); //Then start reading from the network again after Parsing recieved Message. if (client.Connected) { client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackNew), state); } else { Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); } } }
private void ReceiveCallback(IAsyncResult result) { int bytesRead; StateObject state = (StateObject)result.AsyncState; Socket client = state.workSocket; try { // Retrieve the state object and the client socket try { bytesRead = client.EndReceive(result); } catch (Exception e) { //An error has occured when reading Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Error:" + e.Message + " Stack:" + e.StackTrace); Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); return; } if (bytesRead == 0) { //The connection has been closed. //Reconnect Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Zero Bytes Read"); Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); return; } /// Begin processing Diameter messages , If new data is too large for buffer, reset the buffer and store new data //StackLog.Write2TraceLog("ReceiveCallBack", "INIT:Bytesread=" + bytesRead + "\r\nINIT:mLen=" + mLen + "\r\nINIT:mPos=" + mPos); if (mLen + bytesRead > MAX_BUFF_SIZE) { //StackLog.Write2ErrorLog("ReceiveCallBack", "RESET:mLen=" + mLen + "\r\nRESET:Bytesread=" + bytesRead); if (mLen - mPos > 0) { byte[] tmpBuffer = new byte[mLen - mPos]; Array.Copy(responseBuffer, mPos, tmpBuffer, 0, mLen - mPos); responseBuffer = new byte[MAX_BUFF_SIZE]; Array.Copy(tmpBuffer, 0, responseBuffer, 0, tmpBuffer.Length); mLen = tmpBuffer.Length; mPos = 0; } else { responseBuffer = new byte[MAX_BUFF_SIZE]; mPos = 0; mLen = 0; } } //Append new data into existing Message Buffer try { //StackLog.Write2ErrorLog("ReceiveCallBack", "Copying Array: mLen: " + mLen + ", Bytesread: " + bytesRead + ", mPos: " + mPos + ", SrcArray len: " + state.buffer.Length + ", DstArray len: " + responseBuffer.Length); Array.Copy(state.buffer, 0, responseBuffer, mLen, bytesRead); mLen = mLen + bytesRead; //StackLog.Write2ErrorLog("ReceiveCallBack", "Array Copied: mLen: " + mLen + ", Bytesread: " + bytesRead + ", mPos: " + mPos + ", SrcArray len: " + state.buffer.Length + ", DstArray len: " + responseBuffer.Length); } catch (Exception ex) { StackLog.Write2ErrorLog("ReceiveCallBack", " Length Error: " + ex.Message + " \nmLen: " + mLen + ", Bytesread: " + bytesRead + ", mPos: " + mPos + ", SrcArray len: " + state.buffer.Length + ", DstArray len: " + responseBuffer.Length); //return; throw ex; } processData(); if (client.Connected) { //Then start reading from the network again after Parsing recieved Message. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } else { StackLog.Write2ErrorLog("ReceiveCallBack", " Socket disconnected, Reconnect Again!"); Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); } } catch (Exception e) { //Then start reading from the network again. //state.workSocket.Connect(state.workSocket.RemoteEndPoint); Common.StackLog.Write2ErrorLog("ReceiveCallBack", " ReceiveCallback Error:" + e.Message + " Stack:" + e.StackTrace); if (client.Connected) { //Then start reading from the network again after Parsing recieved Message. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } else { StackLog.Write2ErrorLog("ReceiveCallBack", " Exception Socket disconnected, Reconnect Again!"); Thread t = new Thread(new ParameterizedThreadStart(Reconnect)); t.Start(stackContext); } } }
private void ReceiveCallbackSeq(IAsyncResult result) { int bytesRead; StateObject state = (StateObject)result.AsyncState; Socket client = state.workSocket; //NetworkStream networkStream = tcpClient.GetStream(); //responseBuffer = result.AsyncState as byte[]; try { // Retrieve the state object and the client socket try { // read = networkStream.EndRead(result); bytesRead = client.EndReceive(result); } catch { //An error has occured when reading return; } if (bytesRead == 0) { //The connection has been closed. return; } client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackSeq), state); /// Begin processing Diameter messages /// byte[] responselocBuffer = new byte[bytesRead]; Array.Copy(state.buffer, 0, responselocBuffer, 0, bytesRead); Message msg = new Message(stackContext, responselocBuffer); //msg = msg.ProcessMessage(stackContext, responselocBuffer); //Perform Additional Messag Processing if (msg.CommandCode == DiameterMessageCode.DEVICE_WATCHDOG) { Message DWA = new Answer(stackContext).CreateDWA(msg); byte[] toSend = DWA.GetBytes(); //networkStream.Write(toSend, 0, toSend.Length); client.Send(toSend); } else { //Return message response if (msg.CommandCode == DiameterMessageCode.CAPABILITY_EXCHANGE) { if (ResponseWaiter.ContainsRequest("CEX")) { msg.SessionID = "CEX"; if (!ResponseWaiter.AddResponse(msg)) { Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Unable to Add Response for CEA Session:" + msg.SessionID); } } else { Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Unexpected CEA Received \r\n" + msg.ToString()); } } else if (msg.CommandCode == DiameterMessageCode.CREDIT_CONTROL) { if (ResponseWaiter.ContainsRequest(msg.SessionID)) { if (!ResponseWaiter.AddResponse(msg)) { Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Unable to Add Response for CCA Session:" + msg.SessionID); } } else { Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Unexpected CCA Received for Session " + msg.SessionID + "\r\n" + msg.ToString()); } } else { Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Unexpected Messages Received:" + msg.ToString()); } } //Then start reading from the network again after Parsing recieved Message. //networkStream.BeginRead(responseBuffer, 0, responseBuffer.Length, ReceiveCallback, buffer); } catch (Exception e) { //Then start reading from the network again. //networkStream.BeginRead(buffer, 0, buffer.Length, ReceiveCallback, buffer); //state.workSocket.Connect(state.workSocket.RemoteEndPoint); Common.StackLog.Write2ErrorLog("ReceiveCallBack", "Error:" + e.Message + " Stack:" + e.StackTrace); client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackSeq), state); } }