/// <summary> /// Send callback /// </summary> /// <param name="socket">client socket</param> /// <param name="status">stend status</param> /// <param name="sentPacket">sent packet</param> public void OnParallelSocketSent(IParallelSocket socket, SendStatus status, ParallelPacket sentPacket) { string sentString = ASCIIEncoding.ASCII.GetString(sentPacket.CloneData()); Debug.Print("Sent [" + sentPacket.PacketID + "] " + sentString); }
/// <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> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnParallelSocketReceived(IParallelSocket socket, ParallelPacket receivedPacket) { byte[] receivedData = receivedPacket.CloneData(); string recvString = ASCIIEncoding.ASCII.GetString(receivedData); Debug.Print("Received [" + receivedPacket.PacketID + "] " + recvString); socket.Send(receivedData); }
/// <summary> /// Receive callback /// </summary> /// <param name="client">client</param> /// <param name="receivedPacket">received packet</param> public void OnParallelClientReceived(IParallelClient client, ParallelPacket receivedPacket) { string recvString=ASCIIEncoding.ASCII.GetString(receivedPacket.CloneData()); Debug.Print("Received [" + receivedPacket.PacketID + "] " + recvString); }
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> /// Receive callback /// </summary> /// <param name="client">client</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(IParallelClient client, ParallelPacket receivedPacket) { string recvString=ASCIIEncoding.ASCII.GetString(receivedPacket.GetData().ToArray()); Debug.Print("[" + receivedPacket.GetPacketID() + "] " + recvString); }
/// <summary> /// Send callback /// </summary> /// <param name="client">client</param> /// <param name="status">send status</param> /// <param name="sentPacket">sent packet</param> public void OnSent(IParallelClient client, SendStatus status, ParallelPacket sentPacket) { }
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> /// 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; } }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnParallelSocketReceived(IParallelSocket socket, ParallelPacket receivedPacket) { lock (m_generalLock) { if (socket == m_socket1) { m_socket2.Send(receivedPacket.PacketRaw, receivedPacket.HeaderSize, receivedPacket.DataByteSize); } else { m_socket1.Send(receivedPacket.PacketRaw, receivedPacket.HeaderSize, receivedPacket.DataByteSize); } } }
/// <summary> /// Send callback /// </summary> /// <param name="socket">client socket</param> /// <param name="status">stend status</param> /// <param name="sentPacket">sent packet</param> public void OnParallelSocketSent(IParallelSocket socket, SendStatus status, ParallelPacket sentPacket) { }
/// <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> /// 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> /// 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> /// Receive callback /// </summary> /// <param name="client">client</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkClient client, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.PacketType) { case ParallelPacketType.DATA: if (ReceiveType == ReceiveType.BURST) { OnParallelClientReceived(this, receivedParallelPacket); } else if (m_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; } OnParallelClientReceived(this, curPacket); } } } break; case ParallelPacketType.IDENTITY_REQUEST: PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(new IdentityResponse(Guid,MaxSocketCount)); ParallelPacket sendPacket=new ParallelPacket(-1,ParallelPacketType.IDENTITY_RESPONSE,serializer.PacketRaw); client.Send(sendPacket.PacketRaw); break; case ParallelPacketType.READY: lock (m_sendLock) { m_pendingClientSet.Add(client); if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } break; } }
/// <summary> /// Receive callback /// </summary> /// <param name="client">client</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkClient client, 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; case ParallelPacketType.IDENTITY_REQUEST: PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(new IdentityResponse(Guid)); ParallelPacket sendPacket=new ParallelPacket(getCurPacketSequence(),ParallelPacketType.IDENTITY_RESPONSE,serializer.GetPacketRaw()); client.Send(sendPacket.GetPacketRaw()); break; case ParallelPacketType.READY: lock (m_sendLock) { m_pendingClientSet.Add(client); if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } break; } }