private void OnDisconnectEvent(SCB scb) { if (_SCB != scb) { //if connect immediately after call disconnect. //the scb is the last connection and ignore it. return; } Connected = false; ClearChannelForSync(); EventHandler <Event.DisconnectEventArgs> disconnectEventHandler = RemoteDisconnected; if (disconnectEventHandler != null) { try { disconnectEventHandler(this, new Event.DisconnectEventArgs(scb.RemoteIPEndPoint, scb.CableId)); } catch { } } Disconnect(); }
/// <summary> /// /// </summary> /// <param name="ipEndPoint"></param> /// <param name="flag"></param> /// <param name="evt"></param> /// <param name="cableId"></param> /// <param name="channel"></param> /// <param name="data"></param> /// <exception cref="TcpException"></exception> /// <exception cref="socketException"></exception> private void InnerASendResponse(IPEndPoint ipEndPoint, MessageFlag flag, UInt32 evt, UInt16 cableId, UInt32 channel, byte[] data) { SCB scb = GetSCB(ipEndPoint); //scb.AsyncSend(flag, evt, cableId, channel, data); scb.ASendFromServer(flag, evt, cableId, channel, data); }
private void OnBatchReceive(SCB scb, List <Event.ReceiveEventArgs> argsList) { EventHandler <Event.ReceiveEventArgs> receiveEventHandler = DataReceived; if (receiveEventHandler != null) { _WorkThreads[scb.Id % _WorkThreads.Length].ASendMessages(argsList); } }
private void OnReceiveEvent(SCB scb, MessageFlag flag, UInt32 evt, UInt16 cableId, UInt32 channel, byte[] data) { SyncBlock syncBlock; if ((flag & MessageFlag.Sync) != 0) { if (channel > int.MaxValue) { //server side ssend EventHandler <Event.ReceiveEventArgs> receiveEventHandler = ReceiveEventHandler; if (receiveEventHandler != null) { try { Event.ReceiveEventArgs args = new Event.ReceiveEventArgs(scb.Id, scb.RemoteIPEndPoint, flag, evt, cableId, channel, data); receiveEventHandler(this, args); InnerASend(flag, evt, cableId, args.ReturnData); } catch { } } } else if (TryGetSyncChannel(channel, out syncBlock)) { syncBlock.RetData = data; syncBlock.AutoEvent.Set(); } } else { EventHandler <Event.ReceiveEventArgs> receiveEventHandler = ReceiveEventHandler; if (receiveEventHandler != null) { try { receiveEventHandler(this, new Event.ReceiveEventArgs(scb.Id, scb.RemoteIPEndPoint, flag, evt, cableId, channel, data)); } catch { } } } }
private void OnReceiveEvent(SCB scb, MessageFlag flag, UInt32 evt, UInt16 cableId, UInt32 channel, byte[] data) { EventHandler <Event.ReceiveEventArgs> receiveEventHandler = DataReceived; if (receiveEventHandler != null) { Event.ReceiveEventArgs args = new Event.ReceiveEventArgs(scb.Id, scb.RemoteIPEndPoint, flag, evt, cableId, channel, data); _WorkThreads[scb.Id % _WorkThreads.Length].ASendMessage(args); } }
/// <summary> /// Connect to server /// </summary> /// <param name="millisecondsTimeout">connect timeout, in millisecond</param> /// <param name="setThreadAffinityMask">need set the thread affinity mask</param> public void Connect(int millisecondsTimeout, bool setThreadAffinityMask) { lock (_ConnectLock) { if (Connected) { return; } else { Disconnect(); } if (millisecondsTimeout < 0) { throw new ArgumentException("milliseconds can't be negative"); } InitVar(); _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _Socket.Bind(this.BindIPEndPoint); _Socket.BeginConnect(this.RemoteIPEndPoint, Async_Connection, _Socket); if (!_ConnectEvent.WaitOne(millisecondsTimeout)) { Disconnect(); throw new NTcpException(string.Format("Try to connect to remote server {0} timeout", this.RemoteIPEndPoint), ErrorCode.ConnectTimeout); } else if (_ConnectException != null) { Disconnect(); throw _ConnectException; } _Socket.NoDelay = true; _Socket.SendBufferSize = 16 * 1024; _SCB = new SCB(_Socket); _SCB.OnReceive = OnReceiveEvent; _SCB.OnError = OnErrorEvent; _SCB.OnDisconnect = OnDisconnectEvent; _SendMessageQueue = new SendMessageQueue(OnReadyToSend, setThreadAffinityMask); Connected = true; this.OnConnectedEvent(); } }
private void AsyncAccept(IAsyncResult iar) { //get orginal socket we input as the argument in BeginAccept Socket orginalServer = (Socket)iar.AsyncState; //Get new socket based on orginal socket try { Socket workSocket = orginalServer.EndAccept(iar); workSocket.NoDelay = true; workSocket.SendBufferSize = 16 * 1024; try { OnAcceptEvent(workSocket.RemoteEndPoint); } catch (Exception e) { OnErrorEvent("OnAcceptEvent", e); } try { SCB scb = new SCB(this, workSocket); scb.OnError = OnErrorEvent; //scb.OnReceive = OnReceiveEvent; //OnReceiveEvent and OnBatchReceive can't be set in same time. scb.OnBatchReceive = OnBatchReceive; scb.OnDisconnect = OnDisconnectEvent; AddSCB(scb); } catch (Exception e) { OnErrorEvent("Accept", e); } } catch (Exception e) { OnErrorEvent("Accept", e); } try { _Server.BeginAccept(new AsyncCallback(AsyncAccept), _Server); } catch (Exception e) { OnErrorEvent("BeginAccept", e); } }
internal SCB[] GetAllSCB() { lock (_SCBLockObj) { SCB[] scbs = new SCB[_RemoteIPToSCB.Values.Count]; int i = 0; foreach (SCB scb in _RemoteIPToSCB.Values) { scbs[i++] = scb; } return(scbs); } }
/// <summary> /// Connect to server /// </summary> /// <param name="timeout">connect timeout, in seconds</param> public void Connect(int timeout) { _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _Socket.Bind(this.BindIPEndPoint); _Socket.Connect(this.RemoteIPEndPoint); _Socket.NoDelay = true; _Socket.SendBufferSize = 16 * 1024; _SCB = new SCB(_Socket); _SCB.OnReceive = OnReceiveEvent; _SendMessageQueue = new SendMessageQueue(OnReadyToSend); Connected = true; }
private void InitVar() { _ConnectEvent = new AutoResetEvent(false); _ConnectException = null; _SCB = null; _Socket = null; _SendMessageQueue = null; _Connected = false; _Closed = false; _ChannelSync = new object(); _CurChannel = 0; _SyncMessageLock = new object(); _SyncMessageDict = new Dictionary <uint, SyncBlock>(); }
/// <summary> /// Inner Asend to the client specified in ipEndPoint. /// </summary> /// <param name="flag"></param> /// <param name="evt"></param> /// <param name="cableId"></param> /// <param name="channel"></param> /// <param name="data"></param> /// <exception cref="TcpException"></exception> /// <exception cref="socketException"></exception> private bool InnerASend(IPEndPoint ipEndPoint, MessageFlag flag, UInt32 evt, UInt16 cableId, byte[] data) { SCB scb = GetSCB(ipEndPoint); if (scb == null) { return(false); } IncCurChannel(); //scb.AsyncSend(flag, evt, cableId, channel, data); scb.ASendFromServer(flag, evt, cableId, CurChannel, data); return(true); //SCB scb = _SCB; //scb.AsyncSend(flag, evt, cableId, channel, data); }
void AddSCB(SCB scb) { lock (_SCBLockObj) { if (_RemoteIPToSCB.ContainsKey(scb.RemoteIPEndPoint)) { //I don't think it will happen, because IPEndPoint can't be same. if (_RemoteIPToSCB[scb.RemoteIPEndPoint].WorkSocket.Connected) { _RemoteIPToSCB[scb.RemoteIPEndPoint].WorkSocket.Close(); } _RemoteIPToSCB[scb.RemoteIPEndPoint] = scb; } else { _RemoteIPToSCB.Add(scb.RemoteIPEndPoint, scb); } } }
private void ProcInnerMessage(Event.ReceiveEventArgs message) { try { switch ((InnerEvent)message.Event) { case InnerEvent.GetProcessorId: { //Get processorid and cableid //Only syncronize connection of single connection cable will //Send this event to server to alloc //Processor mask from client ulong mask = LittleEndianBitConverter.ToUInt64(message.Data, 0); //Get Processor id int pId = _AllocClientProcessor.GetProcessorId(((IPEndPoint)message.RemoteIPEndPoint).Address, mask, message.SCBID); //Get cable id SCB scb = GetSCB((IPEndPoint)message.RemoteIPEndPoint); UInt16 cableId = _CableIdAllocator.Alloc(scb.RemoteIPEndPoint); scb.CableId = cableId; message.ReturnData = new byte[sizeof(int) + sizeof(UInt16)]; Array.Copy(LittleEndianBitConverter.GetBytes(pId), 0, message.ReturnData, 0, sizeof(int)); Array.Copy(LittleEndianBitConverter.GetBytes(cableId), 0, message.ReturnData, sizeof(int), sizeof(UInt16)); } break; } } catch (Exception e) { OnErrorEvent("ProcInnerMessage", e); } }
internal void RemoteSCB(SCB scb) { lock (_SCBLockObj) { scb.Close(); if (_RemoteIPToSCB.ContainsKey(scb.RemoteIPEndPoint)) { try { if (_RemoteIPToSCB[scb.RemoteIPEndPoint].WorkSocket.Connected) { _RemoteIPToSCB[scb.RemoteIPEndPoint].WorkSocket.Close(); } } catch { } _RemoteIPToSCB.Remove(scb.RemoteIPEndPoint); } } }
private void OnReceiveEvent(SCB scb, MessageFlag flag, UInt32 evt, UInt16 group, UInt32 channel, byte[] data) { SyncBlock syncBlock; if ((flag & MessageFlag.Sync) != 0) { if (TryGetSyncChannel(channel, out syncBlock)) { syncBlock.RetData = data; syncBlock.AutoEvent.Set(); } } else { EventHandler <Event.ReceiveEventArgs> receiveEventHandler = ReceiveEventHandler; if (receiveEventHandler != null) { receiveEventHandler(this, new Event.ReceiveEventArgs( scb.RemoteIPEndPoint, flag, evt, group, channel, data)); } } }
private void OnDisconnectEvent(SCB scb) { RemoteClientPID(scb.RemoteIPEndPoint.Address, scb.Id); if (scb.CableId != 0) { _CableIdAllocator.Return(scb.CableId); } RemoteSCB(scb); EventHandler <Event.DisconnectEventArgs> disconnectEventHandler = RemoteDisconnected; if (disconnectEventHandler != null) { try { disconnectEventHandler(this, new Event.DisconnectEventArgs(scb.RemoteIPEndPoint, scb.CableId)); } catch { } } }
public void UpdateSCB() { SCBResult = SCB.ToString(); }
internal SendMessageTask GetTask(SCB scb) { return(_SendMessageTaskPool[scb.Id % _SendMessageTaskPool.Length]); }