/// <summary> /// /// </summary> /// <param name="id">唯一ID</param> /// <param name="receiveBuffer">接收缓冲区</param> /// <param name="sendBuffer">发送缓冲区</param> /// <param name="socket">连接的Socket</param> /// <param name="process"></param> public UserSession(int id, BytesSegment receiveBuffer, BytesSegment sendBuffer, Socket socket, IBufferProcessor process) : base(id, receiveBuffer, sendBuffer, process) { m_socket = socket; m_address = ((System.Net.IPEndPoint)(socket.RemoteEndPoint)).Address; Created = DateTime.UtcNow; }
public ISession CreateSession(Socket socket) { var receivebuffer = new Sinan.Collections.BytesSegment(new byte[m_receiveBufferSize]); var sendbuffer = new Sinan.Collections.BytesSegment(new byte[m_sendBufferSize]); // 初始化session数据 int id = System.Threading.Interlocked.Increment(ref m_id); UserSession session = new UserSession(id, receivebuffer, sendbuffer, socket, m_process); return session; }
protected override void OnConnected(System.Net.Sockets.Socket e) { const int bufferSize = 65535; BytesSegment receivebuffer = new BytesSegment(new byte[bufferSize], 0, bufferSize); BytesSegment sendbuffer = new BytesSegment(new byte[bufferSize], 0, bufferSize); GMProcessor2 processor = new GMProcessor2(bufferSize); GMToken session = new GMToken(m_zoneid, receivebuffer, sendbuffer, e, processor); //发送握手信息.. session.SendAsync(hand); //开始接收数据 session.ReceiveAsync(null,0,0); //发送登录信息 session.Call(LoginCommand.UserLogin, ConfigLoader.Config.Zoneid.ToString(), ConfigLoader.Config.ReportSIP); }
/// <summary> /// 构造函数 /// </summary> /// <param name="maxClients">允许的最大连接数</param> /// <param name="receiveBufferSize">每个连接接收数据缓存的大小</param> /// <param name="sendBufferSize">每个连发送收数据缓存的大小</param> /// <param name="sendQueueSize">等待发送数据的最大业务包</param> public SessionFactory(int maxClients, int receiveBufferSize, int sendBufferSize, int sendQueueSize, IBufferProcessor commandReader) { m_receiveBufferSize = receiveBufferSize; m_sendBufferSize = sendBufferSize; m_bufferPool = new System.Collections.Concurrent.ConcurrentQueue<UserBuffer>(); m_receivebuffer = new byte[maxClients * receiveBufferSize]; m_sendbuffer = new byte[maxClients * sendBufferSize]; m_process = commandReader; for (int i = 0; i < maxClients; i++) { var receivebuffer = new Sinan.Collections.BytesSegment(m_receivebuffer, m_receiveBufferSize * i, m_receiveBufferSize); var sendbuffer = new Sinan.Collections.BytesSegment(m_sendbuffer, m_sendBufferSize * i, m_sendBufferSize); UserBuffer helper = new UserBuffer(i, this, receivebuffer, sendbuffer, sendQueueSize); m_bufferPool.Enqueue(helper); } }
void ProcessHandler(IAsyncResult iar) { try { StateObject state = iar.AsyncState as StateObject; Socket client = state.workSocket; try { SocketError error; int len = client.EndReceive(iar, out error); if (len > 0 && error == SocketError.Success) { //处理握手 Sinan.Collections.BytesSegment bin = new Sinan.Collections.BytesSegment(state.data, 0, len); int offset = m_handshake.Execute(client, bin); if (offset >= 0) { int count = len - offset; if (count == 0) { //继续接收 client.BeginReceive(state.data, 0, state.data.Length, SocketFlags.None, ProcessHandler, state); return; } else if (count > 0) { state.workSocket = null; StartReceive(client, state.data, offset, count); return; } } } } catch { } client.SafeClose(); } catch { } }
private void ReceiveAsync(int len) { Sinan.Collections.BytesSegment data = new Sinan.Collections.BytesSegment(m_receiver.Buffer, m_receiveOffset, len); //读取命令 int remaining = m_processor.Execute(this, data); if (m_socket == null || remaining < 0) { return; } if (remaining > 0 && remaining < m_receiveMax) { //根据剩余量计算新的偏移 m_remainOffset = m_receiveOffset + len - remaining; m_remaining = remaining; if (m_remainOffset > m_receiveOffset) { System.Buffer.BlockCopy(m_receiver.Buffer, m_remainOffset, m_receiver.Buffer, m_receiveOffset, m_remaining); } } else { m_remaining = 0; m_remainOffset = m_receiveOffset; } //因为缓存大小不变,只要偏移量相同,则数量肯定相同,偏移不同,则数量不同 int offset = m_remaining + m_receiveOffset; if (offset != m_receiver.Offset) { m_receiver.SetBuffer(offset, m_receiveMax - m_remaining); } if (m_socket != null) { if (!m_socket.ReceiveAsync(m_receiver)) { ProcessReceive(); } } }
/// <summary> /// 向客户端发送数据(异步方式.) /// </summary> /// <param name="buffer"></param> /// <returns></returns> public SocketError SendAsync(Sinan.Collections.BytesSegment buffer) { try { if (buffer == null) { return SocketError.NoData; } // 尝试0变1,锁定Sender; int state = Interlocked.CompareExchange(ref m_socketInUse, 1, 0); if (state == 0) { if (m_curBuff == null && m_sendPool.IsEmpty) { m_curBuff = buffer; } else { m_sendPool.Enqueue(buffer); } SendData(); } else if (state == 1) { m_sendPool.Enqueue(buffer); } else { return SocketError.NotConnected; } //记日志 #if FlowLog FlowLog.AddOut(buffer.Array, buffer.Offset); #endif return SocketError.Success; } catch { if (m_socketInUse < 2) { Thread.VolatileWrite(ref m_socketInUse, 2); } return SocketError.NotConnected; } }
public virtual void Close() { if (m_socketInUse < 2) { Thread.VolatileWrite(ref m_socketInUse, 2); } Socket client = null; lock (m_locker) { if (m_socket != null) { client = m_socket; m_socket = null; m_sendPool = null; m_curBuff = null; } } if (client != null) { m_written = 0; client.SafeClose(); } }