예제 #1
0
        private bool PostSynHostEntryMessage(IMalockSocket socket, string key, string identity, HostEntry entry)
        {
            if (entry == null || string.IsNullOrEmpty(identity) || string.IsNullOrEmpty(key))
            {
                return(false);
            }
            MalockNnsMessage message = new MalockNnsMessage();

            message.Key      = key;
            message.Identity = identity;
            message.Sequence = MalockMessage.NewId();
            message.Command  = MalockNnsMessage.SERVER_NNS_COMMAND_SYN_HOSTENTRYINFO;
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(stream))
                {
                    message.Serialize(bw);
                    if (entry != null)
                    {
                        entry.Serialize(bw);
                    }
                    MalockMessage.TrySendMessage(socket, stream);
                }
            }
            return(true);
        }
예제 #2
0
        private void QueryHostEntry(MalockSocket socket, int sequence, string key)
        {
            string           identity;
            HostEntry        entry   = this.nnsTable.GetEntry(key, out identity);
            MalockNnsMessage message = new MalockNnsMessage();

            message.Key      = key;
            message.Sequence = sequence;
            message.Command  = MalockMessage.COMMON_COMMAND_ERROR;
            if (entry != null)
            {
                message.Command = MalockNnsMessage.CLIENT_COMMAND_QUERYHOSTENTRYINFO;
            }
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(stream))
                {
                    message.Serialize(bw);
                    if (entry != null)
                    {
                        entry.Serialize(bw);
                    }
                    MalockMessage.TrySendMessage(socket, stream);
                }
            }
            this.PostSynHostEntryMessage(this.nnsStanbyClient, key, identity, entry);
        }
예제 #3
0
            internal static int DeserializeAll(Stream stream, Action <Host> callback)
            {
                if (stream == null)
                {
                    throw new ArgumentNullException("stream");
                }
                if (callback == null)
                {
                    throw new ArgumentNullException("callback");
                }
                BinaryReader br = new BinaryReader(stream);

                if (!MalockMessage.StreamIsReadable(stream, sizeof(int)))
                {
                    return(0);
                }
                int len   = br.ReadInt32();
                int count = 0;

                for (int i = 0; i < len; i++)
                {
                    Host host;
                    if (!Host.TryDeserialize(br, out host))
                    {
                        break;
                    }
                    count++;
                    callback(host);
                }
                return(count);
            }
예제 #4
0
        protected override void OnConnected(object sender, EventArgs e)
        {
            MalockNnsMessage message = new MalockNnsMessage();

            message.Command  = MalockNnsMessage.SERVER_NNS_COMMAND_DUMPHOSTENTRYINFO;
            message.Sequence = MalockMessage.NewId();
            MalockMessage.TrySendMessage(this, message);
        }
예제 #5
0
        private bool TryPostEnterMessage(int millisecondsTimeout, Action <int, MalockMessage, Stream> callback,
                                         ref Exception exception)
        {
            byte          cmd     = MalockNodeMessage.CLIENT_COMMAND_LOCK_ENTER;
            MalockMessage message = this.NewMesssage(cmd, millisecondsTimeout);

            return(MalockMessage.TryInvokeAsync(this.malock, message, millisecondsTimeout, callback, ref exception));
        }
예제 #6
0
 internal void Serialize(BinaryWriter writer)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     writer.Write(this.Available);
     MalockMessage.WriteStringToStream(writer, this.Address);
 }
예제 #7
0
 private void ProcessReceived(MemoryStream stream)
 {
     using (stream)
     {
         if (stream.Position >= stream.Length)
         {
             return;
         }
         bool debarkation = false;
         do
         {
             bool localTaken = false;
             this.connectwait.Enter(ref localTaken);
             if (localTaken)
             {
                 debarkation = this.connected;
                 if (!debarkation)
                 {
                     this.connected = true;
                 }
                 this.connectwait.Exit();
             }
         } while (false);
         if (!debarkation)
         {
             using (BinaryReader br = new BinaryReader(stream))
             {
                 IPEndPoint ipep = this.remoteep as IPEndPoint;
                 if (ipep != null)
                 {
                     this.LinkMode = br.ReadByte();
                     int port = br.ReadUInt16();
                     this.identity   = MalockMessage.FromStringInReadStream(br);
                     this.remoteport = ipep.Port;
                     this.address    = Ipep.ToIpepString(ipep.Address.ToString(), port);
                 }
             }
             if (string.IsNullOrEmpty(this.identity))
             {
                 this.Abort();
             }
             else
             {
                 this.OnConnected(EventArgs.Empty);
             }
         }
         else if (string.IsNullOrEmpty(this.identity))
         {
             this.Abort();
         }
         else
         {
             this.OnReceived(new MalockSocketStream(this, stream));
         }
     }
 }
예제 #8
0
        protected internal virtual bool TryGetAllInfo(int timeout, out IEnumerable <HandleInfo> infos, ref Exception exception)
        {
            IList <HandleInfo> out_infos     = emptryhandleinfos;
            Exception          out_exception = null;

            try
            {
                if (timeout <= 0 && timeout != -1)
                {
                    out_exception = EventWaitHandle.NewTimeoutException();
                    return(false);
                }
                using (ManualResetEvent events = new ManualResetEvent(false))
                {
                    bool success = false;
                    bool abort   = false;
                    if (!MalockMessage.TryInvokeAsync(this, MalockNodeMessage.New(null, this.Identity,
                                                                                  MalockNodeMessage.CLIENT_COMMAND_GETALLINFO, timeout), timeout,
                                                      (errno, message, stream) =>
                    {
                        if (errno == MalockMessage.Mappable.ERROR_NOERROR)
                        {
                            if (message.Command == MalockNodeMessage.CLIENT_COMMAND_GETALLINFO)
                            {
                                out_infos = new List <HandleInfo>();
                                success = HandleInfo.Fill(out_infos, stream);
                            }
                        }
                        else if (errno == MalockMessage.Mappable.ERROR_ABORTED)
                        {
                            abort = true;
                        }
                        else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT)
                        {
                            out_exception = EventWaitHandle.NewTimeoutException();
                        }
                        events.Set();
                    }, ref out_exception) || out_exception != null)
                    {
                        return(false);
                    }
                    events.WaitOne();
                    if (abort)
                    {
                        out_exception = EventWaitHandle.NewAbortedException();
                        return(false);
                    }
                    return(success);
                }
            }
            finally
            {
                infos     = out_infos;
                exception = out_exception;
            }
        }
예제 #9
0
        public void Serialize(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            BinaryWriter bw = new BinaryWriter(stream);

            bw.Write(this.Available);
            MalockMessage.WriteStringToStream(bw, this.Key);
            MalockMessage.WriteStringToStream(bw, this.Identity);
        }
예제 #10
0
 public void Handle(int error, MalockMessage message, Stream stream)
 {
     if (error == MalockMessage.Mappable.ERROR_ABORTED)
     {
         aborted = true;
     }
     else if (error == MalockMessage.Mappable.ERROR_NOERROR)
     {
         if (message.Command == MalockNodeMessage.CLIENT_COMMAND_LOCK_ENTER)
         {
             this.localTaken = true;
         }
     }
     this.Set();
 }
예제 #11
0
        private bool TryPostAckLockStateMessage(ref Exception exception)
        {
            byte errno = MalockNodeMessage.CLIENT_COMMAND_LOCK_ACKPIPELINEENTER;

            lock (this.syncobj)
            {
                if (this.enterthread == null)
                {
                    errno = MalockNodeMessage.CLIENT_COMMAND_LOCK_ACKPIPELINEEXIT;
                }
            }
            MalockMessage message = this.NewMesssage(errno, -1);

            return(MalockMessage.TrySendMessage(this.malock, message, ref exception));
        }
예제 #12
0
 protected internal virtual void GetAllInfoAsync(int timeout, Action <int, IEnumerable <HandleInfo> > state)
 {
     if (state == null)
     {
         throw new ArgumentNullException("callback");
     }
     if (timeout <= 0 && timeout != -1)
     {
         state(kERROR_TIMEOUT, emptryhandleinfos);
     }
     else
     {
         Exception exception = null;
         if (!MalockMessage.TryInvokeAsync(this,
                                           MalockNodeMessage.New(null, this.Identity, MalockNodeMessage.CLIENT_COMMAND_GETALLINFO, timeout), timeout,
                                           (errno, message, stream) =>
         {
             if (errno == MalockMessage.Mappable.ERROR_NOERROR)
             {
                 bool error = true;
                 if (message.Command == MalockNodeMessage.CLIENT_COMMAND_GETALLINFO)
                 {
                     IList <HandleInfo> results = new List <HandleInfo>();
                     if (HandleInfo.Fill(results, stream))
                     {
                         error = false;
                         state(kERROR_NOERROR, results);
                     }
                 }
                 if (error)
                 {
                     state(kERROR_ERRORNO, emptryhandleinfos);
                 }
             }
             else if (errno == MalockMessage.Mappable.ERROR_ABORTED)
             {
                 state(kERROR_ABORTED, emptryhandleinfos);
             }
             else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT)
             {
                 state(kERROR_TIMEOUT, emptryhandleinfos);
             }
         }, ref exception))
         {
             state(kERROR_ABORTED, emptryhandleinfos);
         }
     }
 }
예제 #13
0
            internal static bool TryDeserialize(BinaryReader reader, out Host host)
            {
                host = null;
                HostEntry entry;

                if (!HostEntry.TryDeserialize(reader, out entry))
                {
                    return(false);
                }
                string identity;

                if (!MalockMessage.TryFromStringInReadStream(reader, out identity))
                {
                    return(false);
                }
                host = new Host(identity, entry);
                return(true);
            }
예제 #14
0
 public SocketWorkContext(MalockSocket malock)
 {
     if (malock == null)
     {
         throw new ArgumentNullException("malock");
     }
     this.malock = malock;
     using (MemoryStream ms = new MemoryStream())
     {
         BinaryWriter bw = new BinaryWriter(ms);
         bw.Write(Convert.ToByte(malock.LinkMode));
         bw.Write(Convert.ToUInt16(malock.listenport));
         MalockMessage.WriteStringToStream(bw, malock.identity);
         this.connectauthbuf = ms.ToArray();
     }
     this.auxiliary       = new MalockSocketAuxiliary(this.syncobj, this.OnError, this.OnReceive);
     this.connectcallback = this.StartConnect;
 }
예제 #15
0
        public bool Abort(MalockTaskInfo info)
        {
            if (string.IsNullOrEmpty(info.Identity))
            {
                return(false);
            }
            this.malockTaskPoll.Remove(info.Identity);
            do
            {
                string[] keys;
                this.malockTable.FreeKeyCollection(info.Identity, out keys);
                this.AckPipelineEnter(info.Identity, keys);
            } while (false);
            MalockMessage message = this.NewMessage(info.Key, info.Identity, MalockNodeMessage.SERVER_COMMAND_SYN_FREE, info.Sequence, -1);

            MalockMessage.TrySendMessage(this.malockStandbyClient, message);
            return(true);
        }
예제 #16
0
        public bool Exit(MalockTaskInfo info)
        {
            byte errno = MalockNodeMessage.CLIENT_COMMAND_LOCK_EXIT;

            if (!this.malockTable.Exit(info.Key, info.Identity))
            {
                errno = MalockMessage.COMMON_COMMAND_ERROR;
            }
            using (Stream message = this.NewMessage(info.Key, info.Identity, errno, info.Sequence, info.Timeout).Serialize())
            {
                if (info.Socket != null)
                {
                    MalockMessage.TrySendMessage(info.Socket, message);
                }
                MalockMessage.TrySendMessage(malockStandbyClient, message);
            }
            this.AckPipelineEnter(info);
            return(true);
        }
예제 #17
0
        private void RegisterHostEntry(MalockSocket socket, int sequence, HostEntry entry)
        {
            MalockNnsMessage message = new MalockNnsMessage();

            message.Sequence = sequence;
            message.Command  = MalockMessage.COMMON_COMMAND_ERROR;
            if (entry != null)
            {
                lock (this.nnsTable.GetSynchronizationObject())
                {
                    if (this.nnsTable.Register(socket.Identity, entry))
                    {
                        message.Command = MalockNnsMessage.SERVER_NDN_COMMAND_REGISTERHOSTENTRYINFO;
                    }
                    this.nnsTable.SetAvailable(socket.Identity, socket.Address, true);
                }
            }
            MalockMessage.TrySendMessage(socket, message);
        }
예제 #18
0
        private void DumpHostEntry(MalockSocket socket, MalockNnsMessage message)
        {
            MalockNnsMessage msg = new MalockNnsMessage();

            msg.Sequence = message.Sequence;
            msg.Command  = message.Command;
            do
            {
                var hosts = this.nnsTable.GetAllHosts();
                lock (this.nnsTable)
                {
                    NnsTable.Host.SerializeAll(hosts, (count, stream) =>
                    {
                        using (MemoryStream ms = (MemoryStream)stream)
                        {
                            MalockMessage.TrySendMessage(socket, message, ms.GetBuffer(), 0, unchecked ((int)ms.Position));
                        }
                    });
                }
            } while (false);
        }
예제 #19
0
 public void GetAllHostEntryAsync(int timeout, Action <NnsError, IEnumerable <HostEntry> > state)
 {
     if (state == null)
     {
         throw new ArgumentNullException("state");
     }
     if (timeout <= 0 && timeout != -1)
     {
         state(NnsError.kTimeout, emptryentries);
     }
     else
     {
         Exception exception = null;
         if (!MalockMessage.TryInvokeAsync(this, this.NewMessage(null, MSG.CLIENT_COMMAND_DUMPHOSTENTRYINFO),
                                           timeout, (errno, message, stream) =>
         {
             if (errno == MalockMessage.Mappable.ERROR_NOERROR)
             {
                 IList <HostEntry> hosts = new List <HostEntry>();
                 NnsTable.Host.DeserializeAll(stream, (host) =>
                 {
                     HostEntry entry = host.Entry;
                     hosts.Add(entry);
                 });
                 state(NnsError.kSuccess, hosts);
             }
             else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT)
             {
                 state(NnsError.kTimeout, emptryentries);
             }
             else if (errno == MalockMessage.Mappable.ERROR_ABORTED)
             {
                 state(NnsError.kAborted, emptryentries);
             }
         }, ref exception))
         {
             state(NnsError.kAborted, emptryentries);
         }
     }
 }
예제 #20
0
 public bool Enter(MalockTaskInfo info)
 {
     if (!this.malockTable.Enter(info.Key, info.Identity))
     {
         return(false);
     }
     using (Stream message = this.NewMessage(info.Key, info.Identity, MalockNodeMessage.CLIENT_COMMAND_LOCK_ENTER,
                                             info.Sequence, info.Timeout).Serialize())
     {
         if (MalockMessage.TrySendMessage(info.Socket, message))
         {
             MalockMessage.TrySendMessage(this.malockStandbyClient, message);
             return(true);
         }
         else
         {
             this.malockTable.Exit(info.Key, info.Identity);
             this.AckPipelineEnter(info);
         }
     }
     return(false);
 }
예제 #21
0
        private void ProcessAborted(object sender, EventArgs e)
        {
            MalockSocket socket = (MalockSocket)sender;

            if (socket.LinkMode == MalockMessage.LINK_MODE_CLIENT)
            {
                if (!string.IsNullOrEmpty(socket.Identity))
                {
                    this.malockEngine.Abort(new MalockTaskInfo()
                    {
                        Type      = MalockTaskType.kAbort,
                        Key       = null,
                        Stopwatch = null,
                        Timeout   = -1,
                        Sequence  = MalockMessage.NewId(),
                        Socket    = socket,
                        Identity  = socket.Identity,
                    });
                }
            }
            else if (socket.LinkMode == MalockMessage.LINK_MODE_SERVER)
            {
                MalockNodeMessage message = (MalockNodeMessage)socket.UserToken;
                if (message != null)
                {
                    string[]    keys;
                    MalockTable malock = this.malockEngine.GetTable();
                    malock.Exit(message.Identity, out keys);
                    this.malockEngine.AckPipelineEnter(message.Identity, keys);
                }
            }
            lock (socket)
            {
                socket.Aborted   -= this.onAboredHandler;
                socket.Connected -= this.onConnectedHandler;
                socket.Received  -= this.onReceivedHandler;
            }
        }
예제 #22
0
            internal bool Deserialize(BinaryReader reader)
            {
                if (reader == null)
                {
                    throw new ArgumentNullException("reader");
                }
                Stream stream = reader.BaseStream;

                if (!MalockMessage.StreamIsReadable(stream, sizeof(bool)))
                {
                    return(false);
                }
                this.Available = reader.ReadBoolean();
                do
                {
                    string s;
                    if (!MalockMessage.TryFromStringInReadStream(reader, out s))
                    {
                        return(false);
                    }
                    this.Address = s;
                } while (false);
                return(true);
            }
예제 #23
0
 private void InternalRemoteGetHostEntryAsync(string key, int timeout, Action <NnsError, HostEntry> state, bool retrying)
 {
     if ((retrying && timeout <= 0) || (timeout <= 0 && timeout != -1))
     {
         state(NnsError.kTimeout, null);
     }
     else if (!this.Available)
     {
         state(NnsError.kAborted, null);
     }
     else
     {
         Stopwatch stopwatch = new Stopwatch();
         stopwatch.Start();
         Action <NnsClient> onaborted = (self) =>
         {
             if (!this.Available)
             {
                 state(NnsError.kAborted, null);
             }
             else
             {
                 var delaytick = Malock.NewTimer();
                 delaytick.Interval = Malock.SmoothingInvokeTime;
                 delaytick.Tick    += delegate
                 {
                     long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                     {
                         stopwatch.Stop();
                         delaytick.Stop();
                     }
                     if (!this.Available)
                     {
                         state(NnsError.kAborted, null);
                     }
                     else
                     {
                         if (timeout != -1)
                         {
                             timeout -= Convert.ToInt32(elapsedMilliseconds);
                         }
                         this.InternalRemoteGetHostEntryAsync(key, timeout, state, timeout == -1 ? false : true);
                     }
                 };
                 delaytick.Start();
             }
         };
         Action <int, MalockMessage, Stream> callback = (errno, response, stream) =>
         {
             bool closedstopwatch = true;
             if (errno == MalockMessage.Mappable.ERROR_NOERROR)
             {
                 if (response.Command != MSG.CLIENT_COMMAND_QUERYHOSTENTRYINFO)
                 {
                     state(NnsError.kError, null);
                 }
                 else
                 {
                     HostEntry entry;
                     if (!HostEntry.TryDeserialize(stream, out entry))
                     {
                         state(NnsError.kError, null);
                     }
                     else
                     {
                         state(NnsError.kSuccess, entry);
                     }
                 }
             }
             else if (errno == MalockMessage.Mappable.ERROR_ABORTED)
             {
                 closedstopwatch = false;
                 onaborted(this);
             }
             else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT)
             {
                 state(NnsError.kTimeout, null);
             }
             if (closedstopwatch)
             {
                 stopwatch.Stop();
             }
         };
         MalockMessage message   = this.NewMessage(key, MSG.CLIENT_COMMAND_QUERYHOSTENTRYINFO);
         Exception     exception = null;
         if (!MalockMessage.TryInvokeAsync(this, message, timeout, callback, ref exception))
         {
             onaborted(this);
         }
     }
 }
예제 #24
0
 internal void Serialize(BinaryWriter writer)
 {
     this.Entry.Serialize(writer);
     MalockMessage.WriteStringToStream(writer, this.Identity);
 }
예제 #25
0
        private bool TryPostExitMessage(ref Exception exception)
        {
            MalockMessage message = this.NewMesssage(MalockNodeMessage.CLIENT_COMMAND_LOCK_EXIT, -1);

            return(MalockMessage.TrySendMessage(this.malock, message, ref exception));
        }
예제 #26
0
 protected virtual void UnbindEventInMessage()
 {
     MalockMessage.Unbind(this);
 }
예제 #27
0
 protected virtual void BindEventToMessage()
 {
     MalockMessage.Bind(this);
 }
예제 #28
0
        protected virtual void OnConnected(object sender, EventArgs e)
        {
            MalockMessage message = MalockNodeMessage.New(null, this.Identity, MalockNodeMessage.SERVER_COMMAND_SYN_LOADALLINFO, -1);

            MalockMessage.TrySendMessage(this, message);
        }