首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 實作 P2P 網路通訊程式 (4) — 客戶端打洞處理與伺服器訊息分派迴圈

     編輯:關於C語言

客戶端處理打洞請求

收到 P2PPurchHolePacket,設定 P2P 位址、嘗試 UPnP 埠映射、回覆 P2PPurchHoleAckPacket:


private void ProcP2PPurchHoleMsg(Packet packet, IPEndPoint remoteEP) {
    P2PPurchHolePacket purchReqMsg = (P2PPurchHolePacket)packet;
    PeerEntity toUser = userList.Single(c => c.UserName == purchReqMsg.ToUserName);
    PeerEntity user = userList.Single(c => c.UserName == purchReqMsg.UserName);
    toUser.P2PAddress = toUser.RemoteEndPoint;
    printf("Set P2P Address for {0}->[{1}]", user.UserName, toUser.P2PAddress);

    if (NAT.AddPortMapping(toUser.P2PAddress.Port, ProtocolType.Udp, "AddPortMapping"))
        printf("Port mapping successed!");

    P2PPurchHoleAckPacket trashMsg = new P2PPurchHoleAckPacket(purchReqMsg.UserName, purchReqMsg.ToUserName);
    byte[] buffer = UtilityHelper.Serialize(trashMsg);
    client.Send(buffer, buffer.Length, user.RemoteEndPoint);
}

伺服器訊息處理執行緒

持續 Receive、反序列化、依 Command 分派:


private void RecvThreadProc() {
    IPEndPoint remotePoint = null;
    byte[] msgBuffer = null;
    while (true) {
        msgBuffer = server.Receive(ref remotePoint);
        try {
            object msgObj = UtilityHelper.Deserialize(msgBuffer);
            switch ((msgObj as Packet).GetCommandType()) {
                case Command.MSG_USERLOGIN: ProcUserLoginMsg(...); break;
                case Command.MSG_USERLOGOUT: ProcUserLogoutMsg(...); break;
                case Command.MSG_GETUSERLIST: ProcGetUserListMsg(...); break;
                case Command.MSG_P2PCONNECT: ProcP2PConnectMsg(...); break;
                case Command.MSG_USERACTIVEQUERY: ProcUserActiveQueryMsg(...); break;
            }
            Thread.Sleep(100);
        } catch { }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved