/// <summary> /// 实例化socket类 /// </summary> public static SocketClient SocketConnect(string RemoteIP, int RemotePort, string cn) { SocketClient sc = null; SocketClientType _type = GetSocketClientType(cn); if (!SocketDic.TryGetValue(_type, out sc)) { SocketDic[_type] = new SocketClient(RemoteIP, RemotePort, cn); sc = SocketDic[_type]; } else if (sc.ip == RemoteIP && sc.port == RemotePort) { if (!sc.IsConnected) { sc.StartInitSocket(); } } else { sc.Close(); SocketDic[_type] = new SocketClient(RemoteIP, RemotePort, cn); sc = SocketDic[_type]; } return(sc); }
public SocketClient(SocketClientMgr mgr, SocketClientType socketClientType, Action <SocketClientType, MsgPacket> packetHandler = null) { Status = SocketStatus.DisConnected; this._mgr = mgr; this.SocketClientType = socketClientType; this._packetHandler = packetHandler; }
public void DisConnect(SocketClientType socketClientType) { SocketClient socketClient; _map.TryGetValue(socketClientType, out socketClient); if (socketClient != null) { socketClient.Disconnect(); } }
public static void RemoveSocket(SocketClientType _type) { SocketClient sc = null; if (!SocketDic.TryGetValue(_type, out sc)) { SocketDic.Remove(_type); sc.Close(); } }
/// <summary> /// 处理接受的数据 /// </summary> /// <param name="socket"></param> private void Recieve(IAsyncResult socket) { Socket SocketClient = (Socket)socket.AsyncState; string IP = SocketClient.RemoteEndPoint.ToString(); if (SocketClient == null || !SessionPool.ContainsKey(IP)) { return; } try { int length = SocketClient.EndReceive(socket); if (length > 0) { byte[] buffer = SessionPool[IP].buffer; SocketClient.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), SocketClient); string msg = Encoding.UTF8.GetString(buffer, 0, length); // websocket建立连接的时候,除了TCP连接的三次握手,websocket协议中客户端与服务器想建立连接需要一次额外的握手动作 SocketClientType socketClientType = SocketClientType.UnknownClient; if (TCPClientHelper.IsClientConnctionRequest(msg, out socketClientType)) { SocketClient.Send(PackageHandShakeData(buffer, length)); SessionPool[IP].SocketClientType = socketClientType; return; } if (TCPClientHelper.IsConnected(SessionPool, IP)) { //if (SessionPool[IP].SocketClientType == SocketClientType.WebClient) //{ //} msg = AnalyzeClientData(buffer, length); if (msg != "") { CommandProcessModel cpModel = new CommandProcessModel(SocketClient, msg, IP); Thread commandProcessThread = new Thread(new ParameterizedThreadStart(CommandProcess)) { IsBackground = true, Name = "在线检查线程" }; commandProcessThread.Start(cpModel); } else { CloseSocketClient(SocketClient, IP); } } } } catch { CloseSocketClient(SocketClient, IP); } }
public void BeginConnect(string ip, int port, SocketClientType socketClientType, Action <bool> callback = null) { SocketClient socketClient; _map.TryGetValue(socketClientType, out socketClient); if (socketClient == null) { socketClient = new SocketClient(this, socketClientType, PacketHandler); _map.Add(socketClientType, socketClient); } socketClient.BeginConnect(ip, port, callback); }
/// <summary> /// 处理接受的数据 /// </summary> /// <param name="socket"></param> private void Recieve(IAsyncResult socket) { Socket SockeClient = (Socket)socket.AsyncState; string IP = SockeClient.RemoteEndPoint.ToString(); if (SockeClient == null || !SessionPool.ContainsKey(IP)) { return; } try { int length = SockeClient.EndReceive(socket); if (length > 0) { byte[] buffer = SessionPool[IP].buffer; SockeClient.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), SockeClient); string msg = Encoding.UTF8.GetString(buffer, 0, length); // websocket建立连接的时候,除了TCP连接的三次握手,websocket协议中客户端与服务器想建立连接需要一次额外的握手动作 SocketClientType socketClientType = SocketClientType.UnknownClient; if (TCPClientHelper.IsClientConnctionRequest(msg, out socketClientType)) { SockeClient.Send(PackageHandShakeData(buffer, length)); SessionPool[IP].SocketClientType = socketClientType; return; } if (TCPClientHelper.IsConnected(SessionPool, IP)) { if (SessionPool[IP].SocketClientType == SocketClientType.WebClient) { msg = AnalyzeClientData(buffer, length); } CommandProcess(SockeClient, msg, IP); } } } catch { try { //SockeClient.Disconnect(true); CommandProcessHandler.ExceptionAnswer(SockeClient); } catch { } finally { //SessionPool.Remove(IP); } } }
/// <summary> /// 是否客户端链接 /// </summary> /// <param name="msg"></param> /// <param name="socketClientType"></param> /// <returns></returns> public static bool IsClientConnctionRequest(string msg, out SocketClientType socketClientType) { socketClientType = SocketClientType.UnknownClient; if (msg.Contains("Sec-WebSocket-Key")) { socketClientType = SocketClientType.WebClient; return(true); } else { return(false); } }
void InitSocketClientType() { if ("SocketClient_User" == ClaName) { mReconnectType = ReconnectType.Reexteral; mSocketClientType = SocketClientType.Socket_User; } else if ("SocketClient_Game" == ClaName) { mReconnectType = ReconnectType.Reexteral; mSocketClientType = SocketClientType.Socket_Game; } }
public static SocketClientType GetSocketClientType(string cn) { SocketClientType sct = SocketClientType.Socket_Null; if ("SocketClient_User" == cn) { sct = SocketClientType.Socket_User; } else if ("SocketClient_Game" == cn) { sct = SocketClientType.Socket_Game; } return(sct); }
public void SendMsg(SocketClientType socketClientType, ushort ID, byte[] buff) { SocketClient socketClient; _map.TryGetValue(socketClientType, out socketClient); if (socketClient != null && socketClient.Status == SocketStatus.Connected) { MsgPacket packet = new MsgPacket(ID, buff); socketClient.SendPacket(packet); } else { SocketTools.LogError("socketClientType:" + socketClientType + " is not connected!"); } }
private void PacketHandler(SocketClientType socketClientType, MsgPacket packet) { Dictionary <ushort, List <MsgPacketCallback> > map; _mapListener.TryGetValue(socketClientType, out map); if (map == null) { return; } List <MsgPacketCallback> list; map.TryGetValue(packet.ID, out list); if (list == null) { return; } for (int i = 0; i < list.Count; i++) { list[i].Invoke(packet); } //SocketTools.Log(string.Format("receive,packetID:{0} packetStatus:{1} packetData:{2}", packet.ID, packet.Status, ((Msg.Info)packet.ProtoBufObj).msg)); }
public void RegisterListener(SocketClientType socketClientType, ushort ID, MsgPacketCallback callback) { Dictionary <ushort, List <MsgPacketCallback> > map; _mapListener.TryGetValue(socketClientType, out map); if (map == null) { map = new Dictionary <ushort, List <MsgPacketCallback> >(); _mapListener.Add(socketClientType, map); } List <MsgPacketCallback> list; map.TryGetValue(ID, out list); if (list == null) { list = new List <MsgPacketCallback>(); map.Add(ID, list); } if (!list.Contains(callback)) { list.Add(callback); } }
/// <summary> /// 填充客户端套接字 /// </summary> /// <param name="conferenceName">会议名称</param> /// <param name="contactUrl">参会人</param> /// <param name="socket">套接字</param> public static void FillsocDic(Dictionary <int, MeetServerSocket> dicServierSocket, int conferenceID, string contactUrl, Socket socket, SocketClientType socketClientType) { try { //查看是否包含该会议通讯节点 if (dicServierSocket.ContainsKey(conferenceID)) { SocketModel socketModel = new SocketModel() { Socket = socket, SocketClientType = socketClientType }; //会议的通信节点集合 Dictionary <string, SocketModel> dicSocketModels = dicServierSocket[conferenceID].DicClientSocket; //该会议通讯节点是否包含该客户端通讯节点(处于一对多的映射关系) if (!dicSocketModels.ContainsKey(contactUrl)) { //该会议通讯节点添加该客户端的通讯节点 dicSocketModels.Add(contactUrl, socketModel); } else if (dicSocketModels.ContainsKey(contactUrl) && !dicSocketModels[contactUrl].Socket.Connected) { //该会议通讯节点添加该客户端的通讯节点 dicSocketModels[contactUrl] = socketModel; } } } catch (Exception ex) { LogManage.WriteLog(typeof(Constant), ex); } }
/// <summary> /// 矩阵应用 /// </summary> /// <param name="args"></param> static void MatrixServerSocket_TCPDataArrival(ConferenceWebCommon.Common.TcpDateEvent args, SocketClientType socketClientType) { try { //填充客户端套接字 FillsocDic(DicMatrixMeetServerSocket, args.ClientIncordingEntity.ConferenceID, args.ClientIncordingEntity.SelfUri, args.Socket, socketClientType); } catch (Exception ex) { LogManage.WriteLog(typeof(Constant), ex); } }