private void ConnectCallback_obsolete(IAsyncResult ar) { TcpClient client = null; try { client = (TcpClient)ar.AsyncState; client.EndConnect(ar); client.NoDelay = true; RemoteState = new RemoteHostState(client); RemoteState.GotDataToSend += new EventHandler <EventArgs>(RemoteState_GotDataToSend); RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, new AsyncCallback(RecvCallback_obsolete), null); OnConnected(); } catch { if (RemoteState != null) { RemoteState.Close(); RemoteState = null; } else if (client != null) { client.Close(); client = null; } OnFailedConnect(); } }
void DoSend(RemoteHostState state) { try { byte[] data = null; lock (state.SendQueue) { if (state.SendQueue.Count > 0) { data = state.SendQueue.Peek(); } } if (data != null) { state.Stream.BeginWrite(data, 0, data.Length, writeCallback, state); } } catch (System.IO.IOException ioe) { HandleIOException(ioe, state); } //We dont need to take care of ObjectDisposedException. //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already catch { } }
private void ConnectCallback(IAsyncResult ar) { TcpClient client = null; try { client = (TcpClient)ar.AsyncState; client.EndConnect(ar); client.NoDelay = true; RemoteState = new RemoteHostState(client); RemoteState.GotDataToSend += RemoteState_GotDataToSend; RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, readCallback, RemoteState); OnOpenedConnection(); } catch (Exception ex) { if (RemoteState != null) { DoCloseConnection(); } else { if (client != null) { client.Close(); client = null; } } OnClosedConnection(ex); } }
void HandleIOException(System.IO.IOException ioe, RemoteHostState state) { if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError)) { System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException; CloseConnection(state, (se.SocketErrorCode == SocketError.Interrupted) ? null : se); } else { CloseConnection(state, ioe); } }
void CloseConnection(RemoteHostState state, bool remote, Exception ex) { if (state != null) { lock (clients) clients.Remove(state); IPEndPoint remoteHost = state.EndPoint; state.GotDataToSend -= state_GotDataToSend; state.Close(); OnClientConnectionStateChanged(remoteHost, false, ex, remote); } }
protected void OnClientConnected(RemoteHostState state) { try { if (ClientConnectionStateChanged != null) { ClientConnectionEventArgs eventargs = new ClientConnectionEventArgs(state.EndPoint.Address.ToString(), state.EndPoint.Port, true, null, true); eventargs.Client = state; ClientConnectionStateChanged(this, eventargs); } } catch { } }
void ReadCallback(IAsyncResult ar) { RemoteHostState state = (RemoteHostState)ar.AsyncState; try { int len = 0; len = state.Stream.EndRead(ar); if (len == 0) { CloseConnection(state, true); } else { try { if (ProtocolStrategy != null) { if (ProtocolStrategy.Encoding != null) { if (state.Decoder == null) { state.Decoder = ProtocolStrategy.Encoding.GetDecoder(); } int charCount = state.Decoder.GetCharCount(state.ReadBuffer, 0, len); char[] chars = new char[charCount]; state.Decoder.GetChars(state.ReadBuffer, 0, len, chars, 0); string msg = new string(chars); ProtocolStrategy.Parse(msg, state); } else { ProtocolStrategy.Parse(state.ReadBuffer, len, state); } } } catch { } state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state); } } catch (System.IO.IOException ioe) { HandleIOException(ioe, state); } //We dont need to take care of ObjectDisposedException. //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already catch { } }
void WriteCallback(IAsyncResult ar) { try { RemoteHostState state = ar.AsyncState as RemoteHostState; state.Stream.EndWrite(ar); } catch (System.IO.IOException ioe) { if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError)) { System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException; if (DoCloseConnection()) { OnClosedConnection((se.SocketErrorCode == SocketError.Interrupted) ? null : se); } } else if (DoCloseConnection()) { OnClosedConnection(ioe); } return; } //We dont need to take care of ObjectDisposedException. //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already catch { } bool doSendMore = false; if (RemoteState != null) { lock (RemoteState.SendQueue) { RemoteState.SendQueue.Dequeue(); if (RemoteState.SendQueue.Count > 0) { doSendMore = true; } } } if (doSendMore) { DoSend(); } }
void AcceptCallback(IAsyncResult ar) { RemoteHostState state = null; bool beginNewAccept = true; try { if (ar.IsCompleted) { state = new RemoteHostState(listener.EndAcceptTcpClient(ar)); state.GotDataToSend += state_GotDataToSend; state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state); } else { beginNewAccept = false; } } catch (SocketException se) { if (se.SocketErrorCode == SocketError.Interrupted) { beginNewAccept = false; } } catch (ObjectDisposedException) { beginNewAccept = false; } catch (Exception ex) { CloseConnection(state, ex); state = null; } if (state != null) { lock (clients) clients.Add(state); OnClientConnected(state); } if (beginNewAccept) { DoBeginAccept(); } }
void WriteCallback(IAsyncResult ar) { RemoteHostState state = (RemoteHostState)ar.AsyncState; try { state.Stream.EndWrite(ar); } catch (System.IO.IOException ioe) { HandleIOException(ioe, state); return; } //We dont need to take care of ObjectDisposedException. //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already catch { } bool doSendMore = false; lock (state.SendQueue) { if (state.SendQueue.Count > 0) //This should always be true, since the currently sending data is left in the queue until this point { state.SendQueue.Dequeue(); } if (state.SendQueue.Count > 0) { doSendMore = true; } } if (doSendMore) { DoSend(state); } }
void CloseConnection(RemoteHostState state, Exception ex) { CloseConnection(state, false, ex); }
void CloseConnection(RemoteHostState state, bool remote) { CloseConnection(state, remote, null); }
public void SendTo(byte[] data, RemoteHostState client) { client.Send(data); }
public void SendTo(string str, RemoteHostState client) { SendTo(EncodeString(str), client); }
void AcceptCallback(IAsyncResult ar) { RemoteHostState state = null; bool beginNewAccept = true; try { if (ar.IsCompleted) { state = new RemoteHostState(listener.EndAcceptTcpClient(ar)); state.GotDataToSend += state_GotDataToSend; state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state); } else beginNewAccept = false; } catch (SocketException se) { if (se.SocketErrorCode == SocketError.Interrupted) { beginNewAccept = false; } } catch (ObjectDisposedException) { beginNewAccept = false; } catch (Exception ex) { CloseConnection(state, ex); state = null; } if (state != null) { lock(clients) clients.Add(state); OnClientConnected(state); } if(beginNewAccept) DoBeginAccept(); }
void HandleIOException(System.IO.IOException ioe, RemoteHostState state) { if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError)) { System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException; CloseConnection(state, (se.SocketErrorCode == SocketError.Interrupted) ? null : se); } else CloseConnection(state, ioe); }
private bool DoCloseConnection() { if(RemoteState != null) { RemoteState.GotDataToSend -= RemoteState_GotDataToSend; RemoteState.Close(); RemoteState = null; return true; } else return false; }
private void ConnectCallback_obsolete(IAsyncResult ar) { TcpClient client = null; try { client = (TcpClient)ar.AsyncState; client.EndConnect(ar); client.NoDelay = true; RemoteState = new RemoteHostState(client); RemoteState.GotDataToSend +=new EventHandler<EventArgs>(RemoteState_GotDataToSend); RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, new AsyncCallback(RecvCallback_obsolete), null); OnConnected(); } catch { if (RemoteState != null) { RemoteState.Close(); RemoteState = null; } else if (client != null) { client.Close(); client = null; } OnFailedConnect(); } }
private void ConnectCallback(IAsyncResult ar) { TcpClient client = null; try { client = (TcpClient)ar.AsyncState; client.EndConnect(ar); client.NoDelay = true; RemoteState = new RemoteHostState(client); RemoteState.GotDataToSend += RemoteState_GotDataToSend; RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, readCallback, RemoteState); OnOpenedConnection(); } catch(Exception ex) { if (RemoteState != null) { DoCloseConnection(); } else { if (client != null) { client.Close(); client = null; } } OnClosedConnection(ex); } }
public void Disconnect() { if(RemoteState != null && RemoteState.Close()) { RemoteState = null; try { //Signal that we got disconnected if (Disconnected != null) Disconnected(this, new NetworkEventArgs(Hostname, Port)); } catch { } } }
void state_GotDataToSend(object sender, EventArgs e) { RemoteHostState state = (RemoteHostState)sender; DoSend(state); }
void DoSend(RemoteHostState state) { try { byte[] data = null; lock (state.SendQueue) { if (state.SendQueue.Count > 0) data = state.SendQueue.Peek(); } if (data != null) state.Stream.BeginWrite(data, 0, data.Length, writeCallback, state); } catch (System.IO.IOException ioe) { HandleIOException(ioe, state); } //We dont need to take care of ObjectDisposedException. //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already catch { } }
private void ReadCallback(IAsyncResult ar) { try { RemoteHostState state = ar.AsyncState as RemoteHostState; int len = 0; len = state.Stream.EndRead(ar); if (len == 0) { CloseConnection(); } else { try { if (ProtocolStrategy != null) { if (ProtocolStrategy.Encoding != null) { if (state.Decoder == null) { state.Decoder = ProtocolStrategy.Encoding.GetDecoder(); } int charCount = state.Decoder.GetCharCount(state.ReadBuffer, 0, len); char[] chars = new char[charCount]; state.Decoder.GetChars(state.ReadBuffer, 0, len, chars, 0); string msg = new string(chars); ProtocolStrategy.Parse(msg, state); } else { ProtocolStrategy.Parse(state.ReadBuffer, len, state); } } } catch { } state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state); } } catch (System.IO.IOException ioe) { if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError)) { System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException; if (DoCloseConnection()) { OnClosedConnection((se.SocketErrorCode == SocketError.Interrupted) ? null : se); } } else if (DoCloseConnection()) { OnClosedConnection(ioe); } } //We dont need to take care of ObjectDisposedException. //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already catch { } }