public void run() { //validate that both the client side and server side sockets are ok. If so, do read/write if (m_conn.clientSocket == null || m_conn.serverSocket == null) { logger.error("[{0}] ProxyConnection#{1}--Either client socket or server socket is null.", m_conn.serviceName, m_conn.connNumber); m_conn.Release(); return; } if (m_conn.clientSocket.Connected && m_conn.serverSocket.Connected) { //Read data from the client socket m_conn.clientSocket.BeginReceive( m_conn.clientReadBuffer, 0, ProxyConnection.BUFFER_SIZE, 0, new AsyncCallback(clientReadCallBack), m_conn); //Read data from the server socket m_conn.serverSocket.BeginReceive( m_conn.serverReadBuffer, 0, ProxyConnection.BUFFER_SIZE, 0, new AsyncCallback(serverReadCallBack), m_conn); } else { logger.error("[{0}] ProxyConnection#{1}: Either the client or server socket got disconnected.", m_conn.serviceName, m_conn.connNumber ); m_conn.Release(); } m_conn = null; }
public void run() { //validate that both the client side and server side sockets are ok. If so, do read/write if (m_conn.clientSocket == null || m_conn.serverSocket == null) { logger.error("[{0}] ProxyConnection#{1}--Either client socket or server socket is null.", m_conn.serviceName, m_conn.connNumber); m_conn.Release(); return; } if (m_conn.clientSocket.Connected && m_conn.serverSocket.Connected) { //Read data from the client socket m_conn.clientSocket.BeginReceive(m_conn.clientReadBuffer, 0, ProxyConnection.BUFFER_SIZE, 0, new AsyncCallback(clientReadCallBack), m_conn); //Read data from the server socket m_conn.serverSocket.BeginReceive(m_conn.serverReadBuffer, 0, ProxyConnection.BUFFER_SIZE, 0, new AsyncCallback(serverReadCallBack), m_conn); } else { logger.error("[{0}] ProxyConnection#{1}: Either the client or server socket got disconnected.", m_conn.serviceName, m_conn.connNumber); m_conn.Release(); } m_conn = null; }
public bool Release(ProxyConnection conn) { m_releaseCount++; if (conn.clientSocket != null) { logger.info("[{0}] Releasing ProxyConnection#{1}: Client {2}, Server {3}.", conn.serviceName, conn.connNumber, conn.clientSocket.RemoteEndPoint.ToString(), conn.serverEP.ToString()); } else { logger.info("[{0}] Releasing ProxyConnection#{1}: Server {2}.", conn.serviceName, conn.connNumber, conn.serverEP); } conn.disconnect(); m_connections.Remove(conn); if (m_releaseCount%100 == 0) { logger.info("Process is currently using {0} bytes of memory.", System.GC.GetTotalMemory(true)); } return true; }
public bool Release(ProxyConnection conn) { m_releaseCount++; if (conn.clientSocket != null) { logger.info("[{0}] Releasing ProxyConnection#{1}: Client {2}, Server {3}.", conn.serviceName, conn.connNumber, conn.clientSocket.RemoteEndPoint.ToString(), conn.serverEP.ToString()); } else { logger.info("[{0}] Releasing ProxyConnection#{1}: Server {2}.", conn.serviceName, conn.connNumber, conn.serverEP); } conn.disconnect(); m_connections.Remove(conn); if (m_releaseCount % 100 == 0) { logger.info("Process is currently using {0} bytes of memory.", System.GC.GetTotalMemory(true)); } return(true); }
public ProxyConnection getConnection() { ProxyConnection conn = null; lock(this) { logger.info("Allocating ProxyConnection#{0} for new connection.", m_connCount); conn = new ProxyConnection(); //create a new one m_connections.Add(conn); conn.connNumber = m_connCount++; } return conn; }
//Call back when the socket to the server has connected public static void connectCallBack(IAsyncResult ar) { ProxyConnection conn = (ProxyConnection)ar.AsyncState; try { conn.serverSocket.EndConnect(ar); logger.info("[{0}] ProxyConnection#{1}--connected to Server. Server: {2}, Local: {3}.", conn.serviceName, conn.connNumber, conn.serverSocket.RemoteEndPoint, conn.serverSocket.LocalEndPoint); //create task for proxying data between the client and server socket ProxySwapDataTask dataTask = new ProxySwapDataTask(conn); ThreadPool.getInstance().addTask(dataTask); } catch (SocketException se) { if (!conn.isShutdown) { if (se.ErrorCode == 10060) { logger.error("[{0}] Conn#{1} Socket Connect Timed out for server {2}", conn.serviceName, conn.connNumber, conn.serverEP); } else { logger.error("[{0}] Conn#{1} Socket Error occurred when connecting to server. Error Code is: {2}", conn.serviceName, conn.connNumber, se.ErrorCode); } conn.Release(); } } catch (Exception e) { if (!conn.isShutdown) { logger.error("[{0}] Conn# {1} Error occurred when connecting to server. Error is: {2}", conn.serviceName, conn.connNumber, e); conn.Release(); } } finally { conn = null; //free reference to the object } }
//Call back when the socket has connected public static void acceptCallBack(IAsyncResult ar) { ProxyConnection conn = null; try { ProxyClientListenerTask listener = (ProxyClientListenerTask)ar.AsyncState; //create a new task for connecting to the server side. conn = m_mgr.getConnection(); conn.serviceName = listener.m_config.serviceName; conn.clientSocket = listener.listenSocket.EndAccept(ar); //accept the client connection logger.info("[{0}] Conn#{1} Accepted new connection. Local: {2}, Remote: {3}.", conn.serviceName, conn.connNumber, conn.clientSocket.LocalEndPoint.ToString(), conn.clientSocket.RemoteEndPoint.ToString()); conn.serverEP = listener.m_config.serverEP; //Start listening for connection on this port again listener.listenSocket.BeginAccept(new AsyncCallback(ProxyClientListenerTask.acceptCallBack), listener); ProxyServerConnectTask serverTask = new ProxyServerConnectTask(conn); //now try to connect to the server ThreadPool.getInstance().addTask(serverTask); } catch (SocketException se) { logger.error("[{0}] Conn# {1} Socket Error occurred when accepting client socket. Error Code is: {2}", conn.serviceName, conn.connNumber, se.ErrorCode); if (conn != null) { conn.Release(); } } catch (Exception e) { logger.error("[{0}] Conn# {1} Error occurred when accepting client socket. Error is: {2}", conn.serviceName, conn.connNumber, e); if (conn != null) { conn.Release(); } } finally { conn = null; //free reference to the object } }
public ProxyConnection getConnection() { ProxyConnection conn = null; lock (this) { logger.info("Allocating ProxyConnection#{0} for new connection.", m_connCount); conn = new ProxyConnection(); //create a new one m_connections.Add(conn); conn.connNumber = m_connCount++; } return(conn); }
private static void clientReadCallBack(IAsyncResult ar) { ProxyConnection conn = (ProxyConnection)ar.AsyncState; int numBytes = 0; try { numBytes = conn.clientSocket.EndReceive(ar); if (numBytes > 0) //write to the server side socket { //copy the bytes to the server send buffer and call send Array.Copy(conn.clientReadBuffer, 0, conn.serverSendBuffer, 0, numBytes); conn.serverNumBytes += numBytes; conn.serverSocket.BeginSend(conn.serverSendBuffer, 0, numBytes, 0, new AsyncCallback(serverSendCallBack), conn); } else { logger.error("[{0}] ProxyConnection#{1}: Detected Client Socket disconnect via read.", conn.serviceName, conn.connNumber); conn.Release(); } } catch (SocketException se) { if (!conn.isShutdown) { logger.error("[{0}] ProxyConnection#{1}: Socket Error occurred when reading data from the client socket. Error Code is: {2}.", conn.serviceName, conn.connNumber, se.ErrorCode); conn.Release(); } } catch (Exception e) { if (!conn.isShutdown) { logger.error("[{0}] ProxyConnection#{1}: Error occurred when reading data from the client socket. The error is: {2}.", conn.serviceName, conn.connNumber, e); conn.Release(); } } finally { conn = null; } }
private static void serverSendCallBack(IAsyncResult ar) { ProxyConnection conn = (ProxyConnection)ar.AsyncState; try { int numBytes = conn.serverSocket.EndSend(ar); if (numBytes == conn.serverNumBytes) //finished sending the data, now read from client socket again { conn.serverNumBytes = 0; conn.clientSocket.BeginReceive(conn.clientReadBuffer, 0, ProxyConnection.BUFFER_SIZE, 0, new AsyncCallback(clientReadCallBack), conn); } else { conn.serverNumBytes -= numBytes; } } catch (SocketException se) { if (!conn.isShutdown) { logger.error("[{0}] ProxyConnection#{1}: Socket Error occurred when writing data to the server socket. Error Code is: {2}.", conn.serviceName, conn.connNumber, se.ErrorCode); conn.Release(); } } catch (Exception e) { if (!conn.isShutdown) { logger.error("[{0}] ProxyConnection#{1}, conn#{2}: Error occurred when writing data to the server socket. The error is: {2}.", conn.serviceName, conn.connNumber, e); conn.Release(); } } finally { conn = null; } }
public ProxySwapDataTask(ProxyConnection conn) { m_conn = conn; logger = Logger.getInstance(); }
public ProxyServerConnectTask(ProxyConnection conn) { m_conn = conn; }