コード例 #1
0
 public virtual object SessionCreate(Application.SessionCreationInfo sessionInfo)
 {
     return(null);
 }
コード例 #2
0
            private void ThreadTarget()
            {
                var sessionInfo = new Application.SessionCreationInfo()
                {
                    Endpoint = socket.RemoteEndPoint
                };

                if (logger != null)
                {
                    logger.Log(LogLevel.Info, string.Format("Accepted connection from {0}", sessionInfo.Endpoint));
                }

                config          = new SLChannel();
                config.Endpoint = socket.RemoteEndPoint as IPEndPoint;
                config.Session  = app.SessionCreate(sessionInfo);
                config.SLState  = ConnectionState.Opening;

                int recvAccumSize = 0;
                var recvBuffer    = new byte[maximumMessageSize];

                DateTime lastServerAliveNotification = DateTime.MinValue;

                while (socket.Connected)
                {
                    if (threadAbort)
                    {
                        break;
                    }

                    while (NeedsPulse())
                    {
                        Monitor.Enter(csDispatching);

                        try
                        {
                            //var sw = new System.Diagnostics.Stopwatch();
                            //sw.Start();
                            if (!Pulse())
                            {
                                if (logger != null)
                                {
                                    logger.Log(LogLevel.Error, "Pulse failed");
                                }
                                recvAccumSize = -1;
                                break;
                            }
                            //sw.Stop();
                            //Console.WriteLine("Pulse in {0}", sw.Elapsed.ToString());
                        }
                        finally
                        {
                            Monitor.Exit(csDispatching);
                        }
                    }

                    if (!socket.Poll(PulseInterval * 1000, SelectMode.SelectRead))
                    {
                        continue;
                    }

                    int bytesRead      = 0;
                    int bytesAvailable = maximumMessageSize - recvAccumSize;
                    if (bytesAvailable < 0)
                    {
                        break;
                    }

                    try
                    {
                        bytesRead = socket.Receive(recvBuffer, recvAccumSize, bytesAvailable, SocketFlags.None);
                    }
                    catch
                    {
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        // Disconnected
                        break;
                    }

                    recvAccumSize += bytesRead;
                    if (recvAccumSize > maximumMessageSize)
                    {
                        if (logger != null)
                        {
                            logger.Log(LogLevel.Error, string.Format("Received {0} but maximum message size is {1}", recvAccumSize, maximumMessageSize));
                        }
                        break;
                    }

                    while (recvAccumSize > 0 && UAStatusCode == (uint)StatusCode.Good)
                    {
                        Monitor.Enter(csDispatching);
                        int consumedSize = -1;

                        try
                        {
                            consumedSize = Consume(new MemoryBuffer(recvBuffer, recvAccumSize));
                        }
                        catch (NotImplementedException)
                        {
                            if (UAStatusCode == (uint)StatusCode.Good)
                            {
                                UAStatusCode = (uint)StatusCode.BadNotImplemented;
                            }
                            consumedSize = ErrorInternal;
                        }
                        catch (Exception ex)
                        {
                            consumedSize = ErrorInternal;
                        }
                        finally
                        {
                            Monitor.Exit(csDispatching);
                        }

                        if (consumedSize < 0)
                        {
                            if (UAStatusCode == (uint)StatusCode.Good)
                            {
                                if (consumedSize == ErrorInternal)
                                {
                                    UAStatusCode = (uint)StatusCode.BadInternalError;
                                }
                                if (consumedSize == ErrorParseFail)
                                {
                                    UAStatusCode = (uint)StatusCode.BadDecodingError;
                                }
                                if (consumedSize == ErrorRespWrite)
                                {
                                    UAStatusCode = (uint)StatusCode.BadEncodingLimitsExceeded;
                                }
                            }

                            // Handler failed
                            recvAccumSize = -1;
                            break;
                        }
                        else if (consumedSize == 0)
                        {
                            // Not enough to read a message
                            break;
                        }
                        else if (consumedSize >= recvAccumSize)
                        {
                            if (consumedSize > recvAccumSize)
                            {
                                if (logger != null)
                                {
                                    logger.Log(LogLevel.Error, string.Format("Consumed {0} but accumulated message size is {1}", consumedSize, recvAccumSize));
                                }
                            }

                            recvAccumSize = 0;
                        }
                        else
                        {
                            int newSize = recvAccumSize - consumedSize;

                            var newRecvBuffer = new byte[maximumMessageSize];
                            Array.Copy(recvBuffer, consumedSize, newRecvBuffer, 0, newSize);
                            recvBuffer = newRecvBuffer;

                            recvAccumSize = newSize;
                        }

                        if (UAStatusCode != (uint)StatusCode.Good)
                        {
                            threadAbort = true;
                        }
                    }

                    // Cannot receive more or process existing
                    if (recvAccumSize >= maximumMessageSize)
                    {
                        if (logger != null)
                        {
                            logger.Log(LogLevel.Error, string.Format("Received {0} but maximum message size is {1}", recvAccumSize, maximumMessageSize));
                        }
                        break;
                    }

                    if (recvAccumSize < 0)
                    {
                        break;
                    }
                }

                if (UAStatusCode != (uint)StatusCode.Good)
                {
                    TLError(UAStatusCode);
                }

                if (config.Session != null)
                {
                    app.SessionRelease(config.Session);
                }

                socket.Shutdown(SocketShutdown.Send);
                socket.Close();
                server.RemoveDispatcher(this);

                //foreach (var cfg in monitorMap.Values)
                //{
                //	app.MonitorDispatcherRemove(cfg);
                //}

                if (logger != null)
                {
                    logger.Log(LogLevel.Info, string.Format("Ended connection from {0}", sessionInfo.Endpoint));
                }
            }
コード例 #3
0
        private void ThreadTarget()
        {
            Application.SessionCreationInfo sessionInfo = new Application.SessionCreationInfo()
            {
                Endpoint = this.socket.RemoteEndPoint
            };
            if (this.logger != null)
            {
                this.logger.Log(LogLevel.Info, string.Format("Accepted connection from {0}", sessionInfo.Endpoint));
            }

            this.config          = new SLChannel();
            this.config.Endpoint = this.socket.RemoteEndPoint as IPEndPoint;
            this.config.Session  = this.app.SessionCreate(sessionInfo);
            this.config.SLState  = ConnectionState.Opening;
            int num1 = 0;

            byte[]   numArray1 = new byte[this.maximumMessageSize];
            DateTime minValue  = DateTime.MinValue;

            while (this.socket.Connected && !this.threadAbort)
            {
                while (this.NeedsPulse())
                {
                    Monitor.Enter(this.csDispatching);
                    try
                    {
                        if (!this.Pulse())
                        {
                            if (this.logger != null)
                            {
                                this.logger.Log(LogLevel.Error, "Pulse failed");
                            }

                            num1 = -1;
                            break;
                        }
                    }
                    finally
                    {
                        Monitor.Exit(this.csDispatching);
                    }
                }
                if (this.socket.Poll(100000, SelectMode.SelectRead))
                {
                    int size = this.maximumMessageSize - num1;
                    if (size >= 0)
                    {
                        int num2;
                        try
                        {
                            num2 = this.socket.Receive(numArray1, num1, size, SocketFlags.None);
                        }
                        catch
                        {
                            break;
                        }
                        if (num2 != 0)
                        {
                            num1 += num2;
                            if (num1 > this.maximumMessageSize)
                            {
                                if (this.logger != null)
                                {
                                    this.logger.Log(LogLevel.Error, string.Format("Received {0} but maximum message size is {1}", num1, maximumMessageSize));
                                    break;
                                }
                                break;
                            }
                            while (num1 > 0 && this.UAStatusCode == 0U)
                            {
                                Monitor.Enter(this.csDispatching);
                                int sourceIndex = -1;
                                try
                                {
                                    sourceIndex = this.Consume(new MemoryBuffer(numArray1, num1));
                                }
                                catch (NotImplementedException)
                                {
                                    if (this.UAStatusCode == 0U)
                                    {
                                        this.UAStatusCode = 2151677952U;
                                    }

                                    sourceIndex = -1;
                                }
                                catch
                                {
                                    sourceIndex = -1;
                                }
                                finally
                                {
                                    Monitor.Exit(this.csDispatching);
                                }
                                if (sourceIndex < 0)
                                {
                                    if (this.UAStatusCode == 0U)
                                    {
                                        if (sourceIndex == -1)
                                        {
                                            this.UAStatusCode = 2147614720U;
                                        }

                                        if (sourceIndex == -2)
                                        {
                                            this.UAStatusCode = 2147942400U;
                                        }

                                        if (sourceIndex == -3)
                                        {
                                            this.UAStatusCode = 2148007936U;
                                        }
                                    }
                                    num1 = -1;
                                    break;
                                }
                                if (sourceIndex != 0)
                                {
                                    if (sourceIndex >= num1)
                                    {
                                        if (sourceIndex > num1 && this.logger != null)
                                        {
                                            this.logger.Log(LogLevel.Error, string.Format("Consumed {0} but accumulated message size is {1}", sourceIndex, num1));
                                        }

                                        num1 = 0;
                                    }
                                    else
                                    {
                                        int    length    = num1 - sourceIndex;
                                        byte[] numArray2 = new byte[this.maximumMessageSize];
                                        Array.Copy(numArray1, sourceIndex, numArray2, 0, length);
                                        numArray1 = numArray2;
                                        num1      = length;
                                    }
                                    if (this.UAStatusCode > 0U)
                                    {
                                        this.threadAbort = true;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (num1 >= this.maximumMessageSize)
                            {
                                if (this.logger != null)
                                {
                                    this.logger.Log(LogLevel.Error, string.Format("Received {0} but maximum message size is {1}", num1, maximumMessageSize));
                                    break;
                                }
                                break;
                            }
                            if (num1 < 0)
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (this.UAStatusCode > 0U)
            {
                this.TLError(this.UAStatusCode);
            }

            if (this.config.Session != null)
            {
                this.app.SessionRelease(this.config.Session);
            }

            this.socket.Shutdown(SocketShutdown.Send);
            this.socket.Close();
            this.server.RemoveDispatcher(this);
            if (this.logger == null)
            {
                return;
            }

            this.logger.Log(LogLevel.Info, string.Format("Ended connection from {0}", sessionInfo.Endpoint));
        }