public void Start() { isRunning = true; systemSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); systemSocket.Bind(IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, port)); threadRecv = new Thread(ThreadRecv) { IsBackground = true }; threadRecv.Start(); }
private void DoReceiveInThread() { EndPoint remotePoint = IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0); int cnt = currentSocket.ReceiveFrom(byteBuffer, byteBuffer.Length, SocketFlags.None, ref remotePoint); if (cnt > 0) { byte[] dst = new byte[cnt]; Buffer.BlockCopy(byteBuffer, 0, dst, 0, cnt); lock (recvBufferQueue) { recvBufferQueue.Enqueue(dst); } } }
public void Start() { try { currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); currentSocket.Bind(IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, ipcInfo.Port)); isRunning = true; threadRecv = new Thread(ThreadRecv) { IsBackground = true }; threadRecv.Start(); } catch (Exception e) { Debuger.LogError(e.Message + e.StackTrace); Stop(); } }
private void DoReceiveInThread() { EndPoint remotePoint = IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0); int cnt = currentSocket.ReceiveFrom(recvBufferTemp, recvBufferTemp.Length, SocketFlags.None, ref remotePoint); if (cnt > 0) { if (!remoteEndPoint.Equals(remotePoint)) { Debuger.LogError("收到非目标服务器的数据!"); return; } byte[] dst = new byte[cnt]; Buffer.BlockCopy(recvBufferTemp, 0, dst, 0, cnt); recvBuffQueue.Push(dst); } }
private void DoReceiveInThread() { EndPoint remotePoint = IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0); int cnt = currentSocket.ReceiveFrom(byteBuffer, byteBuffer.Length, SocketFlags.None, ref remotePoint); if (cnt > 0) { recvBufferTempReader.Attach(byteBuffer, cnt); byte[] convBytes = new byte[4]; recvBufferTempReader.ReadBytes(convBytes, 0, 4); uint sessionid = BitConverter.ToUInt32(convBytes, 0); lock (sessions) { ISession session = null; if (sessionid == 0) { sessionid = SessionIDGenerator.GetNextSessionID(); session = new KCPSession(sessionid, HandleSessionSend, sessionListener); sessions.Add(session.id, session); } else { if (sessions.ContainsKey(sessionid)) { session = sessions[sessionid]; } } if (session != null) { session.Active(remotePoint as IPEndPoint); session.DoReceiveInGateway(byteBuffer, cnt); } else { Debuger.LogWarning("无效的包! sessionid:{0}", sessionid); } } } }
private void DoReceiveInThread() { EndPoint remotePoint = IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0); int cnt = systemSocket.ReceiveFrom(recvBufferTemp, recvBufferTemp.Length, SocketFlags.None, ref remotePoint); if (cnt > 0) { recvBufferTempReader.Attach(recvBufferTemp, cnt); byte[] m_32b = new byte[4]; recvBufferTempReader.ReadBytes(m_32b, 0, 4); uint sid = BitConverter.ToUInt32(m_32b, 0); lock (mapSession) { FSPSession session = null; if (sid == 0) { Debuger.LogError("基于KCP的Sid为0,该包需要被丢掉"); } else { if (mapSession.ContainsKey(sid)) { session = mapSession[sid]; } } if (session != null) { session.Active(remotePoint as IPEndPoint); session.DoReceiveInGateway(recvBufferTemp, cnt); } else { Debuger.LogWarning("无效的包! sid:{0}", sid); } } } }
public bool Connect(string ipAddress, int listenPort) { if (currentSocket != null) { Debuger.LogError("无法建立连接,需要先关闭上一次连接!"); return(false); } ip = ipAddress; port = listenPort; lastRecvTimestamp = (uint)TimeUtility.GetTotalMillisecondsSince1970(); try { remoteEndPoint = IPUtility.GetHostEndPoint(ip, port); if (remoteEndPoint == null) { Debuger.LogError("无法将Host解析为IP!"); Close(); return(false); } currentSocket = new Socket(remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); currentSocket.Bind(IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0)); isRunning = true; threadRecv = new Thread(ThreadRecv) { IsBackground = true }; threadRecv.Start(); } catch (Exception e) { Debuger.LogError(e.Message + e.StackTrace); Close(); return(false); } return(true); }