private static void Msg_CommonRequest(INetworkSocket socket, int msgIdx, string[] msgData) { string sendData = TransferProtocol.InitMsgData((MsgTypeCS)msgIdx); foreach (var node in msgData) { sendData += node; sendData += "#"; } Program.g_console_server.Send(socket, sendData); }
/// <summary> /// Broadcasts message to all channels except 'except' /// </summary> /// <param name="message"></param> /// <param name="channel"></param> /// <returns></returns> public void BroadcastExcept(object message, INetworkSocket except) { foreach (var channel in ThreadSafeChannels) { if (channel == except) { continue; } SendMessage(channel, message); } }
public SocketPipeWriter( IConnectionLogger logger, InlineSocketsOptions options, IConnection connection, INetworkSocket socket) { _logger = logger; _options = options; _connection = connection; _socket = socket; _buffer = new RollingMemory(options.MemoryPool); }
public bool SetNetworkSocket(INetworkSocket socket) { if (this.Status == ServerStatus.Stop) { this.NetworkSocket = socket; return(true); } else { return(false); } }
/// <summary> /// Broadcast given data to the server /// </summary> /// <param name="data">data in byte array</param> public void Broadcast(INetworkSocket sender, byte[] data) { List <IocpTcpSocket> socketList = GetClientSocketList(); foreach (IocpTcpSocket socket in socketList) { if (socket != sender) { socket.Send(data); } } }
public void AddSocket(INetworkSocket socket) { socket_count++; ClientSocket client = new ClientSocket(); client.socket = socket; client.hostname = socket.IPInfo.IPAddress; client.connectTime = DateTime.Now; client.gameStep = 0; client.uploadState = 0; m_socketList.Add(socket, client); }
public void MsgReceived(INetworkSocket socket, int msgId, Packet receivedPacket) { bool isContain = m_socketList.ContainsKey(socket); if (!isContain) { Program.AddLog("Errpr, socket list."); return; } if (msgId == (int)CoDriverMessageSegments.Type.HeartBeat) { m_socketList[socket].stayTime = DateTime.Now - m_socketList[socket].connectTime; SendMsg_HeartBeat(socket); } if (msgId == (int)CoDriverMessageSegments.Type.Login) { byte[] bytes = receivedPacket.PacketRaw; String login_name = StringFromByteArrWithMsgId(bytes); m_socketList[socket].loginname = login_name; m_socketList[socket].loginTime = DateTime.Now; } if (msgId == (int)CoDriverMessageSegments.Type.ReportResult) { byte[] bytes = receivedPacket.PacketRaw; m_socketList[socket].UpdateReportResult(bytes); String sendString = "User(" + socket.IPInfo.IPAddress + "): Report Result."; Program.AddLog(sendString); } if (msgId == (int)CoDriverMessageSegments.Type.TriggerEvent) { byte[] bytes = receivedPacket.PacketRaw; int size = receivedPacket.PacketByteSize; byte[] data_bytes = new byte[size - 16]; Array.Copy(bytes, 8, data_bytes, 0, size - 16); string str = System.Text.Encoding.UTF8.GetString(data_bytes); String sendString = "User(" + socket.IPInfo.IPAddress + "): Trigger Event: " + str; Program.AddLog(sendString); } if (msgId == (int)CoDriverMessageSegments.Type.ClientStep) { byte[] bytes = receivedPacket.PacketRaw; int step = bytes[1]; m_socketList[socket].gameStep = step; String sendString = "User(" + socket.IPInfo.IPAddress + "): Step Update " + step; Program.AddLog(sendString); } //float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat); }
public void OnReceived(INetworkSocket socket, Packet receivedPacket) { string sendString = "User(" + socket.IPInfo.IPAddress + ") : " + StringFromByteArr(receivedPacket.PacketRaw); AddMsg(sendString); foreach (var socketObj in m_socketList) { if (socketObj != socket) { socketObj.Send(receivedPacket); } } }
public void RemoveSocket(INetworkSocket socket) { m_socketList.Remove(socket); //foreach (var pair in m_socketList) //{ // ClientSocket value = pair.Value; // if (value.socket == socket) // { // } //} }
public void OnReceived(INetworkSocket socket, Packet receivedPacket) { if (receivedPacket.PacketByteSize <= 0) { String sendString = "User(" + socket.IPInfo.IPAddress + "): null packet!"; Program.AddLog(sendString); return; } string dataReceived = StringFromByteArr(receivedPacket.PacketRaw); m_socket_manager.MsgReceived(socket, dataReceived); return; }
public SocketPipeWriter( IConnectionLogger logger, InlineSocketsOptions options, IConnection connection, INetworkSocket socket) { _logger = logger; #if NETSTANDARD2_0 _connection = connection; #endif _socket = socket; _buffer = new RollingMemory(options.MemoryPool); }
private static void Msg_ReqVideoList(INetworkSocket socket, string[] msgData) { string[] infoData = Program.videoCollectionFunc.GetVideoInfoList(); foreach (var node in infoData) { //string msgData = TransferProtocol.Msg_SendVideoList_SC(node); //SendData(socket, msgData); string[] sendData = new string[1]; sendData[0] = node; Program.g_MsgSender.SendMsg(socket, (int)MsgTypeSC.SendVideoList, sendData); } return; }
public string GetSocketDesc(INetworkSocket inSocket) { if (!m_socketList.ContainsKey(inSocket)) { return("Null Socket."); } ClientSocket socket = m_socketList[inSocket]; string text; text = string.Format("Host:{0}.Name:{1}.ConnectTime:{2:HH:mm:ss.fff}.LoginTime:{3:HH:mm:ss.fff}.{4}.Stay:{5:dd\\.hh\\:mm\\:ss}" , socket.hostname, socket.loginname, socket.connectTime, socket.loginTime, socket.client_step, socket.stayTime); return(text); }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkSocket socket, Packet receivedPacket) { lock (m_generalLock) { if (socket == m_socket1) { m_socket2.Send(receivedPacket); } else { m_socket1.Send(receivedPacket); } } }
public void AddSocket(INetworkSocket socket) { lock (m_listLock) { m_socketList.Add(socket); } Task t = new Task(delegate() { OnJoin(this, socket); }); t.Start(); }
public void RaisePacketReceived(object packet, INetworkSocket channel) { InboundMessage messageObject = new InboundMessage() { Message = packet, SourceChannel = channel }; if (InputQueue != null) { if (!InputQueue.IsAddingCompleted) { InputQueue.Add(messageObject); } } }
/// <summary> /// Detach the given client from the server management /// </summary> /// <param name="clientSocket">the client to detach</param> /// <returns>the number of socket in the room</returns> public int DetachClient(INetworkSocket socket) { lock (m_listLock) { m_socketList.Remove(socket); Task t = new Task(delegate() { OnLeave(this, socket); }); t.Start(); return(m_socketList.Count); } }
public void OnNewConnection(INetworkSocket socket) { m_socketList.Add(socket); String sendString = "** New user(" + socket.GetIPInfo().GetIPAddress() + ") connected!"; AddMsg(sendString); byte[] sendBuff = BytesFromString(sendString); foreach (var socketObj in m_socketList) { if (socketObj != socket) { socketObj.Send(new Packet(sendBuff, sendBuff.Count(), false)); } } }
private static void Msg_ReqDownloadImages(INetworkSocket socket, string[] msgData) { ClientSocket clientSocket = Program.g_console_server.m_socket_manager.GetClientSocket(socket); int nRes = Program.videoCollectionFunc.CheckIsPossibleDownloadImages(msgData[2]); string[] sendData = new string[2]; sendData[0] = Convert.ToString(nRes); if (nRes == 0) { int ftpServerIdx = 1; sendData[1] = Convert.ToString(ftpServerIdx); clientSocket.uploadState = 1; } Program.g_MsgSender.SendMsg(socket, (int)MsgTypeSC.ResDownloadImages, sendData); return; }
public void AddSocket(INetworkSocket socket) { socket_count++; ClientSocket client = new ClientSocket(); client.socket = socket; client.hostname = socket.IPInfo.IPAddress; client.connectTime = DateTime.Now; client.reportRes.speedResult = new int[3]; client.reportRes.intersectionResult = new int[3]; client.reportRes.cornerResult = new int[3]; client.reportRes.roadPosResult = new int[3]; client.gameStep = 0; client.is_update_report_result = false; m_socketList.Add(socket, client); }
/// <summary> /// Detach given socket from the given room /// </summary> /// <param name="socket">socket to detach</param> /// <param name="roomName">room name</param> /// <returns>number of sockets left in the room</returns> public int Leave(INetworkSocket socket, string roomName) { lock (m_roomLock) { if (m_roomMap.ContainsKey(roomName)) { int numSocketLeft = m_roomMap[roomName].DetachClient(socket); if (numSocketLeft == 0) { m_roomMap.Remove(roomName); } return(numSocketLeft); } return(0); } }
public Connection( IConnectionLogger logger, InlineSocketsOptions options, INetworkSocket socket) { Features = new FeatureCollection(this); _logger = logger; _options = options; _socket = socket; RemoteEndPoint = _socket.RemoteEndPoint; LocalEndPoint = _socket.LocalEndPoint; (_socketInput, _socketOutput) = options.CreateSocketPipelines(this, socket); _connectionClosedTokenSource = new CancellationTokenSource(); _connectionClosedTokenSource.Token.Register(() => _logger.LogTrace("TODO: ConnectionClosed")); }
public void ReceivedMsg(INetworkSocket socket, string msgData) { int msgIdx; string[] msgStr = TransferProtocol.GetMsgStruct(msgData, out msgIdx); if (!msgDictionary.ContainsKey(msgIdx)) { return; } bool isValid = Program.g_console_server.m_socket_manager.IsContainSocket(socket); if (!isValid) { return; } msgDictionary[msgIdx].Run(socket, msgStr); }
public void OnNewConnection(INetworkSocket socket) { m_socketList.Add(socket); String sendString = "** New user(" + socket.IPInfo.IPAddress + ") connected!"; AddMsg(sendString); byte[] sendBuff = BytesFromString(sendString); foreach (var socketObj in m_socketList) { if (socketObj != socket) { socketObj.Send(new Packet(sendBuff, 0, sendBuff.Count(), false)); } } }
/// <summary> /// Join the room /// </summary> /// <param name="socket">socket</param> /// <param name="roomName">room name</param> /// <returns>the instance of the room</returns> public Room Join(INetworkSocket socket, string roomName) { lock (m_roomLock) { Room curRoom = null; if (m_roomMap.ContainsKey(roomName)) { curRoom = m_roomMap[roomName]; } else { curRoom = new Room(roomName, RoomCallBackObj); m_roomMap[roomName] = curRoom; } curRoom.AddSocket(socket); return(curRoom); } }
public void OnReceived(INetworkSocket socket, Packet receivedPacket) { string sendString = socket.IPInfo.IPAddress; //AddMsg(sendString); bclient_connected = true; Process_Msg(receivedPacket.PacketRaw); /* * foreach (var socketObj in m_socketList) * { * if (socketObj != socket) * { * socketObj.Send(receivedPacket); * } * } */ }
private void Disconnect(INetworkSocket socket, RconMessenger messenger) { try { if (messenger != null) { messenger.CloseConnection(); } if (socket != null) { socket.CloseConnection(); } } catch (Exception e) { Console.WriteLine(e); } }
/// <summary> /// Broadcast the given packet to all the client, connected /// </summary> /// <param name="sender">sender of the broadcast</param> /// <param name="packet">packet to broadcast</param> public void Broadcast(INetworkSocket sender, Packet packet) { List <INetworkSocket> list = GetSocketList(); foreach (INetworkSocket socket in list) { if (socket != sender) { socket.Send(packet); } } Task t = new Task(delegate() { OnBroadcast(this, sender, packet); }); t.Start(); }
/// <summary> /// Connect given two socket as p2p /// </summary> /// <param name="socket1">first socket</param> /// <param name="socket2">second socket</param> /// <param name="callback">callback object</param> /// <returns>true if paired otherwise false</returns> public bool ConnectPair(INetworkSocket socket1, INetworkSocket socket2, IP2PCallback callBackObj) { if (!Paired) { if (socket1 != null && socket2 != null && socket1 != socket2 && socket1.IsConnectionAlive && socket2.IsConnectionAlive) { lock (m_generalLock) { m_socket1 = socket1; m_socket2 = socket2; m_socket1.CallBackObj = this; m_socket2.CallBackObj = this; Paired = true; CallBackObj = callBackObj; return(true); } } } return(false); }
/// <summary> /// Queues message to be sent via channel /// </summary> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public bool SendMessage(INetworkSocket channel, object message) { if (!IsStarted) { return(false); } channel.SendMessage(message); var onMessageSent = OnMessageSent; if (OnMessageSent != null) { OnMessageSent(this, new OutboundMessage() { Message = message, DestinationChannel = channel }); } return(true); return(false); }
public void OnReceived(INetworkSocket socket, Packet receivedPacket) { //ListTimerViews.OnreceivedTimerMessage(socket, receivedPacket, ListEventViews); TimerView timerView = FindTimerViaSocket(socket); //timerView.DealWithPacket(receivedPacket, EventViews); try { string cmd = StringFromByteArr(receivedPacket.PacketRaw); string[] s = cmd.Split(','); if (s.Count() >= 2 && timerView != null) { switch (s[0]) { case "chipid": timerView.ChipId = Convert.ToInt32(s[1]); BindChip(timerView); break; case "timerstatus": if (timerView.ChipId != null) { timerView.Status = (TimerView.TimerStatus)Convert.ToInt32(s[1]); timerView.Time_used = new TimeSpan(Convert.ToInt64(s[3]) / 1000 * 10000000); } break; default: break; } } } catch (Exception e) { SetTipMessage(e.Message); } }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkSocket socket, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.PacketType) { case ParallelPacketType.DATA: if (ReceiveType == ReceiveType.BURST) { OnParallelSocketReceived(this, receivedParallelPacket); } else if (ReceiveType == ReceiveType.SEQUENTIAL) { lock (m_receiveLock) { m_receivedQueue.Enqueue(receivedParallelPacket); while (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().PacketID) { ParallelPacket curPacket = m_receivedQueue.Dequeue(); m_curReceivedPacketId = curPacket.PacketID; if (m_curReceivedPacketId == long.MaxValue) { m_curReceivedPacketId = -1; } OnParallelSocketReceived(this, curPacket); } } } break; default: socket.Disconnect(); // Invalid protocol break; } }
/// <summary> /// Send callback /// </summary> /// <param name="socket">client socket</param> /// <param name="status">stend status</param> /// <param name="sentPacket">sent packet</param> public void OnSent(INetworkSocket socket, SendStatus status, Packet sentPacket) { ParallelPacket sentParallelPacket = ParallelPacket.FromPacket(sentPacket); if (sentParallelPacket.PacketType == ParallelPacketType.DATA) { lock (m_sendLock) { m_pendingPacketSet.Remove(sentParallelPacket); if (status == SendStatus.SUCCESS || status == SendStatus.FAIL_INVALID_PACKET) { m_pendingClientSet.Add(socket); } if (status != SendStatus.SUCCESS) { m_errorPacketSet.Add(sentParallelPacket); } if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } Task t = new Task(delegate() { OnParallelSocketSent(this, status, sentParallelPacket); }); t.Start(); } }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkSocket socket, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.PacketType) { case ParallelPacketType.IDENTITY_RESPONSE: PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(receivedParallelPacket.PacketRaw,receivedParallelPacket.HeaderSize,receivedParallelPacket.DataByteSize); IdentityResponse response = serializer.ClonePacketObj(); Guid guid = response.m_guid; int streamCount = response.m_streamCount; lock (m_listLock) { if (m_socketMap.ContainsKey(guid)) { m_socketMap[guid].AddSocket(socket); } else { if (CallBackObj == null) { socket.Disconnect(); return; } if (Acceptor.OnAccept(this, socket.IPInfo, streamCount)) { // Create new Parallel Socket IParallelSocketCallback socketCallback = Acceptor.GetSocketCallback(); ParallelSocket parallelSocket = new ParallelSocket(guid,socket, this); parallelSocket.CallBackObj = socketCallback; parallelSocket.Start(); m_socketMap[guid] = parallelSocket; OnParallelServerAccepted(this, parallelSocket); } else { // Rejected by server socket.Disconnect(); } } } break; default: // Invalid protocol socket.Disconnect(); break; } }
/// <summary> /// NewConnection callback /// </summary> /// <param name="socket">client socket</param> public void OnNewConnection(INetworkSocket socket) { // Request Identity of new connected socket ParallelPacket sendPacket = new ParallelPacket(-1, ParallelPacketType.IDENTITY_REQUEST,null); socket.Send(sendPacket.GetPacketRaw()); }
/// <summary> /// Broadcast the given packet to all the client, connected /// </summary> /// <param name="sender">sender of the broadcast</param> /// <param name="data">data in byte array</param> /// <param name="offset">offset in bytes</param> /// <param name="dataSize">data size in bytes</param> public void Broadcast(INetworkSocket sender, byte[] data, int offset, int dataSize) { Broadcast(sender, new Packet(data, offset, dataSize, false)); }
/// <summary> /// Detach pair /// </summary> public void DetachPair() { if (Paired) { lock (m_generalLock) { if (m_socket1 != null) m_socket1.CallBackObj = null; if (m_socket2 != null) m_socket2.CallBackObj = null; Paired = false; Task t = new Task(delegate() { OnDetached(this, m_socket1, m_socket2); }); t.Start(); m_socket1 = null; m_socket2 = null; } } }
public void OnSent(INetworkSocket socket, SendStatus status, Packet sentPacket) { switch (status) { case SendStatus.SUCCESS: Debug.WriteLine("SEND Success"); break; case SendStatus.FAIL_CONNECTION_CLOSING: Debug.WriteLine("SEND failed due to connection closing"); break; case SendStatus.FAIL_INVALID_PACKET: Debug.WriteLine("SEND failed due to invalid socket"); break; case SendStatus.FAIL_NOT_CONNECTED: Debug.WriteLine("SEND failed due to no connection"); break; case SendStatus.FAIL_SOCKET_ERROR: Debug.WriteLine("SEND Socket Error"); break; } }
/// <summary> /// Detach the given client from the server management /// </summary> /// <param name="clientSocket">the client to detach</param> /// <returns>the number of socket in the room</returns> public int DetachClient(INetworkSocket socket) { lock (m_listLock) { m_socketList.Remove(socket); Task t = new Task(delegate() { OnLeave(this, socket); }); t.Start(); return m_socketList.Count; } }
/// <summary> /// Disconnect callback /// </summary> /// <param name="socket">client socket</param> public void OnDisconnect(INetworkSocket socket) { lock (m_generalLock) { m_clientSet.Remove(socket); lock (m_sendLock) { m_pendingClientSet.Remove(socket); } CurSocketCount--; if (CurSocketCount <= 0) { lock (m_sendLock) { m_packetQueue.Clear(); m_pendingPacketSet.Clear(); m_errorPacketSet.Clear(); } lock (m_receiveLock) { m_receivedQueue.Clear(); } m_sendReadyEvent.SetEvent(); IsConnectionAlive = false; m_server.DetachClient(this); if (CallBackObj != null) { Task t = new Task(delegate() { CallBackObj.OnDisconnect(this); }); t.Start(); } } } }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkSocket socket, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.GetPacketType()) { case ParallelPacketType.DATA: if (m_receiveType == ReceiveType.BURST) { if (CallBackObj != null) { // Task t = new Task(delegate() // { // CallBackObj.OnReceived(this, receivedParallelPacket); // }); // t.Start(); CallBackObj.OnReceived(this, receivedParallelPacket); } } else if (m_receiveType == ReceiveType.SEQUENTIAL) { lock (m_receiveLock) { m_receivedQueue.Enqueue(receivedParallelPacket); while (m_curReceivedPacketId == -1 || (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().GetPacketID())) { ParallelPacket curPacket = m_receivedQueue.Dequeue(); m_curReceivedPacketId = curPacket.GetPacketID(); if (CallBackObj != null) { // Task t = new Task(delegate() // { // CallBackObj.OnReceived(this, curPacket); // }); // t.Start(); m_callBackObj.OnReceived(this, curPacket); } } } } break; default: socket.Disconnect(); // Invalid protocol break; } }
public void AddSocket(INetworkSocket socket) { m_clientSet.Add(socket); CurSocketCount++; ((IocpTcpSocket)socket).CallBackObj = this; ParallelPacket sendPacket = new ParallelPacket(getCurPacketSequence(), ParallelPacketType.READY, null); socket.Send(sendPacket.GetPacketRaw()); lock (m_sendLock) { m_pendingClientSet.Add(socket); if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } }
/// <summary> /// Disconnect callback /// </summary> /// <param name="socket">client socket</param> public void OnDisconnect(INetworkSocket socket) { lock (m_generalLock) { m_clientSet.Remove(socket); lock (m_sendLock) { m_pendingClientSet.Remove(socket); } CurSocketCount--; if (CurSocketCount <= 0) { lock (m_sendLock) { m_packetQueue.Clear(); m_pendingPacketSet.Clear(); m_errorPacketSet.Clear(); } lock (m_receiveLock) { m_receivedQueue.Clear(); } m_sendReadyEvent.SetEvent(); IsConnectionAlive = false; m_server.DetachClient(this); List<IParallelRoom> roomList = Rooms; foreach (IRoom room in roomList) { ((ParallelServer)m_server).Leave(this, room.RoomName); } lock (m_roomLock) { m_roomMap.Clear(); } Task t = new Task(delegate() { OnParallelSocketDisconnect(this); }); t.Start(); } } }
public void OnDisconnect(INetworkSocket socket) { m_socketList.Remove(socket); String sendString = "** User(" + socket.IPInfo.IPAddress + ") disconnected!"; AddMsg(sendString); byte[] sendBuff= BytesFromString(sendString); foreach (var socketObj in m_socketList) { if (socketObj != socket) { socketObj.Send(new Packet(sendBuff,0,sendBuff.Count(),false)); } } }
public void AddSocket(INetworkSocket socket) { lock (m_listLock) { m_socketList.Add(socket); } Task t = new Task(delegate() { OnJoin(this,socket); }); t.Start(); }
public void OnServerAccepted(INetworkServer server, INetworkSocket socket) { }
/// <summary> /// Broadcast the given packet to all the client, connected /// </summary> /// <param name="sender">sender of the broadcast</param> /// <param name="packet">packet to broadcast</param> public void Broadcast(INetworkSocket sender, Packet packet) { List<INetworkSocket> list = GetSocketList(); foreach (INetworkSocket socket in list) { if(socket!=sender) socket.Send(packet); } Task t = new Task(delegate() { OnBroadcast(this,sender, packet); }); t.Start(); }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkSocket socket, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.GetPacketType()) { case ParallelPacketType.IDENTITY_RESPONSE: PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(receivedParallelPacket.GetPacketRaw(),receivedParallelPacket.GetHeaderSize(),receivedParallelPacket.GetDataByteSize()); Guid guid = serializer.GetPacket().m_guid; lock (m_listLock) { if (m_socketMap.ContainsKey(guid)) { m_socketMap[guid].AddSocket(socket); } else { IParallelSocketCallback socketCallback = CallBackObj.OnAccept(this, socket.IPInfo); if (socketCallback != null) { // Create new Parallel Socket ParallelSocket parallelSocket = new ParallelSocket(guid,socket, this); parallelSocket.CallBackObj = socketCallback; parallelSocket.Start(); m_socketMap[guid] = parallelSocket; } else { // Rejected by server socket.Disconnect(); } } } break; default: // Invalid protocol socket.Disconnect(); break; } }
/// <summary> /// Broadcast the given packet to all the client, connected /// </summary> /// <param name="sender">sender of the broadcast</param> /// <param name="data">data in byte array</param> public void Broadcast(INetworkSocket sender, byte[] data) { Broadcast(sender, new Packet(data, 0, data.Count(), false)); }
/// <summary> /// Join the room /// </summary> /// <param name="socket">socket</param> /// <param name="roomName">room name</param> /// <returns>the instance of the room</returns> public Room Join(INetworkSocket socket, string roomName) { lock (m_roomLock) { Room curRoom=null; if (m_roomMap.ContainsKey(roomName)) { curRoom=m_roomMap[roomName]; } else { curRoom= new Room(roomName,RoomCallBackObj); m_roomMap[roomName] = curRoom; } curRoom.AddSocket(socket); return curRoom; } }
/// <summary> /// Connect given two socket as p2p /// </summary> /// <param name="socket1">first socket</param> /// <param name="socket2">second socket</param> /// <param name="callback">callback object</param> /// <returns>true if paired otherwise false</returns> public bool ConnectPair(INetworkSocket socket1, INetworkSocket socket2, IP2PCallback callBackObj) { if (!Paired) { if (socket1 != null && socket2 != null && socket1 != socket2 && socket1.IsConnectionAlive && socket2.IsConnectionAlive) { lock (m_generalLock) { m_socket1 = socket1; m_socket2 = socket2; m_socket1.CallBackObj = this; m_socket2.CallBackObj = this; Paired = true; CallBackObj = callBackObj; return true; } } } return false; }
/// <summary> /// Send callback /// </summary> /// <param name="socket">client socket</param> /// <param name="status">stend status</param> /// <param name="sentPacket">sent packet</param> public void OnSent(INetworkSocket socket, SendStatus status, Packet sentPacket) { }
/// <summary> /// Default constructor /// </summary> /// <param name="client">client</param> /// <param name="server">managing server</param> public ParallelSocket(Guid guid,INetworkSocket client, IParallelServer server) : base() { m_guid = guid; m_server = server; IPInfo = client.IPInfo; ReceiveType = server.ReceiveType; MaxStreamCountPerSocket = server.MaxStreamCountPerSocket; AddSocket(client); }
/// <summary> /// Disconnect callback /// </summary> /// <param name="socket">client socket</param> public void OnDisconnect(INetworkSocket socket) { DetachPair(); }
/// <summary> /// Detach given socket from the given room /// </summary> /// <param name="socket">socket to detach</param> /// <param name="roomName">room name</param> /// <returns>number of sockets left in the room</returns> public int Leave(INetworkSocket socket, string roomName) { lock (m_roomLock) { if (m_roomMap.ContainsKey(roomName)) { int numSocketLeft = m_roomMap[roomName].DetachClient(socket); if (numSocketLeft == 0) { m_roomMap.Remove(roomName); } return numSocketLeft; } return 0; } }
public void AddSocket(INetworkSocket socket) { lock (m_generalLock) { if (MaxStreamCountPerSocket != SocketCount.Infinite && m_clientSet.Count > MaxStreamCountPerSocket) { socket.Disconnect(); return; } m_clientSet.Add(socket); } CurSocketCount++; ((IocpTcpSocket)socket).CallBackObj = this; ParallelPacket sendPacket = new ParallelPacket(-1, ParallelPacketType.READY, null); socket.Send(sendPacket.PacketRaw); lock (m_sendLock) { m_pendingClientSet.Add(socket); if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } }
/// <summary> /// NewConnection callback /// </summary> /// <param name="socket">client socket</param> public void OnNewConnection(INetworkSocket socket) { // Will never get called }
/// <summary> /// Disconnect callback /// </summary> /// <param name="socket">client socket</param> public void OnDisconnect(INetworkSocket socket) { }