override public void OnMessageReceived(SocketBase.IConnection connection, SocketBase.MessageReceivedEventArgs e) { base.OnMessageReceived(connection, e); int readlength; TMessage message = null; try { message = this._protocol.Parse(connection, e.Buffer, this._maxMessageSize, out readlength); } catch (Exception ex) { this.OnConnectionError(connection, ex); connection.BeginDisconnect(ex); e.SetReadlength(e.Buffer.Count); return; } if (message != null) { this._socketService.OnReceived(connection, message); } e.SetReadlength(readlength); }
/// <summary> /// find response /// </summary> /// <param name="connection"></param> /// <param name="buffer"></param> /// <param name="readlength"></param> /// <returns></returns> /// <exception cref="BadProtocolException">bad async binary protocl</exception> public Response.AsyncBinaryResponse FindResponse(SocketBase.IConnection connection, ArraySegment <byte> buffer, out int readlength) { if (buffer.Count < 4) { readlength = 0; return(null); } //获取message length var messageLength = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset); if (messageLength < 7) { throw new BadProtocolException("bad async binary protocl"); } readlength = messageLength + 4; if (buffer.Count < readlength) { readlength = 0; return(null); } var seqID = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset + 4); var flagLength = SocketBase.Utils.NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + 8); var strName = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + 10, flagLength); var dataLength = messageLength - 6 - flagLength; byte[] data = null; if (dataLength > 0) { data = new byte[dataLength]; Buffer.BlockCopy(buffer.Array, buffer.Offset + 10 + flagLength, data, 0, dataLength); } return(new Response.AsyncBinaryResponse(strName, seqID, data)); }
/// <summary> /// on connected /// </summary> /// <param name="node"></param> /// <param name="connection"></param> private void OnConnected(SocketConnector node, SocketBase.IConnection connection) { //fire connected event. this.Connected(node.Name, connection); bool isActive = false; lock (this) if (this._dicNodes.ContainsKey(node.Name)) { isActive = true; this._dicConnections[node.Name] = connection; this._arrConnections = this._dicConnections.Values.ToArray(); this._hashConnections = new ConsistentHashContainer <SocketBase.IConnection>(this._dicConnections); } if (isActive) { this.ServerAvailable(node.Name, connection); } else { connection.BeginDisconnect(); } }
/// <summary> /// send callback /// </summary> /// <param name="connection"></param> /// <param name="messageInfo"></param> /// <param name="isSuccess"></param> protected override void OnSendCallback(SocketBase.IConnection<TMessageInfo,TMessage> connection, TMessageInfo messageInfo, bool isSuccess) { messageInfo.SetProcess(SendMessageInfoProcess.SetSendResult); messageInfo.SetSendResult(isSuccess); this._socketService.OnSendCallback(connection, messageInfo.Message, isSuccess); }
/// <summary> /// register /// </summary> /// <param name="connection"></param> public void Register(SocketBase.IConnection connection) { if (this._dic.TryAdd(connection.ConnectionID, connection)) { this._stack.Push(connection); } }
/// <summary> /// on received unknow message /// </summary> /// <param name="connection"></param> /// <param name="message"></param> protected virtual void OnReceivedUnknowMessage(SocketBase.IConnection connection, TMessage message) { if (this.ReceivedUnknowMessage != null) { this.ReceivedUnknowMessage(connection, message); } }
/// <summary> /// parse /// </summary> /// <param name="connection"></param> /// <param name="buffer"></param> /// <param name="maxMessageSize"></param> /// <param name="readlength"></param> /// <returns></returns> /// <exception cref="BadProtocolException">bad command line protocol</exception> public Messaging.DefaultBinaryMessage Parse(SocketBase.IConnection connection, ArraySegment <byte> buffer, int maxMessageSize, out int readlength) { if (buffer.Count < Messaging.DefaultBinaryMessage.HEADER_SIZE) { readlength = 0; return(null); } var startPos = buffer.Offset; var bufferLen = buffer.Offset + buffer.Count; var totalSize = BitConverter.ToUInt16(buffer.Array, startPos); if (totalSize > bufferLen) { readlength = 0; return(null); } var bodyLen = totalSize - Messaging.DefaultBinaryMessage.HEADER_SIZE; var packetID = BitConverter.ToUInt16(buffer.Array, startPos + 2); var type = (SByte)buffer.Array[startPos + 4]; var version = BitConverter.ToUInt16(buffer.Array, startPos + 5); byte[] body = null; if (bodyLen > 0) { body = new byte[bodyLen]; Buffer.BlockCopy(buffer.Array, startPos + 7, body, 0, bodyLen); } readlength = totalSize; return(new Messaging.DefaultBinaryMessage(totalSize, packetID, type, version, body)); }
/// <summary> /// 当接收到客户端新消息时,会调用此方法. /// </summary> /// <param name="connection"></param> /// <param name="cmdInfo"></param> public override void OnReceived(SocketBase.IConnection connection, TCommandInfo cmdInfo) { if (string.IsNullOrEmpty(cmdInfo.CmdName)) { return; } ThreadPool.QueueUserWorkItem(_ => { Command.ICommand <TCommandInfo> cmd = null; this._dicCommand.TryGetValue(cmdInfo.CmdName, out cmd); try { if (cmd == null) { this.HandleUnKnowCommand(connection, cmdInfo); } else { cmd.ExecuteCommand(connection, cmdInfo); } } catch (Exception ex) { this.OnCommandExecException(connection, cmdInfo, ex); } }); }
/// <summary> /// find response /// </summary> /// <param name="connection"></param> /// <param name="buffer"></param> /// <param name="readlength"></param> /// <returns></returns> /// <exception cref="BadProtocolException">bad thrift protocol</exception> public Response.ThriftResponse FindResponse(SocketBase.IConnection connection, ArraySegment <byte> buffer, out int readlength) { if (buffer.Count < 4) { readlength = 0; return(null); } //获取message length var messageLength = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset); if (messageLength < 14) { throw new BadProtocolException("bad thrift protocol"); } readlength = messageLength + 4; if (buffer.Count < readlength) { readlength = 0; return(null); } var cmdLen = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset + 8); if (messageLength < cmdLen + 13) { throw new BadProtocolException("bad thrift protocol"); } int seqID = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset + 12 + cmdLen); var data = new byte[messageLength]; Buffer.BlockCopy(buffer.Array, buffer.Offset + 4, data, 0, messageLength); return(new Response.ThriftResponse(seqID, data)); }
/// <summary> /// parse /// </summary> /// <param name="connection"></param> /// <param name="buffer"></param> /// <param name="maxMessageSize"></param> /// <param name="readlength"></param> /// <returns></returns> /// <exception cref="BadProtocolException">bad thrift protocol</exception> public Messaging.ThriftMessage Parse(SocketBase.IConnection connection, ArraySegment <byte> buffer, int maxMessageSize, out int readlength) { if (buffer.Count < 4) { readlength = 0; return(null); } //获取message length var messageLength = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset); if (messageLength < 14) { throw new BadProtocolException("bad thrift protocol"); } if (messageLength > maxMessageSize) { throw new BadProtocolException("message is too long"); } readlength = messageLength + 4; if (buffer.Count < readlength) { readlength = 0; return(null); } var payload = new byte[messageLength]; Buffer.BlockCopy(buffer.Array, buffer.Offset + 4, payload, 0, messageLength); return(new Messaging.ThriftMessage(payload)); }
/// <summary> /// remove server node /// </summary> /// <param name="name"></param> /// <returns></returns> /// <exception cref="ArgumentNullException">name is null or empty</exception> public bool UnRegisterNode(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } SocketConnector node = null; SocketBase.IConnection connection = null; lock (this) { //remove node by name, if (this._dicNodes.TryGetValue(name, out node)) { this._dicNodes.Remove(name); } //get connection by name. this._dicConnections.TryGetValue(name, out connection); } if (node != null) { node.Stop(); } if (connection != null) { connection.BeginDisconnect(); } return(node != null); }
/// <summary> /// release /// </summary> /// <param name="connection"></param> public void Release(SocketBase.IConnection connection) { if (this._dic.ContainsKey(connection.ConnectionID)) { this._stack.Push(connection); } }
/// <summary> /// OnMessageReceived /// </summary> /// <param name="connection"></param> /// <param name="e"></param> protected override void OnMessageReceived(SocketBase.IConnection connection, SocketBase.MessageReceivedEventArgs e) { base.OnMessageReceived(connection, e); int readlength; TCommandInfo cmdInfo = null; try { cmdInfo = this._protocol.FindCommandInfo(connection, e.Buffer, this._maxMessageSize, out readlength); } catch (Exception ex) { this.OnError(connection, ex); connection.BeginDisconnect(ex); e.SetReadlength(e.Buffer.Count); return; } if (cmdInfo != null) { ThreadPool.QueueUserWorkItem(_ => { try { this._socketService.OnReceived(connection, cmdInfo); } catch { } }); } e.SetReadlength(readlength); }
/// <summary> /// OnMessageReceived /// </summary> /// <param name="connection"></param> /// <param name="e"></param> protected override void OnMessageReceived(SocketBase.IConnection connection, SocketBase.MessageReceivedEventArgs e) { base.OnMessageReceived(connection, e); //process message int readlength; TMessage message = null; try { message = this._protocol.Parse(connection, e.Buffer, out readlength); } catch (Exception ex) { base.OnConnectionError(connection, ex); connection.BeginDisconnect(ex); e.SetReadlength(e.Buffer.Count); return; } if (message != null) { Request <TMessage> request = null; if (this._receivingQueue.TryRemove(connection.ConnectionID, message.SeqID, out request)) { this.OnReceived(connection, request, message); } else { this.OnReceivedUnknowMessage(connection, message); } } //continue receiveing.. e.SetReadlength(readlength); }
/// <summary> /// on node already /// </summary> /// <param name="node"></param> /// <param name="connection"></param> private void OnNodeAlready(Node node, SocketBase.IConnection connection) { if (this.Already == null) { return; } this.Already(node.Info.Name, connection); }
/// <summary> /// socket accepted handler /// </summary> /// <param name="listener"></param> /// <param name="connection"></param> private void listener_Accepted(ISocketListener listener, SocketBase.IConnection connection) { if (base._listConnections.Count() > this._maxConnections) { connection.BeginDisconnect(); return; } base.RegisterConnection(connection); }
/// <summary> /// 向集合中添加连接 /// </summary> /// <param name="connection"></param> /// <returns></returns> /// <exception cref="ArgumentNullException">connection is null</exception> public bool Add(SocketBase.IConnection connection) { if (connection == null) { throw new ArgumentNullException("connection"); } return(this._dic.TryAdd(connection.ConnectionID, connection)); }
/// <summary> /// 断开连接 /// </summary> /// <param name="connection"></param> /// <param name="ex"></param> private void OnDisconnected(SocketBase.IConnection connection, Exception ex) { connection.Disconnected -= this.OnDisconnected; //delay reconnect 20ms ~ 200ms if (!this._isStop) SocketBase.Utils.TaskEx.Delay(new Random().Next(20, 200), this.Start); //fire disconnected event this._onDisconnected(this, connection); }
/// <summary> /// reply /// </summary> /// <param name="connection"></param> /// <param name="value"></param> /// <exception cref="ArgumentNullException">connection is null</exception> public void Reply(SocketBase.IConnection connection, string value) { if (connection == null) { throw new ArgumentNullException("connection"); } connection.BeginSend(ToPacket(value)); }
/// <summary> /// on node connected /// </summary> /// <param name="node"></param> /// <param name="connection"></param> private void OnNodeConnected(Node node, SocketBase.IConnection connection) { if (this.Connected == null) { return; } this.Connected(node.Info.Name, connection); }
/// <summary> /// socket accepted handler /// </summary> /// <param name="listener"></param> /// <param name="connection"></param> private void OnAccepted(ISocketListener listener, SocketBase.IConnection connection) { if (base.CountConnection() < this._maxConnections) { base.RegisterConnection(connection); return; } SocketBase.Log.Trace.Info("too many connections."); connection.BeginDisconnect(); }
/// <summary> /// socket accepted handler /// </summary> /// <param name="listener"></param> /// <param name="connection"></param> private void OnAccepted(ISocketListener<TMessageInfo, TMessage> listener, SocketBase.IConnection<TMessageInfo, TMessage> connection) { if (base.CountConnection() < this._maxConnections) { connection.SetProtoHandler(_protocolHandlerFactory.CreateProtocolHandler()); base.RegisterConnection(connection); return; } SocketBase.Log.Trace.Info("too many connections."); connection.BeginDisconnect(); }
/// <summary> /// on disconnected /// </summary> /// <param name="node"></param> /// <param name="connection"></param> private void OnDisconnected(SocketConnector node, SocketBase.IConnection connection) { lock (this) { if (!this._dicConnections.Remove(node.Name)) { return; } this._arrConnections = this._dicConnections.Values.ToArray(); this._hashConnections = new ConsistentHashContainer <SocketBase.IConnection>(this._dicConnections); } }
/// <summary> /// send request /// </summary> /// <param name="connection"></param> /// <param name="request"></param> /// <exception cref="ArgumentNullException">connection is null.</exception> /// <exception cref="ArgumentNullException">request is null.</exception> public void Send(SocketBase.IConnection connection, Request <TMessage> request) { if (connection == null) { throw new ArgumentNullException("connection"); } if (request == null) { throw new ArgumentNullException("request"); } connection.BeginSend(request); }
/// <summary> /// OnStartSending /// </summary> /// <param name="connection"></param> /// <param name="packet"></param> protected override void OnStartSending(SocketBase.IConnection connection, SocketBase.Packet packet) { base.OnStartSending(connection, packet); var request = packet as Request <TMessage>; if (request == null) { return; } request.SendConnection = connection; this._receivingQueue.TryAdd(request); }
/// <summary> /// destroy /// </summary> /// <param name="connection"></param> public void Destroy(SocketBase.IConnection connection) { if (connection == null) { throw new ArgumentNullException("connection"); } lock (this) { if (this._list.Remove(connection)) { this._arr = this._list.ToArray(); } } }
/// <summary> /// send request /// </summary> /// <param name="request"></param> /// <exception cref="ArgumentNullException">request is null.</exception> public void Send(Request <TMessage> request) { if (request == null) { throw new ArgumentNullException("request"); } request.AllowRetry = true; SocketBase.IConnection connection = null; if (this._connectionPool.TryAcquire(out connection)) { connection.BeginSend(request); return; } this._pendingQueue.Enqueue(request); }
/// <summary> /// send packet /// </summary> /// <param name="packet"></param> /// <returns></returns> /// <exception cref="ArgumentNullException">packet is null.</exception> public bool Send(SocketBase.Packet packet) { if (packet == null) { throw new ArgumentNullException("packet"); } SocketBase.IConnection connection = null; if (!this._connectionPool.TryAcquire(out connection)) { return(false); } connection.BeginSend(packet); return(true); }
/// <summary> /// on request received /// </summary> /// <param name="connection"></param> /// <param name="request"></param> /// <param name="message"></param> protected virtual void OnReceived(SocketBase.IConnection connection, Request <TMessage> request, TMessage message) { ThreadPool.QueueUserWorkItem(_ => { try { request.SetResult(message); } catch (Exception ex) { SocketBase.Log.Trace.Error(ex.Message, ex); } }); if (!this._protocol.IsAsync) { //release connection this._connectionPool.Release(connection); //try send next request this.TrySendNext(); } }
/// <summary> /// 当接收到客户端新消息时,会调用此方法. /// </summary> /// <param name="connection"></param> /// <param name="cmdInfo"></param> public virtual void OnReceived(SocketBase.IConnection connection, TCommandInfo cmdInfo) { if (connection == null || cmdInfo == null || string.IsNullOrEmpty(cmdInfo.CmdName)) { return; } Command.ICommand <TCommandInfo> cmd = null; if (this._dicCommand.TryGetValue(cmdInfo.CmdName, out cmd)) { cmd.ExecuteCommand(connection, cmdInfo); } else { this.HandleUnKnowCommand(connection, cmdInfo); } }