protected override IFuture Send(object obj, IPacket packet) { AsyncFuture item = new AsyncFuture(this); item.AssociatedObject = obj; item.AssociatedPacket = packet; if (!base.IsOpened) { item.IsSucceeded = false; return(item); } lock (_sendQueue) { _sendQueue.Enqueue(item); if (_sendQueue.Count > 1) { return(item); } SendFirstOfQueue(); } return(item); }
void SendFirstOfQueue() { if (_sendQueue.Count > 0) { try { AsyncFuture future = null; lock (_sendQueue) { if (_sendQueue.Count <= 0) { return; } future = _sendQueue.Peek(); } if (future != null) { IPacket associatedPacket = future.AssociatedPacket; base.GetSessionFilterChain(new SendOperateFilter(this, future), FilterChainMode.Send).PacketSend(associatedPacket); } } catch (Exception ex) { base.SessionCaughtException(ex); throw; } } }
void OpenClinet(AsyncTcpSession tcpClient, int id) { AppendInfo(string.Concat("TcpClient:", id.ToString(), ", 开始连接服务器!")); AsyncFuture openFuture = tcpClient.Open() as AsyncFuture; openFuture.FutureCompleted += OpenFutureCompleted; }
protected override void OnReceived(TPacket resp) { var task = AsyncFuture <TPacket> .Get(resp.Id); if (task != null && !task.IsCancelled) { base.OnReceived(resp); } AsyncFuture <TPacket> .OnCompleted(resp); }
protected virtual void DoProcessSend(SocketAsyncEventArgs args) { if (!base.IsOpened) { return; } if (!CheckSendOrReceiveSuccess(args)) { Close(); return; } var sendToken = args.UserToken as AsyncSocketSessionToken; AsyncFuture sendFuture = sendToken.Future; if (sendFuture != null) { IPacket associatedPacket = sendFuture.AssociatedPacket; try { int sendCount = args.BytesTransferred; try { IBuffer content = associatedPacket.Buffer; content.Position += sendCount; } catch (Exception ex) { if (_log.IsWarnEnabled) { _log.Warn("Change packet content postion failed.", ex); } } if (associatedPacket.Buffer.Remaining > 0 && base.SessionType != SessionType.Udp) { SocketSendAsync(associatedPacket, sendFuture); } else { SendCompletedProcess(sendFuture, true); } } catch (Exception ex) { _log.Fatal("DoProcessSend failed.", ex); SendCompletedProcess(sendFuture, false); base.SessionCaughtException(ex); } } }
public override IFuture Close() { AsyncFuture future = new AsyncFuture(this); if (!base.IsOpened) { future.IsSucceeded = false; return(future); } lock (_syncRoot) { if (!base.IsOpened) { throw new InvalidOperationException("This session not started."); } base.SessionState = SessionState.Closing; try { InternalClose(); future.IsSucceeded = true; } catch (ObjectDisposedException) { future.IsSucceeded = false; //base.SessionCaughtException(ex); } catch (Exception ex) { future.IsSucceeded = false; base.SessionCaughtException(ex); SafeClose(); } finally { lock (_sendQueue) { foreach (var sendFuture in _sendQueue) { sendFuture.IsSucceeded = false; } _sendQueue.Clear(); } base.SessionState = SessionState.Closed; base.SessionClosed(); } return(future); } }
protected override void OnConnectionClosed() { if (timer != null) { timer.Close(); if (timer != null) { timer.Dispose(); timer = null; } } AsyncFuture <TPacket> .CancelAll(); base.OnConnectionClosed(); }
public override IFuture Open() { if (base.SessionState != SessionState.Initial && SessionState != SessionState.Closed) { throw new InvalidOperationException("This session already started."); } if (base.RemoteEndPoint == null) { throw new InvalidOperationException("Remote endpoint could not be null."); } AsyncFuture future; lock (_syncRoot) { if (base.IsOpened) { throw new InvalidOperationException("This session already started."); } future = new AsyncFuture(this); try { base.SessionState = SessionState.Opening; InternalOpen(future); } catch (ObjectDisposedException) { future.IsSucceeded = false; //base.SessionCaughtException(ex); } catch (Exception ex) { SafeClose(); base.SessionCaughtException(ex); base.SessionState = SessionState.Initial; future.IsSucceeded = false; } return(future); } }
public IFuture Send(object obj) { IPacket packet; try { packet = PacketEncoder.Encode(this, obj); } catch (Exception ex) { SessionCaughtException(ex); AsyncFuture future = new AsyncFuture(this); future.AssociatedObject = obj; return(future); } return(Send(obj, packet)); }
internal void SocketSendAsync(IPacket packet, AsyncFuture future) { if (base.IsOpened) { SocketAsyncEventArgs args = _sendSaea; args.SetBuffer(packet.Buffer.ByteArray, packet.Buffer.Position, packet.Buffer.Remaining); var token = args.UserToken as AsyncSocketSessionToken; token.Future = future; bool isAsync = true; try { isAsync = DoSocketSendAsync(args); } catch (SocketException ex) { args.SetBuffer(null, 0, 0); SendCompletedProcess(future, false); base.SessionCaughtException(ex); Close(); } catch (ObjectDisposedException) { args.SetBuffer(null, 0, 0); SendCompletedProcess(future, false); //base.SessionCaughtException(ex); } catch (Exception ex) { args.SetBuffer(null, 0, 0); SendCompletedProcess(future, false); base.SessionCaughtException(ex); } if (!isAsync) { ProcessSend(args); } } }
void SendCompletedProcess(AsyncFuture sendFuture, bool successed) { lock (_sendQueue) { if (_sendQueue.Count > 0) { _sendQueue.Dequeue(); } } if (successed) { SendFirstOfQueue(); } sendFuture.IsSucceeded = successed; base.GetSessionFilterChain( FilterChainMode.Send).PacketSent(sendFuture.AssociatedPacket); base.GetSessionFilterChain( FilterChainMode.Send).ObjectSent(sendFuture.AssociatedObject); }
void ProcessConnect(SocketAsyncEventArgs args) { AsyncFuture future = args.UserToken as AsyncFuture; if (args.SocketError == SocketError.Success) { lock (base.SyncRoot) { base.SessionState = SessionState.Opened; base.SocketReceiveAsync(); future.IsSucceeded = true; } } else { lock (base.SyncRoot) { base.SessionState = SessionState.Initial; future.IsSucceeded = false; } } }
public virtual AsyncFuture <TPacket> SendAsync(TPacket req, Action <IAsyncContext <TPacket> > callback) { Check.Require(req != null); Check.Require(Coding != null); var future = AsyncFuture <TPacket> .Get(callback); req.Id = future.Id; future.Packet = req; if (Socket == null || !Socket.Connected) { future.Exception = new NetException(SocketError.NotConnected); return(future); } if (!NetHelper.InternetConnectedState) { future.Exception = new NetException(SocketError.NotConnected); //OnExceptionFired(future.Exception); return(future); } var data = Coding.EncodePacket(req); int length = data.Length + Params.PacketSeparateFlag.Length; var bytes = new byte[length]; data.CopyTo(bytes, 0); Params.PacketSeparateFlag.CopyTo(bytes, data.Length); try { Log.Info("Send packet:" + req.ToString()); future.AsyncResult = Socket.BeginSend(bytes, 0, length, SocketFlags.None, ar => { try { Socket.EndSend(ar); //wait response packet future.Reset(); future.Wait(); } catch (SocketException ex) { future.Exception = new NetException(ex.SocketErrorCode); //OnExceptionFired(future.Exception); Close(); } catch (Exception ex) { future.Exception = new NetException(ex.Message, ex); //OnExceptionFired(future.Exception); Close(); } }, null); } catch (IOException ex) { future.Exception = new NetException(ex.Message, ex); //OnExceptionFired(future.Exception); Close(); } catch (SocketException ex) { future.Exception = new NetException(ex.SocketErrorCode); //OnExceptionFired(future.Exception); Close(); } catch (Exception ex) { future.Exception = new NetException(ex.Message, ex); //OnExceptionFired(future.Exception); } return(future); }
public AsyncSocketSessionToken(AsyncSocketSession session, AsyncFuture future, object state) { Session = session; Future = future; State = state; }
protected void AsyncFutureCompleted(TPacket resp) { AsyncFuture <TPacket> .OnCompleted(resp); }
public SendOperateFilter(AsyncSocketSession session, AsyncFuture future) { _future = future; _parentSession = session; }